C++之路进阶codevs1269(匈牙利游戏)
1269 匈牙利游戏
2012年CCC加拿大高中生信息学奥赛
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.
题解
两遍spfa,然后枚举起点终点(所必走的边),取次小,因为是严格次短,所以可以这样YY掉
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 100000+10
#define INF 0x7fffffff using namespace std; struct ss
{
int to,next,edge;
}e_t[maxn<<],e_f[maxn<<]; struct sss
{
int t,f;
void add(int x)
{
t=x; f=x;
}
}dis[maxn]; int head_t[maxn],head_f[maxn],cnt_t,cnt_f,vis[maxn]; void insert_t(int u,int v,int edge)
{
e_t[++cnt_t].next=head_t[u];
e_t[cnt_t].to=v;
e_t[cnt_t].edge=edge;
head_t[u]=cnt_t;
} void insert_f(int u,int v,int edge)
{
e_f[++cnt_f].next=head_f[u];
e_f[cnt_f].to=v;
e_f[cnt_f].edge=edge;
head_f[u]=cnt_f;
} void spfa_t(int x)
{
queue<int>que;
que.push(x);
vis[x]=;dis[x].t=;
while (!que.empty())
{
int now=que.front();que.pop();vis[now]=;
for (int i=head_t[now];i;i=e_t[i].next)
if (dis[e_t[i].to].t>dis[now].t+e_t[i].edge)
{
dis[e_t[i].to].t=dis[now].t+e_t[i].edge;
if (!vis[e_t[i].to]) que.push(e_t[i].to),vis[e_t[i].to]=;
}
}
} void spfa_f(int x)
{
queue<int>que;
que.push(x);
vis[x]=;dis[x].f=;
while (!que.empty())
{
int now=que.front();que.pop();vis[now]=;
for (int i=head_f[now];i;i=e_f[i].next)
if (dis[e_f[i].to].f>dis[now].f+e_f[i].edge)
{
dis[e_f[i].to].f=dis[now].f+e_f[i].edge;
if (!vis[e_f[i].to]) que.push(e_f[i].to),vis[e_f[i].to]=;
}
}
} int n,m; int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
insert_t(x,y,z),insert_f(y,x,z);
}
for (int i=;i<=n;i++) dis[i].add(INF);
spfa_t();spfa_f(n);
int ans=INF,sans=-;
for (int i=;i<=n;i++)
for (int j=head_t[i];j;j=e_t[j].next)
{
if (dis[i].t!=INF&&dis[e_t[j].to].f!=INF&&ans>dis[e_t[j].to].f+dis[i].t+e_t[j].edge)
sans=ans,ans=dis[e_t[j].to].f+dis[i].t+e_t[j].edge;
if (dis[i].t!=INF&&dis[e_t[j].to].f!=INF&&ans<dis[e_t[j].to].f+dis[i].t+e_t[j].edge&&dis[e_t[j].to].f+dis[i].t+e_t[j].edge<sans)
sans=dis[e_t[j].to].f+dis[i].t+e_t[j].edge;
}
if (sans==INF) cout<<-<<endl; else cout<<sans<<endl;
return ;
}
C++之路进阶codevs1269(匈牙利游戏)的更多相关文章
- 求次短路 codevs 1269 匈牙利游戏
codevs 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Descriptio ...
- 【wikioi】1269 匈牙利游戏(次短路+spfa)
http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...
- codevs 1269 匈牙利游戏——次短路(spfa)
欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络. 你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束. 很自然的,你想要尽快的完 ...
- 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
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——1269 匈牙利游戏
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Descript ...
- CODEVS——T 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
http://codevs.cn/problem/1269/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Descript ...
- C++之路进阶——bzoj1455(罗马游戏)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser gryz2016 Logout 捐赠本站 Notice:由于本OJ ...
- Photon服务器进阶&一个新游戏的出产(一)
许久没写了,今日闲来无事随性一些,总结一下最近的游戏经历(写游戏) Photon服务器的进阶~ 距离上一篇估计也过去一个多月了,对于Photon真是又爱又恨. 爱着它的书写方便. 恨着它的教程稀少总走 ...
随机推荐
- python中列表,元组,字符串互相转换
列表,元组和字符串python中有三个内建函数:,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示 >>> s = "xxxxx& ...
- 我觉得好用的VS扩展(不定期更新)
首先向这些扩展的创作者致敬 这里都是2013版的 有些在给出的连接里有该扩展支持的其他版本连接 当然你也可以通过 VS->工具->扩展和更新->在线->搜索扩展名 来找到它们 ...
- div可编辑状态设置
<div contentedittable="ture"></div>
- letter upper lower combo
以前同事为了炫耀ruby的简洁,特意出一道题来考小陈: 在写一个爆破密码的字典生成工具,其中有这样一个需求: 输入一个单词:列出这个单词的所有大小写组合,比如ruby Ruby rUby ruBy r ...
- 20145215实验二 Java面向对象程序设计
一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...
- IIS7应用程序池集成和经典的区别
IIS7应用程序池集成和经典的区别 IIS7应用程序池有集成和经典两种模式,根据微软官方的介绍, 集成模式,如果托管应用程序在采用集成模式的应用程序池中运行,服务器将使用 IIS 和 ASP.NET ...
- C语言中定义全局变量
(1)在C语言的头文件中定义变量出现的问题 最好不要傻嘻嘻的在头文件里定义什么东西.比如全局变量: /*xx头文件*/ #ifndef _XX_头文件.H #define _XX_头文件.H in ...
- Python环境安装
Windows: 1.Python安装包下载 可以去官网下载安装包:https://www.python.org/downloads/ 2.Python安装 默认安装路径:C:\python27:也可 ...
- 【iCore3应用开发平台】发布 iCore3 应用开发平台出厂代码rev0.0.5
iCore3开发平台固件版本信息 =============================================================[stm32f407]:iCore3 ARM ...
- C++ string
C++ string best practices => LPTSTR, PSTR, CString, _T, TEXT, Win32 API, Win16. string, wstring. ...