hdu1595—find the longest of the shortest(dijkstra+枚举)

本文介绍了一个经典的图论问题,即在给定的图中找到从起点到终点的最长最短路径,通过迪杰斯特拉算法求解并在最短路径上进行边的枚举。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595

find the longest of the shortest

Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3297    Accepted Submission(s): 1217


Problem Description
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
 

Input
Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1, and Marica in city N.
In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
 

Output
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
 

Sample Input
  
5 6 1 2 4 1 3 3 2 3 1 2 4 4 2 5 7 4 5 1 6 7 1 2 1 2 3 4 3 4 4 4 6 4 1 5 5 2 5 2 5 6 5 5 7 1 2 8 1 4 10 2 3 9 2 4 10 2 5 1 3 4 7 3 5 10
 

Sample Output
  
11 13 27

题目大意:给你一张图,任意去掉一条路径,求图中从s点到e点最短路径的最大值(s点总能到e点)

解题思路:枚举所有的边,去掉后求一次最短路。直接这样枚举肯定超时,先再原图上求次最短路,枚举最短路上的边即可,因为去掉非最短路上的边后求得的值还是原图上的最短路不变。(感觉最短路上的边有1000的话,还是会超时)


#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <iostream>  
#include <queue>
#include <set>
#include <string>
#include <stack>
#include <algorithm>
#include <map>
using namespace std;  
typedef long long ull;
const int N = 1009;
const int M = 10800;
const int INF = 0x3fffffff;
const double Pi = acos(-1.0);

int mp[N][N],dist[N],vis[N],path[N];
void dijkstra( int s , int n , int flag )
{
	fill( dist , dist+N , INF );
	fill( vis , vis+N , 0 );
	dist[s] = 0; vis[s] = 1;
	path[s] = s;
	int i,j,pre = s;
	for( i = 0 ; i < n-1 ; ++i ){
		int rec = INF;
		for( j = 1 ; j <= n ; ++j ){
			if( !vis[j] && dist[j] > dist[pre]+mp[pre][j] ){
				dist[j] = dist[pre]+mp[pre][j];
				//记录路径,最短路的前驱结点
				if( flag )	path[j] = pre;
			}
		}
		for( j = 1 ; j <= n ; ++j ){
			if( !vis[j] && rec > dist[j] ){
				rec = dist[j];
				pre = j;
			}
		}
		vis[pre] = 1;
	}
}

void init()
{
	fill( path , path+N , 0 );
	for( int i = 0 ; i < N ; ++i ){
		for( int j = 0 ; j < N ; ++j ){
			mp[i][j] = INF;
			mp[i][i] = 0;
		}
	}
}

int main()
{
	int n,m;
	while( ~scanf("%d%d",&n,&m) ){
		init();
		if( n == 0 && m == 0 ) break;
		int a,b,c;
		for( int i = 0 ; i < m ; ++i ){
			scanf("%d%d%d",&a,&b,&c);
			mp[a][b] = mp[b][a] = c;
		}
		dijkstra(1,n,1);
		int ans = -INF;
		for( int i = n ; i != 1 ; i = path[i] ){
			int w = mp[i][path[i]];
			mp[i][path[i]] = mp[path[i]][i] = INF;
			dijkstra(1,n,0);
			mp[i][path[i]] = mp[path[i]][i] = w;
			ans = max(ans,dist[n]);
		}
		printf("%d\n",ans);
	}
	return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值