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。

 /*
Dijkstra + 优先队列优化
奇数边 + 一条边 = 偶数边 D数组装奇数边
偶数边 + 一条边 = 奇数边 d数组装偶数边
互相优化,若点C 在 d 数组(装偶数边)为INF(没被更新),则无法达到
否则可以达到并且是最短的
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 100010
const int inf=;
struct Node
{
int w,next,to;
}edge[MAXN*];
struct node
{
int x,d;
node(){}
node(int a,int b){x=a;d=b;}
bool operator < (const node &a) const
{
if(d==a.d) return x<a.x;
else return d>a.d;
}
}; int head[MAXN],tot,V,E,d[MAXN],D[MAXN]; void add(int u,int v,int cost)
{
edge[tot].to=v;
edge[tot].w=cost;
edge[tot].next=head[u];
head[u]=tot++;
} void dijkstra()
{
priority_queue<node> que;
while(!que.empty()) que.pop();
for(int i=;i<=V;i++){
D[i]=d[i]=inf;
}
d[]=;
que.push(node(,));
while(!que.empty()){
node a=que.top();que.pop();
int top=a.x;
for(int k=head[top];~k;k=edge[k].next){
int cost = edge[k].w;
int v = edge[k].to;
if( d[top] + cost < D[v] ){
D[v] = d[top] + cost;
que.push(node(v,D[v]));
}
if( D[top] + cost < d[v] ){
d[v] = D[top] + cost;
que.push(node(v,d[v]));
}
}
}
} int main()
{
while(~scanf("%d%d",&V,&E)){
int u,v,w;
tot=;
memset(head,-,sizeof(head));
for(int i=;i<=E;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dijkstra();
long long ans;
if(d[V]==inf){
ans=-;
}
else ans=d[V];
printf("%d\n",ans);
}
return ;
}

2016-06-02

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

  1. uva 10801 - Lift Hopping(最短路Dijkstra)

    /* 题目大意: 就是一幢大厦中有0-99的楼层, 然后有1-5个电梯!每个电梯有一定的上升或下降速度和楼层的停止的位置! 问从第0层楼到第k层最少经过多长时间到达! 思路:明显的Dijkstra , ...

  2. uva 10986 - Sending email(最短路Dijkstra)

    题目连接:10986 - Sending email 题目大意:给出n,m,s,t,n表示有n个点,m表示有m条边,然后给出m行数据表示m条边,每条边的数据有连接两点的序号以及该边的权值,问说从点s到 ...

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

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

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

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  5. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  6. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...

  7. 单源最短路dijkstra算法&&优化史

    一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...

  8. HUD.2544 最短路 (Dijkstra)

    HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...

  9. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

  10. 最短路Dijkstra算法的一些扩展问题

    最短路Dijkstra算法的一些扩展问题     很早以前写过关于A*求k短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...

随机推荐

  1. sql语句查询出数据重复,取唯一数据

    select distinct mr.id,ifnull(mr.pid,0) as pid,mr.name from sys_role_res srr left join main_res mr on ...

  2. Swift实战-豆瓣电台(三)获取网络数据

    观看地址:http://v.youku.com/v_show/id_XNzMwMzQxMzky.html 这节内容,我们先说了怎么将storyboard中的组件在类中进行绑定.然后写了一个类用来获取网 ...

  3. Oracle-数据库

    Oracle 1.特点 关系型数据库 采用二维表的行使管理数据库 具有行和列  表间存在关联关系 2.安装 数据库(11g) 版本类型 32位    64位  安装类型 桌面类 本机开发 服务器类 生 ...

  4. for 穷举、迭代 while循环

    1.穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 2.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100 ...

  5. Java基础(38):Calendar类的应用(优于Date类)

    Calendar 类的应用 Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calen ...

  6. ruby初步学习中遇到的错误

    print <<off This is the second way of creating here document ie. multiple line string; off 报错: ...

  7. 禁用cookie后session是如何设置的

    我们都知道当在session 会话有基于cookie和基于url两种传递SESSIONID的方法.为了实现客户端禁止cookie发送的情况也不影响客户登陆网站,可以设置 php.ini中 sessio ...

  8. windows namedPipe 命名管道clent and server

    1.client: #include "iostream" #include "windows.h" using namespace std; void mai ...

  9. paper 20 :color moments

    图像的颜色特征是很重要的,其中颜色矩也是很重要的一部分.(还有一个关于图像颜色特征的review,对于image color写的比较全面).还有,我要强调一下,本blog上的链接都是Google学术上 ...

  10. EBS R12版 GL追溯到各个模块

    应收.应付.收款.付款等单据都可以生成ERP的日记帐,那么这些模块的关系是如何关联的呢,我们将会解决这个问题. 各个模块与总帐模块的关系,主要是通过子分类帐来进行关联的. 下面的SQL就是总帐与子分类 ...