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。
/*
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)的更多相关文章
- uva 10801 - Lift Hopping(最短路Dijkstra)
/* 题目大意: 就是一幢大厦中有0-99的楼层, 然后有1-5个电梯!每个电梯有一定的上升或下降速度和楼层的停止的位置! 问从第0层楼到第k层最少经过多长时间到达! 思路:明显的Dijkstra , ...
- uva 10986 - Sending email(最短路Dijkstra)
题目连接:10986 - Sending email 题目大意:给出n,m,s,t,n表示有n个点,m表示有m条边,然后给出m行数据表示m条边,每条边的数据有连接两点的序号以及该边的权值,问说从点s到 ...
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
- 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法
图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...
- 单源最短路dijkstra算法&&优化史
一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...
- HUD.2544 最短路 (Dijkstra)
HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...
- 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)
layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...
- 最短路Dijkstra算法的一些扩展问题
最短路Dijkstra算法的一些扩展问题 很早以前写过关于A*求k短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...
随机推荐
- C语言深度剖析学习错误点记录
0. static修饰变量和函数 static修饰变量,1)限定作用域,本文件内.全局变量(自定义起,本文件前面要用需extern声明),局部变量函数内:2)生命周期,程序运行期间一直保存. stat ...
- iOS UITableView的分割线短15像素,移动到最左边的方法(iOS8)
有好几个朋友问我ios 分割线端了一些 如何解决,于是我就写一篇博客吧.为什么我说是少了15像素呢?首先我们拖拽一个默认的tableview 控件! 看下xcode5 面板的inspector(检查器 ...
- oracle和sql server的区别(1)
A.instance和database 1.从oracle的角度来说,每个instance对应一个database.有时候多个instance对应一个database(比如rac环境).有自己的Sys ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(2)
12.2安装 Postgres-XC 可以从 http://postgres-xc.sourceforge.net/下载Postgres-XC.对于本书,我们使用1.0.3版本的Postgres-XC ...
- !!常见的上穿突破M20方式——突破还是试探的判断
1和2相似之处在于M5<M20, 最大的区别是M20和M5之间的间距在放大还是缩小,如果是放大,大盘会先试探一下.如果越缩越小,大盘必须选择方向 但是必须注意的是,即使是夹角缩小,选在方向不一定 ...
- 转:python webdriver API 之cookie 处理
有时候我们需要验证浏览器中是否存在某个 cookie,因为基于真实的 cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除 cookie 信息.webdrive ...
- Python学习总结14:时间模块datetime & time & calendar (一)
Python中的常用于处理时间主要有3个模块datetime模块.time模块和calendar模块. 一.time模块 1. 在Python中表示时间的方式 1)时间戳(timestamp):通常来 ...
- MyEclipse安装插件的三种方法和使用心得
本文讲解MyEclipse(MyEclipse10)的三种方法,以TestNG为例 Eclipse update site URL: http://beust.com/eclipse. 一.通过My ...
- 《zw版·Halcon-delphi系列原创教程》 水果自动分类脚本(机器学习、人工智能)
<zw版·Halcon-delphi系列原创教程> 水果自动分类脚本(机器学习.人工智能) 前面介绍了超市,流水线,酸奶的自动分类算法,下面再介绍一个水果的自动分类算法. Halcon强大 ...
- 【Ah20160703】咏叹 By C_SUNSHINE
咏叹 By C_SUNSHINE [试题描述] Salroey拿到了一个1~n的排列A,她想对这个排列进行冒泡排序: counter=0 While A不是升序的 counter=counter+1 ...