codevs1269匈牙利游戏(spfa跑次短路)
来源:互联网 发布:淘宝客服转接设置 编辑:程序博客网 时间:2024/06/04 18:09
题目描述 Description
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是一个例子。
输入描述 Input Description
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 Description
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。
样例输入 Sample Input
样例输入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
样例输出 Sample Output
样例输出1:
11
样例输出2:
3
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int MAXN = 200000+5;int n,m,head[MAXN],tot,dis[MAXN],dis2[MAXN],nxt[MAXN<<1];/*用dis存最短路,dis2存次短路*/bool vis[MAXN],flag = 0;struct Edge{ int from,to,cost;}e[MAXN];void build(int f,int t,int d){ e[++tot].from = f; e[tot].to = t; e[tot].cost = d; nxt[tot] = head[f]; head[f] = tot;}queue <int> q;void spfa(int s){ memset(dis,0x7f,sizeof(dis)); memset(dis2,0x7f,sizeof(dis2)); memset(vis,0,sizeof(vis)); vis[s] = 1; dis[s] = 0; q.push(s); while(!q.empty()) { int x = q.front(); q.pop(); vis[x] = 0; for(int i = head[x] ; i ; i = nxt[i]) { int u = e[i].to; if(dis[u] > dis[x] + e[i].cost) { dis2[u] = dis[u];//短的加入最短路,大的更新次短路 dis[u] = dis[x] + e[i].cost; if(!vis[u]) { vis[u] = 1; q.push(u); } } else if(dis[u] < dis[x]+e[i].cost && dis2[u] > dis[x]+e[i].cost )//如果这条路比最短路要长,比次短路要短 { dis2[u] = dis[x]+e[i].cost ;//更新次短路 if(!vis[u]) { vis[u] = 1; q.push(u); } } if(dis2[u] > dis2[x] + e[i].cost )//用前面的次短路更新次短路 { dis2[u] = dis2[x]+e[i].cost ; if(!vis[u]) { vis[u] = 1; q.push(u); } } } } if(dis2[n] > 200000000) flag = 1;}int main(){ scanf("%d%d",&n,&m); for(int i = 1; i <= m; i ++) { int aa,bb,cc; scanf("%d%d%d",&aa,&bb,&cc); build(aa,bb,cc); } spfa(1); if(flag ) puts("-1"); else printf("%d",dis2[n]); return 0;}
- codevs1269匈牙利游戏(spfa跑次短路)
- Codevs1269 匈牙利游戏 次短路spfa
- codevs 1269 匈牙利游戏 SPFA+次短路
- 【CodeVS】1269 匈牙利游戏 最短路 次短路 SPFA
- 【codevs1269】匈牙利游戏
- Codevs1269 匈牙利游戏
- [codevs1269] 匈牙利游戏
- 匈牙利游戏(次短路问题)
- codevs 1269 匈牙利游戏(次短路)
- nyoj1006(最短路次短路spfa)
- POJ 3255(次短路+SPFA)
- Roadblocks (SPFA次短路求法)
- poj3255 Roadblocks--次短路spfa
- poj 3255 Roadblocks(无向图次短路 SPFA)
- poj 3255 Roadblocks(spfa次短路 || Dijsktra)
- 最短路和次短路的条数(dijstra算法或spfa算法)POJ3463
- poj 3255 次短路(第k短路) A* + spfa 或 dijkstra
- POJ3255:Roadblocks(次短路 SPFA+A星)
- 第4节--Built-in package和External package
- C++ 未捕获的异常
- 搜索二叉树
- 7shiro安全框架、web项目的发布
- php中web上传文件的原理及如何限制上传文件的大小及格式
- codevs1269匈牙利游戏(spfa跑次短路)
- UVA 10905 Children's Game 水 .
- Android-实现底部弹出PopupWindow并让背景逐渐变暗
- 《深度探索C++对象模型》:Data member的布局
- mysql-开发环境为VS2013的嵌入式mysql
- poj3281——Dining(网络流+拆点)
- (笔记)运算符和表达式
- abs()与fabs()
- AFN如何获取并解析XML