HDU-一个人的旅行
View Code
1 #include<iostream> 2 using namespace std; 3 4 const int INF = 0X3fffffff; 5 const int Max = 1005; 6 bool used[Max]; 7 bool vist[Max]; 8 int map[Max][Max]; 9 int dis[Max]; 10 int n, m; 11 12 void Make_set() 13 { 14 for(int i=0; i<Max; i++) 15 { 16 for(int j=0; j<Max; j++) 17 map[i][j] = map[j][i] = INF; 18 dis[i] = INF; 19 } 20 memset(used, false, sizeof(used)); 21 memset(vist, false, sizeof(vist)); 22 } 23 24 void dijkstra(int a) 25 { 26 int minNode; 27 dis[a] = 0; 28 //used[a] = true; 29 for(int cnt=0; cnt<=n; cnt++) 30 { 31 int min = INF; 32 minNode = -1; 33 for(int i=0; i<=n; i++) 34 { 35 if(min>dis[i] && !used[i] && vist[i]) 36 { 37 minNode = i; 38 min = dis[i]; 39 } 40 } 41 if(minNode == -1) 42 break; 43 used[minNode] = true; 44 for(int j=0; j<=n; j++) 45 { 46 if(vist[j] && !used[j] && dis[j]>dis[minNode]+map[minNode][j]) 47 { 48 dis[j] = dis[minNode]+map[minNode][j]; 49 } 50 } 51 } 52 //return dis[n]; 53 } 54 55 int main() 56 { 57 int a, b, c; 58 int T, S, D; 59 int max_num; 60 while(cin>>T>>S>>D) 61 { 62 Make_set(); 63 max_num = 0; 64 for(int i=0; i<T; i++) 65 { 66 scanf("%d%d%d", &a, &b, &c); 67 if(map[a][b]>c) 68 map[a][b] = map[b][a] = c; 69 vist[a] = vist[b] =true; 70 a=a>b?a:b; 71 max_num = a>max_num?a:max_num; 72 } 73 for(int j=0; j<S; j++) 74 { 75 scanf("%d", &a); 76 map[a][0] = map[0][a] = 0; 77 dis[a] = 0; 78 } 79 n = max_num; 80 vist[0] = true; 81 dijkstra(0); 82 int sum = 0; 83 int min = INF; 84 while(D--) 85 { 86 scanf("%d", &a); 87 if(dis[a] < min) 88 min = dis[a]; 89 //sum += dis[a]; 90 } 91 cout<<min<<endl; 92 } 93 return 0; 94 }