HDU 2433 Travel

Travel

Time Limit: 2000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2433
64-bit integer IO format: %I64d      Java class name: Main

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

解题:看完别人的代码才知道这题是怎么回事。删除边的时候,每次删一条,但是每个删除是独立的,也就是这次删完后,得马上补回去。

用数组记录源点为x的时候,有那些边被选用了。删除的时候,看各源点求最短路时,是否用了此边,用了的,要再次求最短路。

大量重复边,及其无聊。

 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 }

时间: 2024-10-07 05:28:40

HDU 2433 Travel的相关文章

hdu 2433 Travel (最短路径树)

hdu 2433 Travel 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

HDU - 2433 Travel (最短路树)

题目大意:有N个点,M条边,每次删掉一条边,问删掉该边后,所有点之间的最短路的和是多少 解题思路:刚开始就想,删掉一次floyd一次,结果可想而之,TLE了 后来看了别人的,发现了一种叫做最短路树的东西. 就是先求出以每个点为源点的最短路并纪录该点到每个点的距离和,和每个点的pre,这样的话,就预处理好了 因为要删掉边,前面我们已经预处理好了最短路树的pre,也就是说,就可以依次判断删除的边是否在最短路树上,只要你要删除的边不在该最短路树上,那么就没有影响了,可以直接用前面纪录的数据 如果要删掉

HDU 5441 Travel 并查集

HDU 5441 Travel 题意:一张无向图,q个查询,对于每个x,有多少对点之间的路径中最长的一条路不大于x. 思路:比赛时王秋平写的,我补下题.这题也比较简单,将边排序,从小到大加到并查集,对查询也排序,从小到大对于每个查询把不大于x的边加到并查集,用cnt[y]记录以y为根的连通块有多少节点,那么在连通块发生 变化时,ans=2 * cnt[x] * cnt[y] 1 #include <iostream> 2 #include <cstdio> 3 #include &

HDU 4571 Travel in time (SPFA 或 dp)

HDU 4571 大概题意:n个点(<=100)m条边(<=1000)的无向图,每个点有消耗costp和价值moneyp,每条边有消耗coste.问从点s出发到点e,消耗不超过t(<=300)所获得的最大价值.经过边必有消耗coste:经过点时,当取得价值moneyp时消耗costp,即为visit该点:当取得价值0,时消耗也为0,即为pass该点.visit的点的moneyp值必须是严格升序. 解法: 容易看出应该用spfa和dp来解.关键时对visit和pass点的处理. 通过flo

hdu 5380 Travel with candy(双端队列)

题目链接:hdu 5380 Travel with candy 保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别可以以多少价格退货,以及可以推货的量.每到一个位置,可以该商店的sell值更新队列中所有价格小于sell的(还没有卖).用buy值更新队列中大于buy(卖掉了).移动所消耗的油从价格最低的开始. #include <cstdio> #include <cstring> #include <algorithm> using namespace st

HDU 2433 (最短路+BFS+剪枝)

http://acm.hdu.edu.cn/showproblem.php?pid=2433 这个问题因为路径都是1,所以可以用bfs遍历 可以看这几篇文章讲解: http://blog.csdn.net/panyanyany/article/details/7215069 (这篇代码非常清晰,而且效率很高) http://www.cppblog.com/keroro/archive/2013/05/27/200622.html?opt=admin 1 #include <cstdio> 2

hdu 5441 Travel 离线带权并查集

Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m

HDU 5380 Travel with candy (单调队列&amp;贪心)

本文纯属原创,转载请注明出处.http://blog.csdn.net/zip_fan,谢谢. 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5380. Travel with candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description There are n+1 cities on

hdu 4571 Travel in time (Floyd+记忆化搜索)

Travel in time Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1853    Accepted Submission(s): 374 Problem Description Bob gets tired of playing games, leaves Alice, and travels to Changsha alo