UVA 12950 : Even Obsession(最短路Dijkstra)

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

时间: 2024-11-10 08:06:47

UVA 12950 : Even Obsession(最短路Dijkstra)的相关文章

uva 10801 - Lift Hopping(最短路Dijkstra)

1 /* 2 题目大意: 3 就是一幢大厦中有0-99的楼层, 然后有1-5个电梯!每个电梯有一定的上升或下降速度和楼层的停止的位置! 4 问从第0层楼到第k层最少经过多长时间到达! 5 6 思路:明显的Dijkstra ,在建图的时候u->v可能有多个电梯到达,取时间最少的当作路径的权值! 7 如果我们发现 d[i] > d[j] + map[j][i] + 60, 那么说明从第0层到达第 i 层的时间大于从第j层 8 转移到其他电梯然后到达第 i 层的时间,那么就更新d[i]的值! 9 1

训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - Dijkstra - 图论 - 训练指南 Airport Express UVA - 11374 题意 机场快线有经济线和商业线,现在分别给出经济线和商业线的的路线,现在只能坐一站商业线,其他坐经济线,问从起点到终点的最短用时是多少,还有

训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - 基础DP - Dijkstra - 图论 - 训练指南 Walk Through the Forest UVA - 10917 题意 Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比

POJ1062 Expensive dowry 【最短路dijkstra】

详细看:http://blog.csdn.net/lyy289065406/article/details/6645852 简单说一下:每个物品是一个结点,边的权值是,edge[u][v]的值表示用物品u换物品v的价格 一开始所有物品都置为原价,即所有dist[i]为原价,用dijkstra算法,算出0点(啥物品都没有)到各点的最短距离,求出dist[1]即为花费 枚举每个物品的等级为这条交易链的最高等级,把所有等级高于它的或者比它小超过等级限制的点都剔除,可以用bool数组剔除,然后用上述的d

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ-3268-最短路(dijkstra算法)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12494   Accepted: 5568 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X

hdu 2544 最短路 Dijkstra

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间,要求输出起点到终点的最短时间. /* 最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 35043 Accepted Submission

最短路Dijkstra和Flyod

Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M& lt;=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路.N=M=0表 示输入结束.接下来

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA)

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n,树根为编号1,选择一些边,使得所有节点构成一棵树,选择边的代价是(子孙的点的重量)×(这条边的价值).求代价最小多少. 分析: 单看每个点被计算过的代价,很明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 不过坑点还是很多的. 点的数量高达5w个,用矩阵存不行,只能用边存. 还有路径和结

最短路——dijkstra

                                   最短路--dijkstra                 这次写一下我用dijkstra算法做单源最短路的经历.             首先先引入一个问题:给你两个数 m,n.n代表一共有n个村庄.之后有m行,每行包含三个数 from,to,dist.这三个数代表着,从村庄from到村庄to有dist 这么长的距离,此为无向图.           现在想知道从村庄1到村庄n的最短的路径是多少.           相关题