标签:des style blog http color os io java ar
5 4 5 1 1 3 3 2 5 4 2 2 1 2 1 2
INF INF INF INF 2 2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxn = 110; 17 int sum[maxn],d[maxn],num[maxn][maxn],n,m; 18 bool arc[maxn][maxn],used[maxn][maxn][maxn],in[maxn]; 19 int q[maxn*maxn],head,tail,e[3002][2]; 20 int spfa(int s,bool is){ 21 for(int i = 1; i <= n; i++){ 22 in[i] = false; 23 d[i] = INF; 24 } 25 head = tail = d[s] = 0; 26 in[s] = true; 27 q[tail++] = s; 28 while(head < tail){ 29 int u = q[head++]; 30 in[u] = false; 31 for(int i = 1; i <= n; i++){ 32 if(arc[u][i] && d[i] > d[u]+1){ 33 d[i] = d[u]+1; 34 if(!in[i]){ 35 in[i] = true; 36 q[tail++] = i; 37 if(is) used[s][u][i] = used[s][i][u] = true; 38 } 39 } 40 } 41 } 42 int ans = 0; 43 for(int i = 1; i <= n; i++){ 44 if(d[i] == INF){ans = INF;break;} 45 ans += d[i]; 46 } 47 return ans; 48 } 49 int main(){ 50 int i,j,u,v,ans; 51 while(~scanf("%d %d",&n,&m)){ 52 memset(arc,false,sizeof(arc)); 53 memset(used,false,sizeof(used)); 54 memset(num,0,sizeof(num)); 55 memset(sum,0,sizeof(sum)); 56 for(i = 0; i < m; i++){ 57 scanf("%d %d",&u,&v); 58 arc[u][v] = arc[v][u] = true; 59 num[u][v]++; 60 num[v][u]++; 61 e[i][0] = u; 62 e[i][1] = v; 63 } 64 ans = 0; 65 for(i = 1; i <= n; i++){ 66 sum[i] = spfa(i,true); 67 if(sum[i] == INF){ans = INF;break;} 68 ans += sum[i]; 69 } 70 int tmp = 0; 71 for(i = 0; i < m; i++){ 72 u = e[i][0]; 73 v = e[i][1]; 74 int ans2 = ans; 75 if(ans == INF) puts("INF"); 76 else if(num[u][v] > 1) printf("%d\n",ans); 77 else{ 78 arc[u][v] = arc[v][u] = false; 79 for(j = 1; j <= n; j++){ 80 if(used[j][u][v]){ 81 tmp = spfa(j,false); 82 if(tmp == INF) break; 83 ans2 += tmp - sum[j]; 84 } 85 } 86 if(tmp == INF) puts("INF"); 87 else printf("%d\n",ans2); 88 arc[u][v] = arc[v][u] = true; 89 } 90 } 91 } 92 return 0; 93 }
标签:des style blog http color os io java ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3947408.html