hdu 2433 Travel 最短路 dijkstra算法。

50 篇文章 0 订阅
19 篇文章 0 订阅

Travel

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


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
 
本来不打算写这篇博客的代码的。
因为我的代码根本就是错的,
我是照这篇博客写的:http://www.cppblog.com/luxiuyuan/archive/2012/03/09/167494.aspx
我在看这篇博客的时候,,就对博主的算法感到怀疑。但是他的代码可以A掉,
后来看了discuss
有一组数据:
4 4 
1 2
2 3
3 4 
2 4

答案:
INF
20
18
20
很多代码都是过不了遮住测试数据的不掉的。
对此,我感觉务必绝望啊~~
代码:
#include <stdio.h>
#include <string.h>
#define MAX 150
#define INF 1000000000
struct Point{
	int x, y ;
}p[MAX*30] ;
int c[MAX][MAX];
int graph[MAX][MAX] , sum[MAX];
int dis[MAX] ;
bool visited[MAX] ;
int dijkstra(int n,int s)
{
	memset(visited,false,sizeof(visited)) ;
	for(int i = 1 ; i <= n ; ++i)
	{
		dis[i] = graph[s][i] ; 
	}
	dis[s] = 0 ;
	visited[s] = true ;
	int sumt = 0 ;
	for(int i = 1 ; i < n ; ++i)
	{
		int index = -1 , min = INF ;
		for(int j = 1 ; j <= n ; ++j)
		{
			if(!visited[j] && min>dis[j])
			{
				index = j ;
				min = dis[j] ;
			}
		}
		if(index == -1)
		{
			return INF ;
		}	
		sumt += min ;
		visited[index] = true ;
		for(int j = 1 ; j <= n ; ++j)
		{
			if(!visited[j] && dis[j]>dis[index]+graph[index][j])
			{
				dis[j] = dis[index] + graph[index][j] ;
			}
		}
	}
	return sumt ;
}

int main()
{
	int n , m ;
	while(~scanf("%d%d",&n,&m))
	{
		memset(c,0,sizeof(c)) ;
		for(int i = 0 ; i < MAX ; ++i)
		{
			for(int j = 0 ; j < MAX ; ++j)
			{
				graph[i][j] = INF ;
			}
		}
		for(int i = 0 ; i < m ; ++i)
		{
			int x , y ;
			scanf("%d%d",&x,&y) ; 
			p[i].x = x , p[i].y = y ;
			c[x][y]++ ,c[y][x]++ ;
			graph[x][y] = graph[y][x] = 1 ;
		}
		bool flag = true ;
		int ans = 0 ;
		for(int i = 1 ; i <= n ; ++i)
		{
			if(flag)
			{
				sum[i] = dijkstra(n,i) ;
				if(sum[i] == INF)
				{
					flag = false ;
				}
				ans += sum[i] ;
			}
			
		}
		for(int i = 0 ; i < m ; ++i)
		{
			if(!flag)
			{
				puts("INF") ;
			}
			else
			{
				if(c[p[i].x][p[i].y]>1)
				{
					printf("%d\n",ans) ;
				}
				else
				{
					graph[p[i].x][p[i].y] = graph[p[i].y][p[i].x] = INF ;
					int sumx = dijkstra(n,p[i].x) ;
					int sumy = dijkstra(n,p[i].y) ;
					if(sumx == INF || sumy == INF)
					{
						puts("INF") ;
					}
					else
					{
						printf("%d\n",ans+sumx+sumy-sum[p[i].x]-sum[p[i].y]) ;
					}
					graph[p[i].x][p[i].y] = graph[p[i].y][p[i].x] = 1 ;
				}
			}
		}
	}
	return 0 ;
}

与君共勉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值