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短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...
随机推荐
- Eclipse/Myeclipse 开发项目技巧
Eclipse/Myeclipse 开发项目 编程的本质: 把现实生活中的业务逻辑用代码实现. eclipse 是一个开放源代码.基于Java的可扩展开发平台. (最初主要用来Java语言开发,但目前 ...
- 学习JAVA 安装
下载 JDK Tomcat9 Apache mod_jk 1.安装JDK 这里就说配置环境变量 添加环境变量 JAVA_HOME(就是jdk的安装路径) CLASSPATH( ...
- 转:VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径
http://www.cnblogs.com/lidabo/archive/2012/05/29/2524170.html
- 测试App运行状态
示例代码: #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - ...
- C++ note
主要是为了学习c++的类和对象 内容摘自 c++概述 http://see.xidian.edu.cn/cpp/biancheng/cpp/rumen_1/ 1,变量 ,C++中,我们可以在 ...
- Java基础之处理事件——使用适配器类(Sketcher 3 using an Adapter class)
控制台程序. 适配器类是指实现了监听器接口的类,但监听器接口中的方法没有内容,所以它们什么也不做.背后的思想是:允许从提供的适配器类派生自己的监听器类,之后再实现那些自己感兴趣的类.其他的空方法会从适 ...
- 不等高cell搭建(二)
一.commentView模块搭建 commentView样式分为两种 1.xib搭建界面 1.1 因为评论的样式大体上一样,我们可以用同一个xib来处理 1.2 最热评论 用 ...
- editplus3运行Python程序
editplus3是一款不错的编辑器,他可以编译,运行java,php等各种程序,现把他运行Python程序的方法贴出来,首先得安装python,然后打开editplug3,工具——配置用户工具——组 ...
- 在自定义的UINavigationController中设置背景图片
//这个方法中设置 + (void)initialize { UINavigationBar *bar = [UINavigationBar appearance]; [bar setBackgrou ...
- 两条直线(蓝桥杯)二分枚举+RMQ
算法提高 两条直线 时间限制:1.0s 内存限制:256.0MB 问题描述 给定平面上n个点. 求两条直线,这两条直线互相垂直,而且它们与x轴的夹角为45度,并且n个点中离这两条 ...