# Travel

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2616    Accepted Submission(s): 861

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 110
#define M 3050
#define INF 1<<28
int n,m;
int d[N],vis[N];
int used[N][N][N];
int sum[N],pre[N],num[N][N];
struct Edge
{
int v,next,w;
} edge[M*2];
void init()
{
cnt=0;
memset(used,0,sizeof(used));
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
}
{
edge[cnt].v=v;
edge[cnt].w=w;
}
int spfa(int s,int t)
{
queue<int> q;
for(int i=1;i<=n;i++)
d[i]=INF,vis[i]=0;
d[s]=0,vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
{
int v=edge[i].v,w=edge[i].w;
if(d[u]+w<d[v])
{
d[v]=d[u]+w;
if(!vis[v])
{
if(!t) used[s][u][v]=used[s][v][u]=1;
q.push(v);
vis[v]=1;
}
}
}
}
int ans=0;
for(int i=1; i<=n; i++)
{
if(d[i]==INF) return INF;
ans+=d[i];
}
return ans;
}
int main()
{
int u,v,mark,ans,s,t;
while(~scanf("%d %d",&n,&m))
{
ans=0;
init();
for(int i=0; i<m; i++)
{
scanf("%d %d",&u,&v);
num[u][v]++,num[v][u]++;
}
for(int i=1; i<=n; i++)
{
sum[i]=spfa(i,0);
if(sum[i]==INF)
{
ans=INF;
break;
}
ans+=sum[i];
}
for(int i=0; i<m; i++)
{
s=ans;
if(ans==INF)
{
printf("INF\n");
continue;
};
if(num[edge[i*2+1].v][edge[i*2].v]>1)
{
printf("%d\n",ans);
continue;
}
edge[i*2].w=edge[i*2+1].w=INF;
for(int j=1; j<=n; j++)
if(used[j][edge[i*2+1].v][edge[i*2].v])
{
t=spfa(j,1);
if(t==INF)break;
s=s-sum[j]+t;
}
if(t==INF) printf("INF\n");
else printf("%d\n",s);
edge[i*2].w=edge[i*2+1].w=1;
}
}
return 0;
}


#### hdu 2433 Travel 最短路 dijkstra算法。

2015-03-27 15:42:21

#### Hduoj2433【SPFA】

2016-02-17 20:05:04

#### 原来我还不会学习

2015-01-03 11:18:57

#### hdu 2433 Travel （最短路径树）

2015-08-17 21:07:43

#### hdu 2433 Travel(枚举删掉的边求取最短路)

2015-04-25 10:18:47

#### HDU 1673 题意难懂啊

2017-06-12 09:24:22

#### [HDU2433] Travel BFS+最短路生成树

2016-10-25 20:50:09

#### HDU 5441 Travel(最短路径变形）

2015-09-24 12:21:40

#### 2.栈溢出简单利用

2011-12-02 23:17:47

#### hdu 5226 Tom and matrix

2015-05-22 16:30:28