hdu 1595 find the longest of the shortest

来源:互联网 发布:linux 下的awk gensub 编辑:程序博客网 时间:2024/06/05 00:54
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 61 2 41 3 32 3 12 4 42 5 74 5 16 71 2 12 3 43 4 44 6 41 5 52 5 25 6 55 71 2 81 4 102 3 92 4 102 5 13 4 73 5 10
Sample Output
111327


这道题的题意是说,Marica在1号城市,她发现在n号城市的Mirko有了新女友,很生气,决定去Mirko的城市找他的新女友。从1号城市到n号城市有很多条路径,每条路都有走这条路所花费的时间,Marica想尽可能早的到达n城市。但是呢,在去1-n的路上,这里有且仅有一条路是在修,不能走。Mirko当然希望绕过这条路后Mirko到达他所在的城市用的时间越多越好,而Marica只想尽快到,所以即使绕过这条路,她也要走其他花费时间最少的路。也就是说,Marica一定会走最短的路。问,现在假如让Mirko选择一条路被破坏掉,那么Marica能到达n城市最坏情况下的花费时间是多少。


其实意思就是说,Mirko每选择一条路被破坏掉,Marica相应的就会走在没有这条路的情况下,最短的路。那么就问,Mirko要怎样选择才能使Marica走的最短路在所有的最短路中是最长的,输出Marica走的最短路路程是多少。


如果要想Marica走最长的最短路,那么一定要选择正常情况下,从1-n的最短路所经过的街道,选择这些街道将它破坏。因为如果不选最短路上的街道,那么Marica就可以走正常情况下的最短路,那么用的时间就会很少。所以一定要破坏掉最短路上的街道。

#include<iostream>#include<string>#include<cstring>#include<algorithm>#include<cstdio>#include<cstdlib>#define N 1010#define INF 0x3f3f3f3fusing namespace std;int e[N][N],b[N],dis[N],path[N],Min_path[N],k,Min,m,n;void dijkstra(int flag){    for(int i=1; i<=n; i++)    {        dis[i]=e[1][i];        b[i]=0;    }    b[1]=1,dis[1]=0;    int u,v;    for(int i=1; i<n; i++)    {        Min=INF;        for(int j=1; j<=n; j++)        {            if(b[j]==0&&dis[j]<Min)            {                Min=dis[j];                u=j;            }        }        b[u]=1;        for(v=1; v<=n; v++)        {            if(dis[v]>dis[u]+e[u][v]&&b[v]==0&&e[u][v]<INF)            {                dis[v]=dis[u]+e[u][v];                if(flag)//只有在第一次求最短路的时候才能改变path数组,不然每次跑dij都更新的话,path里存的最短路就会不一样                    path[v]=u;            }        }    }    if(flag)    {        int j=n;        k=0;        while(path[j]!=-1)//求正常情况下的最短路,存储到数组里        {            Min_path[k++]=j;            j=path[j];        }        Min_path[k++]=j;        Min_path[k++]=1;    }}int main(){    int t1,t2,t3;    while(cin>>n>>m)    {        memset(e,INF,sizeof(e));        for(int i=1; i<=m; i++)        {            scanf("%d%d%d",&t1,&t2,&t3);            e[t1][t2]=e[t2][t1]=t3;        }        memset(path,-1,sizeof(path));        dijkstra(1);        int ans=0,num;        for(int i=0; i<k-1; i++)//每次选择最短路中的一条边进行破坏        {            int tx=Min_path[i];            int ty=Min_path[i+1];            num=e[tx][ty];            e[tx][ty]=e[ty][tx]=INF;            dijkstra(0);            ans=max(ans,dis[n]);//求最短路中的最大值            e[tx][ty]=e[ty][tx]=num;//求完最短路都要把破坏的边再变回来        }        printf("%d\n",ans);    }    return 0;}



原创粉丝点击