标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24029 Accepted Submission(s): 8358
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define max(a, b) a>b?a:b 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 const int maxn = 1000 + 5; 8 // int maxx; 9 int map[maxn][maxn], a[maxn], b[maxn]; 10 void Floyd() 11 { 12 for(int k = 1; k <= 1000; k++) 13 for(int i = 1; i <= 1000; i++) 14 if(map[i][k] != INF) //一个优化,不加就TLE; 15 for(int j = 1; j <= 1000; j++) 16 if(map[i][j] > map[i][k] + map[k][j]) 17 map[i][j] = map[i][k] + map[k][j]; 18 } 19 int main() 20 { 21 int t, s, d; 22 while(~scanf("%d %d %d", &t, &s, &d)) 23 { 24 int i, j, max = -1; 25 for(i = 1; i <= 1000; i++) 26 for(j = 1; j <= 1000; j++) 27 map[i][j]=(i==j?0:INF); 28 int u, v, w; 29 for(i = 1; i <= t; i++) 30 { 31 scanf("%d %d %d", &u, &v, &w); 32 //maxx = max( max(u, v), maxx); // 开始以为这边能省点,结果WA。 33 if(map[u][v] > w) 34 map[u][v] = map[v][u] = w; 35 } 36 for(i = 1; i <= s; i++) 37 scanf("%d", &a[i]); 38 for(j = 1; j <= d; j++) 39 scanf("%d", &b[j]); 40 Floyd(); 41 int min = INF; 42 for(i = 1; i <= s; i++) 43 for(j = 1; j <= d; j++){ 44 //printf("%d %d %d\n", ) 45 if(map[a[i]][b[j]] < min) 46 min = map[a[i]][b[j]]; 47 } 48 printf("%d\n", min); 49 } 50 return 0; 51 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4709071.html