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真是又爱又恨. 爱着它的书写方便. 恨着它的教程稀少总走 ...
随机推荐
- 如何使用的Ue4自带的SQLiteSupport
在UE4.6版本加入的模块.可以让开发者使用SQLite数据库.SQlite是个轻量型的本地数据库. 我下面就来介绍一下如何使用这个模块. 第一步:下载SQLite源代码以及SQLite GUI管理工 ...
- CodeForces 279D The Minimum Number of Variables 题解
题目大意: 有一组n个不相同的数字组成数串:a1,a2,a3-an. 1.一个数组b. 2.第一个操作我们将b0的值赋为a1.之后我们有n-1个操作,第k次操作我们将by=bi+bj(y,i,j可能相 ...
- 你应该在开始API开发之前知道的事(下)(翻译)
放了十多天,一直在玩没写,今天终于要把坑填完了.有部分没翻,主要是一般都用不上的,有兴趣的朋友可以自己翻下. 上篇地址 :http://www.cnblogs.com/Scohura/p/357360 ...
- NOI2015 题解
[NOI2015]程序自动分析 离散化+并查集. [NOI2015]软件包管理器 [Noi2015]寿司晚宴 [Noi2015]荷马史诗 [NOI2015]品酒大会 [Noi2015]小园丁与老司机
- 01 - 初探JavaScript魅力
网页特效原理 用JavaScript修改样式 编写JS的流程 布局:HTML+CSS 属性:确定要修改哪些属性 事件:确定用户做哪些操作(产品设计) 编写JS:在事件中,用JS来修改页面元素的样式 鼠 ...
- 错误信息:attempt to create saveOrUpdate event with null entity
错误信息:attempt to create saveOrUpdate event with null entity; 这个错误网上答案比较多,我也不多说了. 我遇到的问题是在前台传过来的参数是nul ...
- 对拍老是忘记的看这里:bat代码
需要写三个程序,makedata.exe 产生测试数据, program1.exe 是你要检测的程序,program2.exe 往往是一个正确但效率不高(暴力的居多)的程序. 代码很简单,稍作解释:l ...
- USACO翻译:USACO 2014 US Open 三题
USACO 2014 US Open 一.题目概览 中文题目名称 牧场装饰 里程表 牛像展览 英文题目名称 decorate odometer fairphoto 可执行文件名 decorate od ...
- IOS网络第二天 - 04-黑酷-GDataXML 解析
****** - (void)viewDidLoad { [super viewDidLoad]; /** 加载服务器最新的视频信息 */ // 1.创建URL NSURL *url = HMUrl( ...
- 纯Java配置使用slf4j配置log4j
工程目录如下 代码里面用的是slf4j,但是想要用log4j来管理日志,就得添加slf4j本来的jar,然后添加log4j和slf4j箱关联的jar即可. 如果是maven项目的话添加下面的依赖即可 ...