标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38804 Accepted Submission(s): 16925
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <string> 5 #include <algorithm> 6 #include <iomanip> 7 #include <vector> 8 #include <cmath> 9 using namespace std; 10 int N, M; 11 #define maxn 105 12 #define INF 0x3f3f3f3f 13 int mp[maxn][maxn]; 14 int dist[maxn], vis[maxn]; 15 void dijkstra(){ 16 for(int i = 1; i <= N; i++) dist[i] = INF; 17 memset(vis, 0, sizeof(vis)); 18 dist[1] = 0; 19 vis[1] = 1; 20 21 int pos = 1; 22 for(int i = 1; i <= N; i++){ 23 int min = INF; 24 for(int j = 1; j <= N; j++){ 25 if(!vis[j] && dist[j] < min){ 26 pos = j; min = dist[j]; 27 } 28 } 29 vis[pos] = 1; 30 for(int j = 1; j <= N; j++){ 31 if(!vis[j] && dist[j] > dist[pos] + mp[pos][j] ){ 32 dist[j] = dist[pos] + mp[pos][j]; 33 } 34 } 35 } 36 } 37 int main(){ 38 while(scanf("%d%d", &N, &M) && (N+M)){ 39 for(int i = 1; i <= N; i++){ 40 for(int j = 1; j <= N; j++) mp[i][j] = INF; 41 } 42 for(int i = 1; i <= M; i++){ 43 int a, b, c; 44 scanf("%d%d%d", &a, &b, &c); 45 mp[a][b] = c; 46 mp[b][a] = c; 47 } 48 dijkstra(); 49 printf("%d\n", dist[N]); 50 } 51 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/titicia/p/4520685.html