最短路:HDU2433

 

Travel

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


 

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

 

出题者能不能好好把题意讲清楚...给你n个点m条边,问你删掉第 i 条边后的每个点到第 i 点的最短路的总和

bfs求每个点的最短路和,然后之后的每一次删边都看看这个条边是否有重边,或者是是否不在最短路上,

 

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 105 
#define INF 0x3f3f3f3f
int dis[N];
int sum[N];
int pre[N][N];
bool vis[N];
int x[3005], y[3005];
int conn[N][N];
int n, m;
int bfs(int s)
{
	for(int i = 1; i <= n; i++)
	{
		dis[i] = INF;
		vis[i] = false;
	}
	dis[s] = 0;
	vis[s] = true;
	queue<int> Q;
	Q.push(s);
	while(!Q.empty())
	{
		int u = Q.front();
		Q.pop();
                //if(vis[u]) continue;  如果把这句话放这就会超时drl...
                //vis[u] = true;
		for(int i = 1; i <= n; i++)
		{
			if(!vis[i] && conn[u][i])
			{
				dis[i] = dis[u] + 1;
				pre[s][i] = u;
				vis[i] = true;
				Q.push(i);
			}
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; i++)
	{
		if(dis[i] == INF) return -1;
		ans += dis[i];
	}
	return ans;
}
int main()
{
	while(~scanf("%d%d", &n, &m))
	{
		memset(conn, 0, sizeof(conn));
		memset(pre, -1, sizeof(pre));
		for(int i = 0; i < m; i++)
		{
			scanf("%d%d", &x[i], &y[i]);
			conn[x[i]][y[i]]++;
			conn[y[i]][x[i]]++;
		}
		bool flag = true;
		for(int i = 1; i <= n; i++)
		{
			sum[i] = bfs(i);
			if(sum[i] == -1) flag = false;
		}
		for(int i = 0; i < m; i++)
		{
			if(!flag)
			{
				printf("INF\n");
				continue;
			}
			int ans = 0;
			conn[x[i]][y[i]]--;
			conn[y[i]][x[i]]--;
			for(int j = 1; j <= n; j++)
			{
				if(pre[j][y[i]] != x[i] && pre[j][x[i]] != y[i])
				{
					ans += sum[j];
				}
				else
				{
					int tmp = bfs(j);
					if(tmp == -1)
					{
						ans = -1;
						break;
					}
					else ans += tmp;
				}
			}
			conn[x[i]][y[i]]++;
			conn[y[i]][x[i]]++;
			if(ans == -1) printf("INF\n");
			else printf("%d\n", ans);
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值