标签:strong connected ota where idt esc char init necessary
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 7114 | Accepted: 2708 | Special Judge |
Description
Input
Output
Sample Input
5 7 1 4 1 1 3 300 3 1 10 1 2 16 2 3 100 2 5 15 5 3 20
Sample Output
1 3 5 2
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #define min(a, b) ((a) < (b) ? (a) : (b)) 6 #define max(a, b) ((a) > (b) ? (a) : (b)) 7 8 inline void read(long long &x) 9 { 10 x = 0;char ch = getchar(), c = ch; 11 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 12 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 13 if(c == ‘-‘)x = -x; 14 } 15 16 const long long INF = 0x3f3f3f3f; 17 const long long MAXN = 500 + 10; 18 const long long MAXM = 20000 + 10; 19 20 long long n,m,g[MAXN][MAXN], w[MAXN][MAXN], ans, zhuan[MAXN][MAXN], l,r,mid, fangan[MAXN], tot; 21 22 void dfs(long long s, long long t) 23 { 24 if(s == t || !zhuan[s][t]) 25 return; 26 dfs(s, zhuan[s][t]); 27 fangan[++ tot] = zhuan[s][t]; 28 dfs(zhuan[s][t], t); 29 } 30 31 int main() 32 { 33 while(scanf("%d %d", &n, &m) != EOF) 34 { 35 ans = INF; 36 memset(g, 0x2, sizeof(g)); 37 memset(w, 0x2, sizeof(w)); 38 memset(zhuan, 0, sizeof(zhuan)); 39 for(register long long i = 1;i <= m;++ i) 40 { 41 long long tmp1, tmp2; 42 read(tmp1), read(tmp2); 43 read(g[tmp1][tmp2]); 44 g[tmp2][tmp1] = w[tmp1][tmp2] = w[tmp2][tmp1] = g[tmp1][tmp2]; 45 } 46 for(register long long k = 1;k <= n;++ k) 47 { 48 for(register long long i = 1;i < k;++ i) 49 for(register long long j = 1;j < k;++ j) 50 { 51 if(i == j) continue; 52 if(ans > w[k][i] + g[i][j] + w[j][k]) 53 { 54 ans = w[k][i] + g[i][j] + w[j][k], l = i, r = j, mid = k; 55 tot = 0;fangan[++tot] = mid;fangan[++tot] = l;dfs(l, r);fangan[++tot] = r; 56 } 57 } 58 for(register long long i = 1;i <= n;++ i) 59 for(register long long j = 1;j <= n;++ j) 60 if(g[i][j] > g[i][k] + g[k][j]) 61 g[i][j] = g[i][k] + g[k][j], zhuan[i][j] = k; 62 } 63 if(ans >= INF) 64 printf("No solution."); 65 else 66 for(register long long i = 1;i <= tot;++ i) 67 printf("%lld ", fangan[i]); 68 putchar(‘\n‘); 69 } 70 return 0; 71 }
标签:strong connected ota where idt esc char init necessary
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7602666.html