您的位置:首页 > 其它

hdu 1595 find the longest of the shortest (枚举+最短路)

2014-08-17 20:01 477 查看

find the longest of the shortest

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

11 13 27
注意细节

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
#define INF 9999999
int N, M;
#define maxn  1010
int mp[maxn][maxn];
int vis[maxn];
int u, v, w;
int dis[maxn];
int pre[maxn];
bool judge;
int dijkstra(){
memset(vis, 0, sizeof(vis));
int pos = 1;
for(int i = 1; i <= N; i++) dis[i] = INF;
dis[1] = 0;
for(int i = 1; i <= N; i++){
int min = INF;
for(int j = 1; j <= N; j++){
if(!vis[j] && dis[j] < min){
min = dis[j]; pos = j;
}
}
vis[pos] = 1;
for(int j = 1; j <= N; j++){
if(!vis[j] && dis[j] > mp[pos][j] + dis[pos]){
dis[j] = mp[pos][j] + dis[pos];
if(judge) {pre[j] = pos;
}
}
}

}
return dis
;
}
int main(){
while(~scanf("%d%d", &N, &M)){

for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(i != j) mp[i][j] = INF;
else mp[i][j] = 0;
}
}
int a, b, c;
memset(pre, 0, sizeof(pre));
for(int i = 1; i <= M; i++){
scanf("%d%d%d", &a, &b, &c);
if(mp[a][b] > c) mp[a][b] = mp[b][a] = c;
}
judge = true;
int ans = dijkstra();
judge = false;
for(int i = N; i != 1; i = pre[i]){
int temp = mp[i][pre[i]];
mp[i][pre[i]] = mp[pre[i]][i] = INF;
if(dijkstra() > ans) ans = dijkstra();
mp[i][pre[i]] = mp[pre[i]][i] = temp;

}
printf("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐