HDU - 6181 Two Paths(真 · 求次短路径)
2017-08-24 22:58
363 查看
Two Paths
Problem DescriptionYou 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.
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.
Output
For each test case print length of valid shortest path in one line.
Sample Input
2
3 3
1 2 1
2 3 4
1 3 3
2 1
1 2 1
Sample Output
5
3
Hint
For 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
题意:求真 · 次短路。一条路可以走多遍,如果最短路有多种,次短路其实就是另外一种最短路……WA的应该都是这里吧……还有就是INF不能用int的,要用LL的(其实应该是可以int的吧)……我感受到了出题者满满的恶意。
解题思路:套个次短路模板就好了,然后将里面的<号改成<=号。用SPFA会超时不知道为什么……还是我写搓了,改成了迪杰斯特拉就过了。思路其实就是,存最短路的同时存下次短路 ……
#include<iostream> #include<deque> #include<memory.h> #include<stdio.h> #include<map> #include<string> #include<algorithm> #include<vector> #include<math.h> #include<stack> #include<queue> #include<set> #define INF (1LL<<62)//WA了好几发 #define ll long long int using namespace std; typedef pair<ll,int>P; struct edge{ int to; ll dis; edge(int to,ll dis){ this -> to = to; this -> dis = dis; } }; int N,R; int a,b; ll c; ll dis[100005]; //记录最短路径 ll disc[100005]; //记录次短路径 vector<edge>G[100005]; void dijkstra(){ fill(dis,dis+100005,INF); fill(disc,disc+100005,INF); priority_queue<P,vector<P>,greater<P> >q; dis[1]=0; q.push(P(0,1)); while(q.size()){ P p=q.top(); q.pop(); ll dd=p.first; int v=p.second; if(disc[v]<dd) continue; for(int i=0;i<G[v].size();i++){ edge& e=G[v][i]; ll d=dd+e.dis; if(dis[e.to]>=d){ ll ttt=d; d=dis[e.to]; dis[e.to]=ttt; q.push(P(dis[e.to],e.to)); } if(disc[e.to]>=d&&dis[e.to]<=d){ disc[e.to]=d; q.push(P(disc[e.to],e.to)); } } } cout<<disc <<endl; } int main() { int t; scanf("%d",&t); while(t--){ for(int i=0;i<100005;i++) G[i].clear(); scanf("%d%d",&N,&R); for(int i=1;i<=R;i++){ scanf("%d%d%lld",&a,&b,&c); G[a].push_back(edge(b,c)); G[b].push_back(edge(a,c)); } dijkstra(); } return 0; }
相关文章推荐
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- HDU 6181 Two Paths 次短路
- HDU 6181 Two Paths【次短路】【模板题】
- hdu 6181 Two Paths (次短路)
- 刷题—— hdu 6181 Two Paths
- hdu 6181 Two Paths
- 2017多校第10场 HDU 6181 Two Paths 次短路
- hdu 6181 Two Paths(最短路,spfa)
- 【多校训练】hdu 6181 Two Paths 次短路径 Dijkstra
- [hdu 6181 Two Paths] Dijkstra求次短路
- hdu 6181 Two Paths -最短路条数+次短路 - 多校联盟10
- 【hdu 6181】Two Paths
- hdu 6181 Two Paths (次短路)
- HDU6181 Two Paths(次短路,路径记录,spfa,2017 HDU多校联赛 第10场)
- HDU 6181 Two Paths
- Hdu 6181 Two Paths【次短路】
- HDU --- 6181 Two Paths 2017第十场多校签到题【裸次短路】
- HDU 6181 Two Paths
- hdu 6181 Two Paths
- HDU 6181 Two Paths (次短路)