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真是又爱又恨. 爱着它的书写方便. 恨着它的教程稀少总走 ...
随机推荐
- linux 查看Java 进程的内存使用情况
top -b -n 1 | grep java| awk '{print "PID:"$1",mem:"$6",CPU percent:"$ ...
- Spring+SpringMvc+Mybatis整合注意事项
1.web.xml代码如下 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ...
- 洛谷 P1387 最大正方形 Label:奇怪的解法
题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...
- [机器学习] ——KNN K-最邻近算法
KNN分类算法,是理论上比较成熟的方法,也是最简单的机器学习算法之一. 该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别 ...
- 伸展树Splay
新学的,其实吧,就那么回事.... 看了几天,splay处理序列问题,真的非常厉害,翻转,插入,删除,线段树实现不了的功能,splay用起来很方便. POJ 3580 SuperMemo 这题基本就是 ...
- OpenSceneGraph控制模型
OpenSceneGraph控制模型 转自:http://www.cppblog.com/eryar/archive/2012/05/28/176538.html 一.简介 对模型的控制就是修改模型的 ...
- Spark 架构
本文转之Pivotal的一个工程师的博客.觉得极好. 作者本人经常在StackOverflow上回答一个关系Spark架构的问题,发现整个互联网都没有一篇文章能对Spark总体架构进行很好的描述, ...
- Areas on the Cross-Section Diagram
Areas on the Cross-Section Diagram Aizu - ALDS1_3_D Areas on the Cross-Section Diagram 地域の治水対策として.洪 ...
- javascript学习之带滚动条的图片
之前找了好久没有找到,就自已动手写了一个: <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...
- tomcat下jsp要加工程名后缀才能访问的问题解决
今天发现一个部署的项目,在tomcat中配置了去掉工程名,直接通过域名访问.配置后其它的html.动态请求等都可以不带工程名访问,但是只要访问jsp页面就报404错误,加上工程名访问jsp却又正常. ...