the recording path 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.
Mallika was very angry with Mirko gas, because Mirko found a new girlfriend, she wanted revenge. Because she does not live in the same city, she began to prepare for a long trip. We need to know each go one way from one city to another city how many minutes. Mirko overheard in the car there is a road being repaired, but was blocked, but do not know specifically which way. Regardless of which way is closed, Marica from city to city are likely to Mirko. Mallika will take the road is not blocked, she will take the shortest route. Mirko wants to know, in the worst case, how long it takes her to get to his city, so he can ensure that his girlfriend long enough out of town. Write a program that helps Mirko find out what is the longest period of time, in minutes, Mallika by the shortest route, not through clogged roads to reach his city. The input in each case, a first line of two numbers, N and M, separated by a space, namely the number of road between the number of towns and cities and towns. 1≤N≤1000,1≤M≤N * (N-1) / 2. These cities marked with numbers from 1 to N, Mirko 1 located in the city, the city is located in Marica N. The next three figures is M rows A, B and V, separated by commas. 1≤A, B≤N, 1≤V≤1000. These figures mean that there is a two-way road between cities A and B, it may be in the V-minute crossing. Output in the first line of the output file, in minutes, the maximum time is written, Marica may need to reach Mirko.

Meaning of the questions: Shortest enumeration Erase seeking to delete the shortest side of each post, find the longest and shortest piece from these, the
problem solution: We just need to find the piece when all roads are open shortest path and followed by deleting every edge you can on this path, because if we remove the side not the shortest way, every time we still find this one to find the shortest path


#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);
	}
}

Published 12 original articles · won praise 1 · views 199

Guess you like

Origin blog.csdn.net/csx_zzh/article/details/105041166