hdu 1595删边最短路

find the longest of the shortest

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


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
 


题意:求删除一条边后,最短路中的最大值。

解题思路:首先求一次初始状态下的最短路,把最短路上的边标记出来,然后枚举最短路上的边进行删边,去最大值。不能直接按边数枚举,因为假设删除的不是最短路上的边,不影响最短路结果。

代码:

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<list>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int inf=1000000000;
const int maxn=3010;
int dist[maxn],head[maxn],tol,m,n,id,pre[maxn];
struct Edge
{
        int st,to,next,val;
}edge[1000*maxn];
void add(int u,int v,int w)
{
        edge[tol].st=u;
        edge[tol].to=v;
        edge[tol].next=head[u];
        edge[tol].val=w;
        head[u]=tol++;
}
struct Node
{
        int id,dist;
        Node(int a=0,int b=0):id(a),dist(b){}
        bool operator < (const Node &b) const
        {
                return dist>b.dist;
        }
};
struct PP
{
        int st,ed,val;
}pp[500*maxn],ss[500*maxn];
void fun(int st)
{
        int i,j,u,v;
        priority_queue<Node> q;
        q.push(Node(st,0));
        while(!q.empty())
        {
                Node ret=q.top();q.pop();
                u=ret.id;
                if(dist[u]<ret.dist)continue;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                        if(edge[i].st==ss[id].st&&edge[i].to==ss[id].ed)continue;
                        if(edge[i].st==ss[id].ed&&edge[i].to==ss[id].st)continue;
                        v=edge[i].to;
                        if(dist[v]>dist[u]+edge[i].val)
                        {
                                pre[v]=u;
                                dist[v]=dist[u]+edge[i].val;
                                q.push(Node(v,dist[v]));
                        }
                }
        }
}
int main()
{
        int i,j,k;
        while(~scanf("%d%d",&n,&m))
        {
                for(i=0;i<m;i++)scanf("%d%d%d",&pp[i].st,&pp[i].ed,&pp[i].val);
                memset(head,-1,sizeof(head));tol=0;
                memset(pre,0,sizeof(pre));
                for(i=0;i<m;i++)
                {
                        add(pp[i].st,pp[i].ed,pp[i].val);
                        add(pp[i].ed,pp[i].st,pp[i].val);
                }
                for(j=0;j<=n;j++)dist[j]=inf;dist[1]=0;
                fun(1);
                int ans=dist[n];
                int cnt=0;
                for(i=n;i!=1;i=pre[i])
                {
                      ss[cnt].st=pre[i];
                      ss[cnt++].ed=i;
                }
                        for(i=0;i<cnt;i++)
                        {
                             id=i;
                             for(j=0;j<=n;j++)dist[j]=inf;dist[1]=0;
                               fun(1);
                              // cout<<dist[n]<<endl;
                              if(dist[n]<inf&&ans<dist[n])ans=dist[n];
                        }
                        printf("%d\n",ans);
        }
        return 0;
}

 

posted @ 2013-09-16 15:54  线性无关  阅读(310)  评论(0编辑  收藏  举报