题目:
find the longest of the shortest
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3341 Accepted Submission(s): 1249
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.
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.
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
Author
ailyanlu
Source
Recommend
先说题意,有n个点m条边,然后让你把这些边每一个都删一次,求每一个的最短路,然后在这些最短路里面求最大的值.
如果直接求的话,会时间很大,我们只需要对这些边求一下最短路,把路径记录下来,然后依次把路径删掉,然后在里面求最大的就好,甩Dijkstra比较方便
题目很简单。。但是在写这个题的时候,发现一个超级bug,定义数组的顺序把map数组和dis数组换一下会导致内存溢出,本地根本过不了样例,但是能AC,换回来本地就正常了,最后找到原因是非法访问了内存map[-1][1],所以在最后寻路的时候,加一句判断,把负数过滤掉
代码:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 1000007
#define N 1010
#define M 12357
#define ll long long
using namespace std;
int n,m,flag;
int map[N][N];
int dis[N];
int vis[N];
int pre[N];
void dijkstra(int x)
{
int k,minn;
for(int i=1; i<=n; i++)
{
dis[i]=inf;
vis[i]=0;
}
dis[x]=0;
for(int i=1; i<=n-1; i++)
{
k=0;
minn=inf;
for(int j=1; j<=n; j++)
if(vis[j]==0&&dis[j]<minn)
{
minn=dis[j];
k=j;
}
vis[k]=1;
for(int j=1; j<=n; j++)
if(vis[j]==0&&dis[k]+map[k][j]<dis[j])
{
dis[j]=dis[k]+map[k][j];//如果找到了通路就加上
if(!flag)pre[j]=k;
}
}
}
int main()
{
int a,b,c;
while(~scanf("%d%d",&n,&m))
{
flag=0;
mem(map,0);
mem(pre,-1);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
map[i][j]=(i==j)?0:inf;
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])map[a][b]=map[b][a]=c;
}
dijkstra(1);
flag=1;
int temp,maxx=dis[n];
for(int i=n; i!=-1; i=pre[i])
{
int j=pre[i];
if(i<0||j<0)continue;
temp=map[j][i];
map[j][i]=map[i][j]=inf;
dijkstra(1);
map[j][i]=map[i][j]=temp;
maxx=max(maxx,dis[n]);
}
printf("%d\n",maxx);
}
return 0;
}