HDU 3986删边最短路

Harry Potter and the Final Battle

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1991    Accepted Submission(s): 543


Problem Description
The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.
 

Input
First line, case number t (t<=20). 
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.
 

Output
Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.
 

Sample Input
3 4 4 1 2 5 2 4 10 1 3 3 3 4 8 3 2 1 2 5 2 3 10 2 2 1 2 1 1 2 2
 

Sample Output
15 -1 2

 

题意与hdu  1595一样,不过有重边,略恶心,先将边排序,然后再加边。找出原始图最短路上的边,枚举求最短路。

代码:

 

#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[100*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[50*maxn],ss[50*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;
                int flag=1;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                        if(flag&&((edge[i].st==ss[id].st&&edge[i].to==ss[id].ed)||(edge[i].st==ss[id].ed&&edge[i].to==ss[id].st))){flag=0;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]));
                        }
                }
        }
}
bool cmp(PP a,PP b)
{
        if(a.st!=b.st)return a.st<b.st;
        if(a.ed!=b.ed)return a.ed<b.ed;
        return a.val>b.val;
}
int main()
{
        int i,j,k,T;
        scanf("%d",&T);
        while(T--)
        {
                scanf("%d%d",&n,&m);
                for(i=0;i<m;i++)
                {
                        scanf("%d%d%d",&pp[i].st,&pp[i].ed,&pp[i].val);
                        if(pp[i].st>pp[i].ed)swap(pp[i].st,pp[i].ed);
                }
                memset(head,-1,sizeof(head));tol=0;
                memset(pre,0,sizeof(pre)); sort(pp,pp+m,cmp);
                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);
                }
               // cout<<"!!!!!!!!!"<<endl;
               
// for(i=head[1];i!=-1;i=edge[i].next)cout<<edge[i].to<<" "<<edge[i].val<<endl;

               
// cout<<tol<<endl;
                for(j=0;j<=n;j++)dist[j]=inf;dist[1]=0;
                fun(1);
                int ans=dist[n];
                if(ans>=inf)
                {
                        puts("-1");continue;
                }
                int cnt=0;
                for(i=n;i!=1;i=pre[i])
                {
                      ss[cnt].st=pre[i];
                      ss[cnt++].ed=i;
                }
               // cout<<ss[0].st<<" "<<ss[0].ed<<endl;
              
//  cout<<cnt<<endl;
                        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(ans<dist[n])ans=dist[n];
                        }
                        if(ans>=inf)ans=-1;
                        printf("%d\n",ans);
        }
        return 0;
}

 

 

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