find the longest of the shortest
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2759 Accepted Submission(s): 995
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 大体题意: 这个题又反应了自己的英语水平。 题意: 男主人要找女朋友,男主人会选择最短路,但它会在最短路的某一条边卡住修车,哪一条边不知道,求得在卡住的情况下,最长路是多少 思路: 直接利用一次Dijkstra算法求出最短边,然后把路径记录下来,枚举路径的每一条边,删除后,再次调用dijkstra算法,更新答案即可!#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; const int maxn = 1000 + 10; const int INF = 1000000 + 10; int g[maxn][maxn]; bool vis[maxn]; int d[maxn]; int pp[maxn]; int p[maxn]; int n,m; void dijkstra(int s){ memset(p,0,sizeof p); memset(vis,0,sizeof vis); for (int i = 1; i <= n; ++i)d[i] = (i == s ? 0 : INF); for (int i = 1; i <= n; ++i){ int x,m = INF; for (int y = 1; y <= n; ++y) if (!vis[y] && d[y] <= m)m = d[x=y]; vis[x] = 1; for (int y = 1; y <= n; ++y){ if (d[y] > d[x] + g[x][y]){ d[y] = d[x] + g[x][y]; p[y] = x; } } } } int main(){ while(scanf("%d%d",&n,&m) == 2 && (n||m)){ for (int i = 0; i < n+2; ++i) for (int j = 0; j < n+2; ++j){ g[i][j] = INF; } for (int i = 0; i < m; ++i){ int u,v,w; scanf("%d%d%d",&u,&v,&w); g[u][v] = min(g[u][v],w); g[v][u] = g[u][v]; } dijkstra(1); int ans = d[n]; int cnt = 0; for (int i = n; i != 0; i = p[i])pp[cnt++] = i; // for (int i = 0; i < cnt; ++i)printf("%d ",pp[i]); // printf("\n"); for (int i = 0; i < cnt-1; ++i){ int s = pp[i],e=pp[i+1]; int t = g[s][e]; g[s][e] = g[e][s] = INF; dijkstra(1); if (d[n] != INF) ans = max(ans,d[n]); g[s][e] = g[e][s] = t; } printf("%d\n",ans); } return 0; }