1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
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.
分类标签 Tags 点此展开
求次短路的问题
注意spfa中的三条注释
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=;
const int maxn=0xfffffff;
struct node
{
int u;
int v;
int w;
int next;
}edge[MAXN];
int num=;
int head[MAXN];
int n,m;
int dis[MAXN];
int vis[MAXN];// 是否在队列中
int tot=;
int pre[MAXN];// 记录次短路
void spfa()
{
dis[]=;
vis[]=;
queue<int>q;
q.push();
while(q.size()!=)
{
int p=q.front();
q.pop();
vis[p]=;
for(int i=head[p];i!=-;i=edge[i].next)
{
int to=edge[i].v;
if(dis[to]>dis[p]+edge[i].w)
{
pre[to]=dis[to];// 记录下更新之前的长度 即次长
dis[to]=dis[p]+edge[i].w;
//if(edge[i].v==n)tot++;
if(vis[to]==)
{
q.push(to);
vis[to]=;
}
}
else if(dis[to]!=dis[p]+edge[i].w&&pre[to]>dis[p]+edge[i].w)
{
pre[to]=dis[p]+edge[i].w;// 根据条件,此时需要更新,否则就不是次短路
if(vis[to]==)
{
q.push(to);
vis[to]=;
}
}
if(pre[to]>pre[p]+edge[i].w)// 同理,需要更新
{
pre[to]=pre[p]+edge[i].w;
if(vis[to]==)
{
q.push(to);
vis[to]=;
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
head[i]=-;
dis[i]=maxn;
pre[i]=maxn;
}
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&edge[num].u,&edge[num].v,&edge[num].w);
edge[num].next=head[edge[num].u];
head[edge[num].u]=num++;
}
spfa();
if(pre[n]!=maxn)
printf("%d",pre[n]);
else
{
printf("-1");
}
return ;
}
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛的更多相关文章
- CODEVS——T 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
http://codevs.cn/problem/1269/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Descript ...
- code vs1262 不要把球传我(组合数学) 2012年CCC加拿大高中生信息学奥赛
1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 De ...
- 1267 老鼠的旅行 2012年CCC加拿大高中生信息学奥赛
1267 老鼠的旅行 2012年CCC加拿大高中生信息学奥赛 题目描述 Description You are a mouse that lives in a cage in a large lab ...
- codevs 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description CCC的足球比赛和传统的足球比赛有一点不同, 一次进球当且仅当先后接触到球的 ...
- 【动态规划】【记忆化搜索】【搜索】CODEVS 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛
可以暴力递归求解,应该不会TLE,但是我们考虑记忆化优化. 设f(i,j)表示第i个数为j时的方案数. f(i,j)=f(1,j-1)+f(2,j-1)+……+f(i-1,j-1) (4>=j& ...
- codevs 1277 生活大爆炸 2012年CCC加拿大高中生信息学奥赛
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description Sheldon and Leonard are physicists wh ...
- codevs 1267 老鼠的旅行 2012年CCC加拿大高中生信息学奥赛
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description You are a mouse that lives in a cage in ...
- 求次短路 codevs 1269 匈牙利游戏
codevs 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Descriptio ...
- codevs——1269 匈牙利游戏
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Descript ...
随机推荐
- BZOJ4085: [Sdoi2015]音质检测
BZOJ4085: [Sdoi2015]音质检测 由于这题太毒了,导致可能会被某些人卡评测,于是成了一道权限题... 本蒟蒻表示没钱氪金... 但是可以去洛谷/Vijos搞搞事... 但是洛谷上只能评 ...
- 解决因为终端打印造成的java程序假死
问题状态: java 程序 日志采用 log4j 运行时由另一个管理进程拉起,程序在后台运行. 现象: 程序后台运行时,运行一段时间后假死 分析原因: 尝试打印输出,定位假死的具体位置,发现出现假死的 ...
- SuperSocket使用自定义协议与服务端通信发送实例
AsyncTcpSession tcpSession = new AsyncTcpSession(); public Encoding encoding = Encoding.GetEncoding( ...
- Flask从入门到做出一个博客的大型教程(一)
本项目全部在虚拟环境中运行,因此请参照前面的文章,链接为https://blog.csdn.net/u014793102/article/details/80302975 建立虚拟环境后,再接着完成本 ...
- 剑指Spring源码(三)俯瞰Spring的Bean的生命周期(大众版)
距离上一次写Spring源码解析,已经过去了快要好几个月了,主要原因还是Spring的源码解析类文章太难写了,不像我先前写的什么CAS源码,AQS源码,LinkedBlockingQueue等等,这些 ...
- UVA - 12563 Jin Ge Jin Qu hao (01背包)
InputThe first line contains the number of test cases T (T ≤ 100). Each test case begins with two po ...
- 洛谷P1505 [国家集训队]旅游
题目描述 \(Ray\) 乐忠于旅游,这次他来到了\(T\) 城.\(T\) 城是一个水上城市,一共有 \(N\) 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,\(T ...
- 洛谷P3582 [POI2015]KIN
题目描述 共有\(m\)部电影,编号为\(1--m\),第\(i\)部电影的好看值为\(w[i]\).在\(n\)天之中(从\(1~n\)编号)每天会放映一部电影,第\(i\)天放映的是第\(f[i] ...
- thinkphp5判断移动或pc端访问并调用不同模块
把下面的代码放在app\common.php公共方法那里. function isMobile() { if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) { ret ...
- 牛客假日团队赛1 J.分组
链接: https://ac.nowcoder.com/acm/contest/918/J 题意: 在Farmer John最喜欢的节日里,他想要给他的朋友们赠送一些礼物.由于他并不擅长包装礼物,他想 ...