hdu 6181 Two Paths(次短路)
2017-08-24 20:28
369 查看
Two Paths
[b]Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 150 Accepted Submission(s): 88
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case print length of valid shortest path in one line.
[align=left]Sample Input[/align]
2
3 3
1 2 1
2 3 4
1 3 3
2 1
1 2 1
[align=left]Sample Output[/align]
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
题意:
在图中找一条从1->n的次短路,可以走回头路
解析:
次短路模板,类似poj3255
#include<cstdio> #include<cstring> #include<queue> #include<functional> #include<vector> #include<algorithm> using namespace std; typedef long long int ll; #define MAXM 350010 #define MAXN 250010 #define INF 1223372036854775807 typedef pair<ll,int> pp; typedef struct node { int v; ll w; nod 4000 e(int tv=0,ll tw=0): v(tv),w(tw){}; }node; int n,m; vector<node> edge[MAXM]; ll dis[MAXN],dis2[MAXN]; //dis[i]表示最短路,dis2[i]表示次短路 void solve() { fill(dis+1,dis+n+1,INF); fill(dis2+1,dis2+n+1,INF); priority_queue<pp, vector<pp>, greater<pp> > q; //用优先队列加速搜索 dis[1]=0; q.push(pp(dis[1],1)); //second是该边指向(虽然是无向的)的顶点,first是这条边的权值 while(q.size()) { pp p=q.top(); q.pop(); int v=p.second; ll d=p.first; if(dis2[v]<d) continue; //如果当前取出的值不是到v的最短路或次短路就contniue,因为v->e的最短边和次短边一定是由->v的最短边和次短边+edge(v,e)得到 for(int i=0;i<edge[v].size();i++) { int e=edge[v][i].v; ll d2=d+edge[v][i].w; if(dis[e]>d2) { swap(dis[e],d2); q.push(pp(dis[e],e)); } if(dis2[e]>d2&&d2>dis[v]) //d2>dis[v]防止d2小于dis[v],这样v->e就变成负权边了,但可有可无 { dis2[e]=d2; q.push(pp(dis2[e],e)); } } } printf("%lld\n",dis2 ); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) edge[i].clear(); for(int i=0;i<m;i++) { int a,b; ll c; scanf("%d%d%lld",&a,&b,&c); edge[a].push_back(node(b,c)); edge[b].push_back(node(a,c)); } solve(); } }
相关文章推荐
- HDU 6181 Two Paths 次短路
- HDU 6181 Two Paths【次短路】【模板题】
- HDU --- 6181 Two Paths 2017第十场多校签到题【裸次短路】
- [hdu 6181 Two Paths] Dijkstra求次短路
- hdu 6181 Two Paths (次短路)
- hdu 6181 Two Paths (次短路)
- hdu 6181 Two Paths -最短路条数+次短路 - 多校联盟10
- 2017多校第10场 HDU 6181 Two Paths 次短路
- Hdu 6181 Two Paths【次短路】
- hdu 6181 Two Paths(最短路,spfa)
- 2017 第十场多校训练 HDU 6181 Two Paths 次短路+Dijkstra
- HDU 6181 Two Paths(次短路变形)
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- HDU 6181 Two Paths (次短路)
- hdu 6181 (次短路
- 刷题—— hdu 6181 Two Paths
- hdu 6181 (次短路
- 【hdu 6181】Two Paths
- hdu 6181 (次短路
- hdu 6181 (次短路