Hdu3986harry Potter and the Final Battle (SPFA book side)

Source: Internet
Author: User
Tags recode

Harry Potter and the Final BattleTime limit:5000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2666 Accepted Submission (s): 761


Problem DescriptionThe Final battle is coming. Now Harry Potter are located at the City 1, and Voldemort are located at City N. To make the world peace as soon as possible, of course, Harry Potter would choose the shortest road between City 1 and city N. But unfortunately, Voldemort are so powerful that he can choose to destroy any one of the existing roads as he wish, BU T he can only destroy one. Now given the roads between cities, you is to give the shortest time then Harry Potter can reach City N and begin the bat Tle in the worst case.

Inputfirst line, Case number T (t<=20).
Then for each Case:an integer n (2<=n<=1000) means the number of the city in the magical world, the cities is numbered from 1 to N. Then a 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 Singl E space. It means there is a bidirectional road between U and V with the cost of time W.There may multiple roads between, cities.

Outputeach 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[email protected]
Source2011 multi-university Training Contest 15-host by WHU problem solving: First use SPFA to find the shortest, and note the mark of the edge. Next is to enumerate an edge (note the edge) of the book, each time only one edge, when encountering a must edge can be directly output-1, because from 1 unreachable n. If there is always a way, then output the maximum cost of that road.
#include <stdio.h> #include <queue> #include <algorithm> #include <vector>using namespace std; const int N = 1005;const int inf = 999999999;struct edg{int v,d,id;}; Vector<edg>mapt[n];bool inq[n];int dis[n],frome[n],id[n],n;void Init () {for (int i=0;i<=n;i++) {MAPT [I].clear (); Id[i]=inf;    Frome[i]=i;    }}inline void spfa (int a,bool recode) {queue<int>q;    int s;    for (int i=1;i<=n;i++) Dis[i]=inf,inq[i]=false;    Inq[n]=true; dis[1]=0;    Q.push (1);        while (!q.empty ()) {S=q.front (); Q.pop ();        Inq[s]=false; for (int i=0;i<mapt[s].size (); i++) if (id[a]!=mapt[s][i].id| |            Recode) {int now=mapt[s][i].v;                if (DIS[NOW]&GT;DIS[S]+MAPT[S][I].D) {dis[now]=dis[s]+mapt[s][i].d;                if (Recode) frome[now]=s,id[now]=mapt[s][i].id;            if (!inq[now]) Inq[now]=true,q.push (now); }        }    }}int Main () {int m,a,b,ans,t;    EDG EDG;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        Init ();            while (m--) {scanf ("%d%d%d", &AMP;A,&AMP;B,&AMP;EDG.D);            Edg.id=m; Edg.v=b;            Mapt[a].push_back (EDG); Edg.v=a;        Mapt[b].push_back (EDG);        } if (n==1) {printf ("0\n"); continue;        } SPFA (0,true);        Ans=-1;        A=n;            while (Frome[a]!=a) {b=frome[a];            SPFA (A,false);            if (Dis[n]>ans&&dis[n]!=inf) ans=dis[n];            else if (dis[n]==inf) {ans=-1; break;        } a=b;    } printf ("%d\n", ans); }}


Hdu3986harry Potter and the Final Battle (SPFA book side)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.