https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4829
Patricia is an excellent software developer, but, as every brilliant person, she has some strange quirks.
One of those is that everything she does has to be in even quantities. Most often that quirk does not
affect her, even though it may seem strange to others. Some examples: every day she has to eat an
even number of meals; during breakfast, she drinks two cups of coffee, eats two toasts and two slices
of cheese; when she goes to the cinema she buys two tickets (fortunately she always has a friend that
goes with her); she takes two baths per day (or four, our six...).
Some other times, however, that quirk makes the life of Patricia more difficult. For example, no
one wants to travel by car with her because if she has to pay toll, the number of tolls she pays has to
be an even number.
Patricia lives in a country where all roads are two-way and have 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 has to pay
to go from her city to the client’s city, obeying her strange quirk that she has to pay an even number
of tolls.
Input
The input consists of several test cases. The first line of a test case contains two integers C and V ,
the total number of cities and the number of roads (2 ≤ C ≤ 104 and 0 ≤ V ≤ 50000). The cities
are identified by integer numbers from 1 to C. Each road links two 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 is G (1 ≤ C1, C2 ≤ C and
1 ≤ G ≤ 104
). Patricia is currently in city 1 and the client’s city is C.
Output
For each 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 is not possible, the value ‘-1’.
Sample Input
4 4
1 2 2
2 3 1
2 4 10
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
12
-1
题意:要求输出的从1到C的最短路径的边数是偶数,如果无偶数则输出-1。
1 /* 2 Dijkstra + 优先队列优化 3 奇数边 + 一条边 = 偶数边 D数组装奇数边 4 偶数边 + 一条边 = 奇数边 d数组装偶数边 5 互相优化,若点C 在 d 数组(装偶数边)为INF(没被更新),则无法达到 6 否则可以达到并且是最短的 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <iostream> 11 #include <cstring> 12 #include <string> 13 #include <cmath> 14 #include <queue> 15 #include <vector> 16 using namespace std; 17 #define MAXN 100010 18 const int inf=1000000000; 19 struct Node 20 { 21 int w,next,to; 22 }edge[MAXN*5]; 23 struct node 24 { 25 int x,d; 26 node(){} 27 node(int a,int b){x=a;d=b;} 28 bool operator < (const node &a) const 29 { 30 if(d==a.d) return x<a.x; 31 else return d>a.d; 32 } 33 }; 34 35 int head[MAXN],tot,V,E,d[MAXN],D[MAXN]; 36 37 void add(int u,int v,int cost) 38 { 39 edge[tot].to=v; 40 edge[tot].w=cost; 41 edge[tot].next=head[u]; 42 head[u]=tot++; 43 } 44 45 void dijkstra() 46 { 47 priority_queue<node> que; 48 while(!que.empty()) que.pop(); 49 for(int i=1;i<=V;i++){ 50 D[i]=d[i]=inf; 51 } 52 d[1]=0; 53 que.push(node(1,0)); 54 while(!que.empty()){ 55 node a=que.top();que.pop(); 56 int top=a.x; 57 for(int k=head[top];~k;k=edge[k].next){ 58 int cost = edge[k].w; 59 int v = edge[k].to; 60 if( d[top] + cost < D[v] ){ 61 D[v] = d[top] + cost; 62 que.push(node(v,D[v])); 63 } 64 if( D[top] + cost < d[v] ){ 65 d[v] = D[top] + cost; 66 que.push(node(v,d[v])); 67 } 68 } 69 } 70 } 71 72 int main() 73 { 74 while(~scanf("%d%d",&V,&E)){ 75 int u,v,w; 76 tot=0; 77 memset(head,-1,sizeof(head)); 78 for(int i=1;i<=E;i++){ 79 scanf("%d%d%d",&u,&v,&w); 80 add(u,v,w); 81 add(v,u,w); 82 } 83 dijkstra(); 84 long long ans; 85 if(d[V]==inf){ 86 ans=-1; 87 } 88 else ans=d[V]; 89 printf("%d\n",ans); 90 } 91 return 0; 92 }
2016-06-02