https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4829
Patricia is a excellent software developer, but as every brilliant person, she had some strange quirks.
One of those is that everything she does have to was in even quantities. Most often, Quirk does not
affect her, even though it could seem strange to others. Some Examples:every Day She had to eat an
even number of meals; During breakfast, she drinks both cups of coffee, eats, toasts and slices
of cheese; when she goes to the cinema she buys and the tickets (fortunately she always have a friend that
goes with her); She takes baths per day (or four, our six ...).
Some Other times, however, that's quirk makes the life of Patricia more difficult. For example, no
One wants to travel by car with her because if she have to pay toll, the number of tolls she pays have to
is an even number.
Patricia lives in a country where all roads is two-way and has exactly one toll each. She needs to
visit a client in a different city, and wants to calculate the minimum total value of tolls she had to pay
to go from her city to the client's city, obeying her strange quirk so she has a even number
of tolls.
Input
The input consists of several test cases. The first line of a test case contains integers C and V,
The total number of cities and the number of roads (2≤c≤104 and 0≤v≤50000). The Cities
is identified by integer numbers from 1 to C. Each road links to different cities, and there is at
Most one road between each pair of cities. Each of the next V lines contains three integers C1, C2
and G, indicating that the toll value of the road linking cities C1 and C2 are g (1≤C1, C2≤c and
1≤g≤104
). Patricia is currently in City 1 and the client's city is C.
Output
for all test case in the input your program must output exactly one line, containing exactly one
Integer, the minimum toll value for Patricia to go from City 1 to City C, paying an even number of tolls,
or, if that was not possible, the value '-1 '.
Sample Input
4 4
1 2 2
2 3 1
2 4
3 4 6
5 6
1 2 3
2 3 5
3 5 2
5 1 8
2 4 1
4 5 4
Sample Output
A
-1
Test instructions: The number of edges required to output the shortest path from 1 to C is even, and output-1 if there are no even numbers.
1 /*2 Dijkstra + Priority Queue Optimization3 Odd edge + one edge = even edge D number assemble odd side4 even edge + one edge = odd edge D number assemble even edge5 optimized for each other, if point C is in the D array (with even-numbered edges) as INF (not updated), it cannot be reached6 Otherwise it can be achieved and is the shortest7 */8#include <cstdio>9#include <algorithm>Ten#include <iostream> One#include <cstring> A#include <string> -#include <cmath> -#include <queue> the#include <vector> - using namespacestd; - #defineMAXN 100010 - Const intinf=1000000000; + structNode - { + intw,next,to; A}edge[maxn*5]; at structnode - { - intx,d; - node () {} -NodeintAintb) {x=a;d=b;} - BOOL operator< (ConstNode &a)Const in { - if(D==A.D)returnx<a.x; to Else returnD>A.D; + } - }; the * intHEAD[MAXN],TOT,V,E,D[MAXN],D[MAXN]; $ Panax Notoginseng voidAddintUintVintCost ) - { theedge[tot].to=v; +edge[tot].w=Cost ; Aedge[tot].next=Head[u]; thehead[u]=tot++; + } - $ voidDijkstra () $ { -Priority_queue<node>que; - while(!que.empty ()) Que.pop (); the for(intI=1; i<=v;i++){ -d[i]=d[i]=inf;Wuyi } thed[1]=0; -Que.push (Node (1,0)); Wu while(!Que.empty ()) { -Node a=que.top (); Que.pop (); About inttop=a.x; $ for(intk=head[top];~k;k=Edge[k].next) { - intCost =EDGE[K].W; - intv =edge[k].to; - if(D[top] + cost <D[v]) { AD[V] = D[top] +Cost ; + Que.push (Node (v,d[v)); the } - if(D[top] + cost <D[v]) { $D[V] = D[top] +Cost ; the Que.push (Node (v,d[v)); the } the } the } - } in the intMain () the { About while(~SCANF ("%d%d",&v,&E)) { the intu,v,w; thetot=0; thememset (head,-1,sizeof(head)); + for(intI=1; i<=e;i++){ -scanf"%d%d%d",&u,&v,&W); the Add (u,v,w);Bayi Add (v,u,w); the } the Dijkstra (); - Long Longans; - if(d[v]==inf) { theans=-1; the } the Elseans=D[v]; theprintf"%d\n", ans); - } the return 0; the}
2016-06-02
UVA 12950:even Obsession (Shortest way Dijkstra)