hdu2433
2015-09-11 20:26
316 查看
Travel
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2163 Accepted Submission(s): 721
Problem Description
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) towns in it. Each town products one kind of food, the food will be transported to all the towns. In addition, the trucks will always
take the shortest way. There are M (M <= 3000) two-way roads connecting the towns, and the length of the road is 1.
Let SUM be the total distance of the shortest paths between all pairs of the towns. Please write a program to calculate the new SUM after one of the M roads is destroyed.
Input
The input contains several test cases.
The first line contains two positive integers N, M. The following M lines each contains two integers u, v, meaning there is a two-way road between town u and v. The roads are numbered from 1 to M according to the order of the input.
The input will be terminated by EOF.
Output
Output M lines, the i-th line is the new SUM after the i-th road is destroyed. If the towns are not connected after the i-th road is destroyed, please output “INF” in the i-th line.
Sample Input
5 4 5 1 1 3 3 2 5 4 2 2 1 2 1 2
Sample Output
INF INF INF INF 2 2
Source
2008 Asia Chengdu Regional Contest Online
//边太多,所以不采用邻接表建 //continue尽量少用 #include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define INF 99999999 #define inf -0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a, b) memset(a, b, sizeof(a)) typedef long long ll; const int maxn=2*3000+100; int sum[maxn]; int n; struct edge{ int from,to; }e[maxn]; int first; int mp[101][101]; int d[101][101][101]; int dis[101]; int vis[101]; int nd[maxn]; int bfs(int u){ mem0(vis); mem0(dis); queue<int>Q; Q.push(u); vis[u]=1; int count_=1; int sum1=0; while(!Q.empty()){ int v=Q.front(); Q.pop(); for(int i=1;i<=n;i++){ if(vis[i]==0&&mp[v][i]){ if(first==1) d[u][v][i]=d[u][i][v]=1; vis[i]=1; dis[i]=dis[v]+1; sum1+=dis[i]; count_++; Q.push(i); } } } if(count_==n) return sum1; return INF; } int main(){ int m; int u,v; //freopen("2433.in","r",stdin); //freopen("2433.out","w",stdout); while(scanf("%d%d",&n,&m)!=EOF){ mem0(d); mem0(sum); mem0(mp); for(int k=1;k<=m;k++){ scanf("%d%d",&u,&v); mp[u][v]++; mp[v][u]++; e[k].from=u; e[k].to=v; } int flag1=1; int sum2=0; first=1; for(int j=1;j<=n;j++){ sum[j]=bfs(j); sum2+=sum[j]; if(sum[j]==INF){ flag1=0; break; } } if(flag1==0){ for(int i=1;i<=m;i++) printf("INF\n"); continue; } first=0; for(int k=1;k<=m;k++){ int sum1=0; if(mp[e[k].from][e[k].to]>1){ printf("%d\n",sum2); } else if(mp[e[k].from][e[k].to]==1){ int j; for(j=1;j<=n;j++){ if(d[j][e[k].from][e[k].to]==0){ sum1+=sum[j]; } else{ mp[e[k].from][e[k].to]--; mp[e[k].to][e[k].from]--; sum1+=bfs(j); mp[e[k].from][e[k].to]++; mp[e[k].to][e[k].from]++; if(sum1>=INF){ break; } } } if(j<=n) printf("INF\n"); else printf("%d\n",sum1); } } } return 0; }
相关文章推荐
- hdu2222Keywords Search AC自动机模板题
- ubuntu14.04 搭建java环境
- ZooKeeper集群配置
- 木头打大孔的新方法-燃烧法
- Processing 练习(10) - 条形码
- 新的征程
- yum ftp源搭建
- 使用GnuPG(PGP)加密信息及数字签名教程
- 矩阵模板
- 获取日期
- 权限(Permission denied)问题如何确认是Selinux 约束引起?
- 野生程序员成长记(一) Android系统特性与环境配置
- arm平台裸机程序下载-linux下的dnw工具解决方案
- 利用zxing识别图片二维码
- 如何设置SELinux 策略规则 ? 在Kernel Log 中出现"avc: denied" 要如何处理?
- IOS_UI_数据库
- linux iptables SNAT NAT 【原创】
- 《代码整洁之道》学习计划
- HBase相关概念
- 崔莺莺到宋楚瑜,张你怎么看待?