2017多校联合第四场1011/hdu6181(次短路)
来源:互联网 发布:网络电视遥控器失灵 编辑:程序博客网 时间:2024/06/05 17:41
Two Paths
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 933 Accepted Submission(s): 431
Problem Description
You are given a undirected graph with n nodes (numbered from 1 to n) and m edges. Alice and Bob are now trying to play a game.
Both of them will take different route from 1 to n (not necessary simple).
Alice always moves first and she is so clever that take one of the shortest path from 1 to n.
Now is the Bob's turn. Help Bob to take possible shortest route from 1 to n.
There's neither multiple edges nor self-loops.
Two paths S and T are considered different if and only if there is an integer i, so that the i-th edge of S is not the same as the i-th edge of T or one of them doesn't exist.
Both of them will take different route from 1 to n (not necessary simple).
Alice always moves first and she is so clever that take one of the shortest path from 1 to n.
Now is the Bob's turn. Help Bob to take possible shortest route from 1 to n.
There's neither multiple edges nor self-loops.
Two paths S and T are considered different if and only if there is an integer i, so that the i-th edge of S is not the same as the i-th edge of T or one of them doesn't exist.
Input
The first line of input contains an integer T(1 <= T <= 15), the number of test cases.
The first line of each test case contains 2 integers n, m (2 <= n, m <= 100000), number of nodes and number of edges. Each of the next m lines contains 3 integers a, b, w (1 <= a, b <= n, 1 <= w <= 1000000000), this means that there's an edge between node a and node b and its length is w.
It is guaranteed that there is at least one path from 1 to n.
Sum of n over all test cases is less than 250000 and sum of m over all test cases is less than 350000.
The first line of each test case contains 2 integers n, m (2 <= n, m <= 100000), number of nodes and number of edges. Each of the next m lines contains 3 integers a, b, w (1 <= a, b <= n, 1 <= w <= 1000000000), this means that there's an edge between node a and node b and its length is w.
It is guaranteed that there is at least one path from 1 to n.
Sum of n over all test cases is less than 250000 and sum of m over all test cases is less than 350000.
Output
For each test case print length of valid shortest path in one line.
Sample Input
23 31 2 12 3 41 3 32 11 2 1
Sample Output
53HintFor testcase 1, Alice take path 1 - 3 and its length is 3, and then Bob will take path 1 - 2 - 3 and its length is 5.For testcase 2, Bob will take route 1 - 2 - 1 - 2 and its length is 3
Source
2017 Multi-University Training Contest - Team 10
Recommend
liuyiding | We have carefully selected several similar problems for you: 6181 6180 6179 6178 6177
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#define ll long long#define INF 1000000000000000000using namespace std;typedef pair<ll,ll> P;struct edge{ ll to,v; edge(ll to,ll v):to(to),v(v) {} edge() {}};const int maxn = 1e5+10;//const int maxe = 100005;ll V,E;vector<edge> g[maxn];ll d[maxn],d2[maxn];//最短距离和次短距离void dijkstra(int s){ priority_queue<P,vector<P>,greater<P> > pq; for(int i=1; i<=V; i++) { d[i]=INF; d2[i]=INF; } d[s]=0; pq.push(P(0,s)); while(pq.size()) { P nowe=pq.top(); pq.pop(); if(nowe.first>d2[nowe.second]) continue; //如果这个距离比当前次短路长continue for(int v=0; v<(int)g[nowe.second].size(); v++) { edge nexte=g[nowe.second][v]; ll dis=nowe.first+nexte.v; if(d[nexte.to]>dis) { swap(dis,d[nexte.to]); pq.push(P(d[nexte.to],nexte.to)); } if(d2[nexte.to]>dis&&d[nexte.to]<dis)//保证最短路是小于这个次短路的 { d2[nexte.to]=dis; pq.push(P(d2[nexte.to],nexte.to));//次短路的点进入pq } } }}int main(){ int s;//起点 int t; cin>>t; //cout<<INF<<endl; while(t--) { scanf("%d%d",&V,&E); { for(int i=1; i<=V; i++) g[i].clear(); for(int i=1; i<=E; i++) { ll f,t,v; scanf("%lld %lld %lld",&f,&t,&v); g[f].push_back(edge(t,v)); g[t].push_back(edge(f,v)); } s=1;//这题默认起点为1 dijkstra(s); printf("%lld\n",d2[V]); //cout<<d2[V]<<endl; } } return 0;}
阅读全文
0 0
- 2017多校联合第四场1011/hdu6181(次短路)
- HDU6181 Two Paths(次短路,路径记录,spfa,2017 HDU多校联赛 第10场)
- hdu6181 Two Paths(次短路)
- hdu6181次短路
- HDU6181 Two Paths【次短路】
- 2013多校联合训练第四场
- HDOJ多校联合第四场
- 2016多校联合第四场 HDU5768
- hdu6181 Two Paths 次短路模板
- hdu6181 2017多校最短路(复习了一拨儿次短路)
- 2017多校联合第四场/HDU 6068 Classic Quotation(kmp+dp)
- hdu6181 第k短路 Dijkstra(优先队列优化)+A* 2017多校1011
- hdu6181 How Many Paths Are There(次短路条数[模板])
- 【hdu6181】Two Paths(次短路----每条边经过不止一次)
- HDU/HDOJ 3875 Euclidean Algorithm 多校联合第四场
- hdu 4642 fliping game 多校联合训练第四场
- hdu 4639 hehe 多校联合训练第四场
- [模拟] 多校联合第四场 HDU5327 Olympad
- [DP] POJ1050
- 关于vue 框架与后台框架的混合使用的尝试
- ios 多线程间通信
- 字符串split切割"."
- python实现求解列表中元素的排列和组合问题
- 2017多校联合第四场1011/hdu6181(次短路)
- 简单讲解SQL的注入以及防御
- php小tips
- 网站 博客
- 论STM32标准库程序修改为HAL库
- poj 3237 Tree 树链剖分 线段树
- Why Go is my favorite programming language
- 如何学习java编程,做一个程序员大牛呢?
- Python多态