hdu3986

来源:互联网 发布:windows组件中没有ie 编辑:程序博客网 时间:2024/05/01 13:17

Harry Potter and the Final Battle

Time Limit : 5000/3000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 4
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
3441 2 52 4 101 3 33 4 8321 2 52 3 10221 2 11 2 2
 

Sample Output
15-12
 

Author
tender@WHU
 

Source
2011 Multi-University Training Contest 15 - Host by WHU
 
刚开始wa到死啊~~~~数组开小了~~
后来tle额~~~
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <vector>#define inf 999999999using namespace std;int d[1005];int p[1005];int vis[1005];int n,m;struct node{    int u,v,w;};node e[100005];int first[1005];int next[100005];int inq[1005];int ee[100005];void spfa(int f){    int i;    for(i=0;i<=n;i++)        d[i]=inf;    d[1]=0;    memset(inq,0,sizeof(inq));    queue<int> q;    q.push(1);    while(!q.empty())    {        int x=q.front();        q.pop();        inq[x]=0;        int y;        for(y=first[x];y!=-1;y=next[y])        {            int v=e[y].v;            if(d[v]>d[x]+e[y].w)            {                d[v]=d[x]+e[y].w;                if(!inq[v])                {                    inq[v]=1;                    q.push(v);                }                if(f)                {                    p[v]=x;                    ee[v]=y;                }            }        }    }}inline void add_edge(int u,int v,int w,int k){    e[k].u=u;    e[k].v=v;    e[k].w=w;    next[k]=first[u];    first[u]=k;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int i;        scanf("%d%d",&n,&m);        memset(p,0,sizeof(p));        memset(first,-1,sizeof(first));        memset(next,-1,sizeof(next));        memset(ee,0,sizeof(ee));        int k=0;        for(i=0;i<m;i++)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            add_edge(u,v,w,k++);            add_edge(v,u,w,k++);        }        spfa(1);        int maxx=d[n];        if(d[n]==inf)        {            printf("-1\n");            //关键优化            continue;        }        for(i=n;i!=1;i=p[i])        {            int tt=e[ee[i]].w;            e[ee[i]].w=inf;            //printf("%d %d %d\n",i,p[i],e[j].w);            spfa(0);            if(d[n]>maxx)                maxx=d[n];            e[ee[i]].w=tt;        }        if(maxx>=inf)            printf("-1\n");        else            printf("%d\n",maxx);    }    return 0;}


原创粉丝点击