codevs——1269 匈牙利游戏
Welcome to the Hungary Games! The streets of Budapest form a twisted network of one-way streets.
欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络。
You have been forced to join a race as part of a “Reality TV” show where you race through these streets, starting at the Sz´echenyi thermal bath (s for short) and ending at the Tomb of G¨ ul Baba (t for short).
你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束。
Naturally, you want to complete the race as quickly as possible, because you will get more promo- tional contracts the better you perform.
很自然的,你想要尽快的完成比赛,因为你的比赛完成的越好,你就能得到更多的商业促销合同。
However, there is a catch: any person who is smart enough to take a shortest s-t route will be thrown into the P´alv¨olgyi cave system and kept as a national treasure. You would like to avoid this fate, but still be as fast as possible. Write a program that computes a strictly-second-shortest s-t route.
但是,有一个需要了解的是,如果有人过于聪明找到从s到t的最短路线,那么他就被扔到国家极品人类保护系统中作为一个国家宝藏收藏起来。你显然要避免这种事情的发生,但是也想越快越好。写一个程序来计算一个从s到t的严格次短路线吧。
Sometimes the strictly-second-shortest route visits some nodes more than once; see Sample Input 2 for an example.
有的时候,严格次短路线可能访问某些节点不止一次。样例2是一个例子。
The first line will have the format N M, where N is the number of nodes in Budapest and M is the number of edges. The nodes are 1,2,...,N; node 1 represents s; node N represents t. Then there are M lines of the form A B L, indicating a one-way street from A to B of length L. You can assume that A != B on these lines, and that the ordered pairs (A,B) are distinct.
第一行包含两个整数N和M,N代表布达佩斯的节点个数,M代表边的个数。节点编号从1到N。1代表出发点s,N代表终点t。接下来的M行每行三个整数A B L,代表有一条从A到B的长度为L的单向同路。你可以认为A不等于B,也不会有重复的(A,B)对。
Output the length of a strictly-second-shortest route from s to t. If there are less than two possible lengths for routes from s to t, output −1.
输出从s到t的严格次短路的长度。如果从s到t的路少于2条,输出-1。
样例输入1:
4 6
1 2 5
1 3 5
2 3 1
2 4 5
3 4 5
1 4 13
样例输入2:
2 2
1 2 1
2 1 1
样例输出1:
11
样例输出2:
3
对于样例1:
There are two shortest routes of length 10 (1 → 2 → 4,1 → 3 → 4) and the strictly-second- shortest route is 1 → 2 → 3 → 4 with length 11.
对于样例2:
The shortest route is 1 → 2 of length 1, and the strictly-second route is 1 → 2 → 1 → 2 of length 3.
次短路裸题
是单向边,本蒟蒻傻不拉几的用双向边坐了半天、、、、(竟然还有分。。。ORZ)
我们要建反向边,因为我们在反着跑spfa的时候要用到反向边、、、、
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 510000
#define maxn 99999999
using namespace std;
bool vis[N];
int n,m,x,y,z,sum,ans,tot,d1[N],d2[N],head[N],head1[N];
queue<int>q;
struct Edge
{
int to,dis,from,next;
}edge[N<<],edge1[N<<];
int add(int x,int y,int z)
{
tot++;
edge[tot].to=y;
edge[tot].dis=z;
edge[tot].next=head[x];
head[x]=tot;
edge1[tot].to=x;
edge1[tot].dis=z;
edge1[tot].next=head1[y];
head1[y]=tot;
}
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
int spfa(int s,int *dis,int *head,Edge *edge)
{
;i<=n;i++) dis[i]=maxn,vis[i]=false;
vis[s]=; q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();vis[x]=false;
for(int i=head[x];i;i=edge[i].next)
{
int t=edge[i].to;
if(dis[t]>dis[x]+edge[i].dis)
{
dis[t]=dis[x]+edge[i].dis;
if(!vis[t]) vis[t]=true,q.push(t);
}
}
}
}
int main()
{
n=read(),m=read();
;i<=m;i++)
{
x=read(),y=read(),z=read();
add(x,y,z);
}
spfa(,d1,head,edge),spfa(n,d2,head1,edge1);
ans=maxn;
;i<=n;i++)
for(int j=head[i];j;j=edge[j].next)
{
int t=edge[j].to;
sum=d1[i]+d2[t]+edge[j].dis;
if(sum>d1[n]&&sum<ans) ans=sum;
}
if(ans==maxn) printf("-1\n");
else printf("%d",ans);
;
}
codevs——1269 匈牙利游戏的更多相关文章
- 求次短路 codevs 1269 匈牙利游戏
codevs 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Descriptio ...
- codevs 1269 匈牙利游戏——次短路(spfa)
欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络. 你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束. 很自然的,你想要尽快的完 ...
- codevs 1269 匈牙利游戏
/*暴力+乱搞 55分(似乎只有暴力得分了)*/ #include<iostream> #include<cstdio> #include<cstring> #in ...
- 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- CODEVS——T 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
http://codevs.cn/problem/1269/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Descript ...
- 【wikioi】1269 匈牙利游戏(次短路+spfa)
http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...
- C++之路进阶codevs1269(匈牙利游戏)
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- 匈牙利游戏(codevs 1269)
题目描述 Description Welcome to the Hungary Games! The streets of Budapest form a twisted network of one ...
- codevs 2853 方格游戏--棋盘dp
方格游戏:http://codevs.cn/problem/2853/ 这和传纸条和noip方格取数这两个题有一定的相似性,当第一眼看到的时候我们就会想到设计$dp[i][j][k][l]$(i,j表 ...
随机推荐
- SQLite-Like语句
SQLite – LIKE子句 使用SQLite LIKE运算符 用于匹配文本.如果搜索表达式可以匹配模式表达式,如操作符将返回true,这是1.有两个通配符与Like操作符一起使用: The per ...
- 事件绑定、取消的二种形式 & call
<script> //call 函数下的一个方法,call方法第一个参数可以改变函数执行过程中的内部this的指向,call方法第二个参数开始就是原来函数的参数列表. function f ...
- Asp.Net Core 入门(三) —— 自定义中间件
上一篇我们讲了Startup文件,其中着重介绍了中间件,现在我们就来自定义我们自己的中间件吧. 中间件通常封装在一个类中,并使用扩展方法进行暴露.它需要拥有一个类型为RequestDelegate的成 ...
- C#中byte类型运算
首先看下面一段代码 byte x = 1; byte y = 2; byte z = x + y; Console.WriteLine(z); 可能很多人会说显示结果是3. 其实,这段代码无法运行,因 ...
- 【简●解】[USACO] 照片Photo
[简●解][USACO] 照片Photo [题目大意] 在\(1\)~\(N\)的序列上有\(M\)个区间,使得这\(M\)个小区间每个覆盖了且仅覆盖了一个点,求最多点数,如果无解,输出\(-1\). ...
- luogu 1968 美元汇率
https://www.luogu.org/problemnew/show/P1968 定义二维数组f[ ][ ],f[i][1]表示在第i天将马克变为美元,f[i][2]表示在第i天将美元变为马克. ...
- ArrayList 和 LinkedList 区别。
1. ArrayList和LinkedList都是实现了List接口的容器类,用于存储一系列的对象引用.他们都可以对元素的增删改查进行操作. 2. ArrayList是实现了基于动态数组的数据结构,L ...
- SecureCRT 64位 破解版v8.1.4
http://www.xue51.com/soft/1510.html#xzdz securecrt 破解版是一款支持SSH1和SSH2的终端仿真程序,这个程序能够在windows系统中登陆UNIX或 ...
- 树梅派 -- 通过/sys读写ADC芯片 pcf8591
通过wiringPi等library, 在user space 通过/dev/i2c来读写i2c设备的方案不在本文讨论了. 编译SENSORS_PCF8591 模块 在Default raspberr ...
- Spider-天眼查字体反爬
字体反爬也就是自定义字体反爬,通过调用自定义的woff文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容! 1.思路 近期在爬取天 ...