Codevs 1269 Hungarian Games

Source: Internet
Author: User

Codevs 1269 Hungarian Games

Title Description Description

Welcome to the Hungary games! The streets of Budapest form a twisted network of one-way streets.

Welcome to the Hungarian game! The streets of Budapest (the Hungarian capital) Form a curved one-way network.

You are been forced to join a race as part of a "Reality TV" show where do race through these streets, starting at the S Z′echenyi Thermal Bath (s for short) and ending at the Tomb of G¨ul Baba (T for short).

You are forced to participate in a race as part of a TV show where you need to cross these streets, starting with S and ending with T.

Naturally, want to complete the race as quickly as possible, because you'll get more promo-tional contracts the bet ter you perform.

Naturally, you want to finish the game as soon as possible, because the better you finish the game, the more business promotion contracts you'll get.

However, there is a catch:any person who was smart enough to take a shortest s-t route would be thrown into the P′alv¨olgyi Cave system and kept as a national treasure. You would like to avoid this fate, but still is as fast as possible. Write a program, that computes a strictly-second-shortest s-t route.

However, one need to understand is that if someone is too smart to find the shortest route from S to T, then he is thrown into the national supreme human protection system as a national treasure collection. You obviously want to avoid this kind of thing, but also think the sooner the better. Write a program to calculate a strict short route from S to T.

Sometimes the Strictly-second-shortest route visits some nodes more than once; See Sample Input 2 for an example.

Sometimes, a strictly short route may access some nodes more than once. Example 2 is a sample.

Enter a description input Description

The. RST line would have the format N m, where n was the number of nodes in Budapest and M are the number of edges. The nodes is,..., N; Node 1 represents s; Node N represents T. Then there is M lines of the form a B L, indicating a one-way street from A to B of length L. You can assume that A! = B on these lines, and that the ordered pairs (a, b) is distinct.

The first line contains two integers n and m,n represents the number of nodes in Budapest, and M represents the number of edges. The node number is from 1 to N. 1 represents the starting point S,n represents the end point T. The next M-line is three integers a b l, which represents a one-way path from A to B with a length of L. You may think that a is not equal to B, and there will be no duplicate (A/b) pair.

outputs description output Description

Output the length of a strictly-second-shortest route from S to T. If there is less than and possible lengths for routes from S to T, output? 1.

The length of the strict secondary short circuit from S to T is output. If the path from S to T is less than 2, output-1.

sample input to sample

Sample Input 1:

4 6

1 2 5

1 3 5

2 3 1

2 4 5

3 4 5

1 4 13

Sample Input 2:

2 2

1 2 1

2 1 1

Sample output Sample outputs

Sample Output 1:

11

Sample Output 2:

3

data size & Hint

For example 1:

There is, shortest routes of length 10 (1→2→4,1→3→4) and the Strictly-second-shortest route is 1→2→3→4 With length 11.

For example 2:

The shortest route is 1→2 of length 1, and the Strictly-second route is 1→2→1→2 of length 3.

Idea: Secondary short-circuit template note to determine if there is a short circuit in the diagram

#include <cstring>#include<cstdio>#include<queue>using namespacestd;#defineLoop (i, n) for (int i = 1; I <= n; ++i)Const intMax_node =50005;Const intMax_edge =100005<<1;Const intINF =0x3f3f3f3f;structNode;structEdge;structNode {intId, Dist, Dist2; BOOLInq; Edge*Head;} _nodes[max_node],*start, *Target;int_vcount;structEdge {intWeight; Node*from, *to ; Edge*Next; Edge () {} Edge (Node* from, Node *to, Edge *next,intweight): from ( from), to, next (next), Weight (Weight) {}}*_edges[max_edge];int_ecount;voidInit (intVcount) {memset (_nodes,0,sizeof_nodes); _vcount=Vcount; _ecount=0; Start=1+_nodes; Target= Vcount +_nodes;}voidAddedge (Node * from, Node *to,intweight) {Edge*e = _edges[++_ecount] =NewEdge ( from, To, from-Head, weight); E->from->head =e;}voidBuild (intUidintVId,intweight) {Node*u = UId + _nodes, *v = VId +_nodes; U->id =uId; V->id =vId; Addedge (U, V, weight);}voidSPFA () {LOOP (i, _vcount) _nodes[i]. Dist= _nodes[i]. Dist2 =INF; StaticQueue<node*>Q; Start->dist =0; Start->dist2 =INF; Start->inq =true;    Q.push (Start);  while(!Q.empty ()) {Node*u =Q.front ();        Q.pop (); U->inq =false;  for(Edge *e = u->head; e; e = e->Next) {            BOOLRelaxok =false; if(U->dist + E->weight < e->to->Dist) {e->to->dist2 = e->to->Dist; E->to->dist = u->dist + e->Weight; Relaxok=true; }            Else if(U->dist + e->weight > e->to->dist && u->dist + e->weight < e->to->Dist2) {e->to->dist2 = u->dist + e->Weight; Relaxok=true; }            if(U->dist2 + E->weight < e->to->Dist2) {e->to->dist2 = U->dist2 + e->Weight; Relaxok=true; }            if(Relaxok &&!e->to->Inq) {e->to->inq =true; Q.push (e-to ); }        }    }}intMain () {intTotnode, Totedge; intuId, vId, weight; scanf ("%d%d", &totnode, &Totedge);    Init (Totnode); LOOP (i, Totedge) {scanf ("%d%d%d", &uid, &vid, &weight);    Build (UId, vId, weight);    } SPFA (); if(Target->dist2 = = INF) printf ("-1\n"); Elseprintf"%d\n", target->Dist2); return 0;}

Codevs 1269 Hungarian Games

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.