hdu 2433 最短路
Travel
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1354 Accepted Submission(s): 463
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.
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.
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
题意:给定一个图,定义一个SUM为图中任意两个顶点之间最短路的和,现在求去掉每一条边后SUM值,假设图不连通,输出INF。
思路:首先对每个顶点求最短路,构造一颗最短路树,假设去掉的边不是最短路树上的边,或者存在重边,则不影响结果,直接返回答案,否则重新计算。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;
const int maxn=110;
const int inf=10000000;
int head[maxn],tol,pre[maxn][maxn],dist[maxn],m,n,flag[maxn][maxn],sum[maxn];
struct node
{
int next,to,val;
}edge[2*maxn*maxn];
void add(int u,int v,int w)
{
edge[tol].to=v;
edge[tol].val=w;
edge[tol].next=head[u];
head[u]=tol++;
}
struct Node
{
int id,dist;
Node(int a=0,int b=0):id(a),dist(b){}
bool operator < (const Node &a) const
{
return dist>a.dist;
}
};
int fun(int st)
{
int i,j,u,v;
for(i=1;i<=n;i++)dist[i]=inf;dist[st]=0;
priority_queue<Node> q;
q.push(Node(st,0));
while(!q.empty())
{
Node ret=q.top();q.pop();
u=ret.id;
if(dist[u]<ret.dist)continue;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(flag[u][v]>0&&dist[v]>dist[u]+edge[i].val)
{
pre[st][v]=u;
dist[v]=dist[u]+edge[i].val;
q.push(Node(v,dist[v]));
}
}
}
int ans=0;
for(i=1;i<=n;i++)
{
if(dist[i]<inf)ans+=dist[i];
else return -1;
}
return ans;
}
struct PP
{
int st,ed;
}pp[3100];
int main()
{
int i,j,k,p,q;
while(~scanf("%d%d",&n,&m))
{
memset(head,-1,sizeof(head));tol=0;
memset(flag,0,sizeof(flag));
memset(sum,0,sizeof(sum));
int connect=1;
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j);
pp[k].st=i;
pp[k].ed=j;
if(flag[i][j]==0)
{
add(i,j,1);
add(j,i,1);
}
flag[i][j]++;
flag[j][i]++;
}
int mm=0;
for(i=1;i<=n;i++)
{
j=fun(i);
if(j==-1)
{
connect=0;
break;
}
sum[i]=j;
mm+=j;
}
for(i=1;i<=m;i++)
{
if(!connect)
{
puts("INF");continue;
}
int u=pp[i].st;
int v=pp[i].ed;
if(flag[u][v]>=2)
{
printf("%d\n",mm);
continue;
}
int ans=0;
int ok=1;
for(j=1;j<=n;j++)
{
if(pre[j][u]!=v&&pre[j][v]!=u)
{
ans+=sum[j];
continue;
}
flag[u][v]--;
flag[v][u]--;
k=fun(j);
flag[u][v]++;
flag[v][u]++;
if(k==-1)
{
ok=0;
puts("INF");
break;
}
ans+=k;
}
if(ok)printf("%d\n",ans);
}
}
return 0;
}
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;
const int maxn=110;
const int inf=10000000;
int head[maxn],tol,pre[maxn][maxn],dist[maxn],m,n,flag[maxn][maxn],sum[maxn];
struct node
{
int next,to,val;
}edge[2*maxn*maxn];
void add(int u,int v,int w)
{
edge[tol].to=v;
edge[tol].val=w;
edge[tol].next=head[u];
head[u]=tol++;
}
struct Node
{
int id,dist;
Node(int a=0,int b=0):id(a),dist(b){}
bool operator < (const Node &a) const
{
return dist>a.dist;
}
};
int fun(int st)
{
int i,j,u,v;
for(i=1;i<=n;i++)dist[i]=inf;dist[st]=0;
priority_queue<Node> q;
q.push(Node(st,0));
while(!q.empty())
{
Node ret=q.top();q.pop();
u=ret.id;
if(dist[u]<ret.dist)continue;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(flag[u][v]>0&&dist[v]>dist[u]+edge[i].val)
{
pre[st][v]=u;
dist[v]=dist[u]+edge[i].val;
q.push(Node(v,dist[v]));
}
}
}
int ans=0;
for(i=1;i<=n;i++)
{
if(dist[i]<inf)ans+=dist[i];
else return -1;
}
return ans;
}
struct PP
{
int st,ed;
}pp[3100];
int main()
{
int i,j,k,p,q;
while(~scanf("%d%d",&n,&m))
{
memset(head,-1,sizeof(head));tol=0;
memset(flag,0,sizeof(flag));
memset(sum,0,sizeof(sum));
int connect=1;
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j);
pp[k].st=i;
pp[k].ed=j;
if(flag[i][j]==0)
{
add(i,j,1);
add(j,i,1);
}
flag[i][j]++;
flag[j][i]++;
}
int mm=0;
for(i=1;i<=n;i++)
{
j=fun(i);
if(j==-1)
{
connect=0;
break;
}
sum[i]=j;
mm+=j;
}
for(i=1;i<=m;i++)
{
if(!connect)
{
puts("INF");continue;
}
int u=pp[i].st;
int v=pp[i].ed;
if(flag[u][v]>=2)
{
printf("%d\n",mm);
continue;
}
int ans=0;
int ok=1;
for(j=1;j<=n;j++)
{
if(pre[j][u]!=v&&pre[j][v]!=u)
{
ans+=sum[j];
continue;
}
flag[u][v]--;
flag[v][u]--;
k=fun(j);
flag[u][v]++;
flag[v][u]++;
if(k==-1)
{
ok=0;
puts("INF");
break;
}
ans+=k;
}
if(ok)printf("%d\n",ans);
}
}
return 0;
}