dijkstra+记录路径

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.
InputEach 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.OutputIn the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
玛丽卡很生米尔科的气,因为米尔科找到了一个新的女朋友,她想报仇。因为她不住在同一个城市,她开始为长途旅行做准备。我们知道从一个城市到另一个城市每走一条路需要多少分钟。              米尔科无意中听到车里有一条路正在维修,而且被堵住了,但不知道具体是哪条路。不管哪条路是封闭的,从马里卡市到米尔科市都有可能。              玛丽卡只会走不堵塞的路,她会走最短的路。米尔科想知道在最坏的情况下,她要花多长时间才能到达他的城市,这样他才能确保他的女朋友足够长的时间不在城里。写一个程序,帮助米尔科找出什么是最长的时间,在几分钟内,玛丽卡可以通过最短的路线,通过不堵塞的道路到达他的城市。              输入              每种情况下,第一行有两个数字,N和M,用一个空格隔开,分别是城镇数量和城镇之间的道路数量。1≤N≤1000,1≤M≤N*(N-1)/2。这些城市标有从1到N的数字,米尔科位于城市1,马里卡位于城市N。              接下来的M行是三个数字A、B和V,用逗号隔开。1≤A,B≤N,1≤V≤1000。这些数字意味着在A和B城市之间有一条双向道路,在V分钟内可以横穿。              输出              在输出文件的第一行中,以分钟为单位写入最长时间,Marica可能需要到达Mirko。

题意:最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条,
题解:我们只需要找出所有路都通畅时的那条最短路,并且依次删去这条路径上的每条边即可,因为如果我们删除的不是这条最短路上的边,我们每次找最短路时依旧会找到这一条


#include "stdio.h"
#include "iostream"
#include "string.h"
#include "math.h"
#include "algorithm"
using namespace std;
#define inf 0x3f3f3f3f
int n,m;
int dp[1002][1002],dis[1002],vis[1002];
int pre[1002];
int djk(int x)
{
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)
	dis[i]=inf;
	dis[1]=0;
	for(int i=1;i<=n;i++)
	{
		int minn=inf,u;
		for(int j=1;j<=n;j++)
		{
			if(!vis[j]&&minn>dis[j])
			{
				minn=dis[j];
				u=j;
			}
		}
		vis[u]=1;
		for(int j=1;j<=n;j++)
		{
			if(!vis[j]&&dis[j]>dis[u]+dp[u][j])
			{
				dis[j]=dis[u]+dp[u][j];
				if(x)
				pre[j]=u;
			}
		}
	}
	return dis[n];
}
int main()
{
	while(cin>>n>>m&&(n+m))
	{
		memset(pre,0,sizeof(pre));
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			dp[i][j]=inf;
			dp[i][i]=0;
		}
		for(int i=0;i<m;i++)
		{
			int a,b,c;
			cin>>a>>b>>c;
			if(dp[a][b]>c)
			dp[a][b]=dp[b][a]=c;
		}
		int ans=djk(1);
		for(int i=n;i!=1;i=pre[i])
		{
			int t=dp[i][pre[i]];
			dp[i][pre[i]]=dp[pre[i]][i]=inf;
			ans=max(ans,djk(0));
			dp[i][pre[i]]=dp[pre[i]][i]=t;
		}
		printf("%d\n",ans);
	}
}

发布了12 篇原创文章 · 获赞 1 · 访问量 199

猜你喜欢

转载自blog.csdn.net/csx_zzh/article/details/105041166