HDU1595 find the longest of the shortest
2014-07-13 14:59
295 查看
find the longest of the shortest
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1560 Accepted Submission(s): 557
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.
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.
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
HDU 2007-Spring Programming Contest - Warm Up (1)
题意:找出减去一条边之后的最短路中的最长路。。
分析:题目就是要求我们去掉一条边,然后求出去掉这条边的图的最短路,去边可以有很多种,求出这很多种里的最短路最大的那个,我们可以先让完整的图跑一遍最短路,然后去掉完整的图上的最短路所经过的边,去掉之后肯定不会小于最短路,所以只要枚举去掉完整图上最短路过的边,然后找出这些里的最大就好了。这题我用没有优化过的spfa跑一直超时,后来用dijkstra过了。。。。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=1010; const int INF=1<<30; int w[MAXN][MAXN],dis[MAXN],pre[MAXN]; bool vis[MAXN]; int n; void dijkstra(int s,int flag) { int i; memset(vis,0,sizeof(vis)); fill(dis,dis+n+1,INF); dis[s]=0; for(i=1;i<=n;i++) { int x,y,m=INF; for(y=1;y<=n;y++) if(!vis[y]&&dis[y]<m) m=dis[x=y]; vis[x]=1; for(y=1;y<=n;y++) { if(dis[y]>dis[x]+w[x][y]) { dis[y]=dis[x]+w[x][y]; if(flag) pre[y]=x; } } } } int main() { int m,i,j,a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(i==j) w[i][j]=0; else w[i][j]=INF; while(m--) { scanf("%d%d%d",&a,&b,&c); w[a][b]=w[b][a]=c; } dijkstra(1,1); int ans=dis ; for(i=n;i!=1;i=pre[i]) { int temp=w[pre[i]][i]; w[pre[i]][i]=w[i][pre[i]]=INF; dijkstra(1,0); if(dis !=INF) ans=max(ans,dis ); w[pre[i]][i]=w[i][pre[i]]=temp; } printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU1595---find the longest of the shortest
- hdu1595 find the longest of the shortest(Dijkstra)
- HDU1595:find the longest of the shortest(spfa+记录路径)
- hdu1595 find the longest of the shortest(dijkstra+枚举)
- hdu1595(spfa) find the longest of the shortest
- hdu1595 find the longest of the shortest(Dijkstra)
- HDU1595:find the longest of the shortest(Dijkstra)
- HDU1595:find the longest of the shortest(Dijkstra)
- hdu1595 find the longest of the shortest (dijkstra+记录路径)
- HDU1595 find the longest of the shortest
- hdu1595 find the longest of the shortest
- HDU1595 - find the longest of the shortest - 最短路变形+暴力
- HDU1595 find the longest of the shortest(最短路)
- hdu1595-find the longest of the shortest
- find the longest of the shortest-spfa删边
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- hdu 1595 find the longest of the shortest (
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest(Dijkstra)