HDU 6181 第k短路
2017-08-25 14:41
357 查看
Two Paths
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 525 Accepted Submission(s): 265
[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
[align=left]Source[/align]
2017 Multi-University Training Contest - Team 10
题解:无向图求第k短路(非次短路) 模板
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> #include<set> using namespace std; typedef long long ll; typedef pair<ll,int> P; const int maxn=2e5+100,maxm=2e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll INF=1e17+7; struct edge { int from,to; ll w; } pre[maxn]; vector<edge>G[maxn],T[maxn]; priority_queue<P,vector<P>,greater<P> >q; ll dist[maxn]; void addedge(int u,int v,ll w) { G[u].push_back((edge) { u,v,w }); T[v].push_back((edge) { v,u,w }); } void dij(int s) { dist[s]=0LL; q.push(P(dist[s],s)); while(!q.empty()) { P p=q.top(); q.pop(); int u=p.second; for(int i=0; i<T[u].size(); i++) { edge e=T[u][i]; if(dist[e.to]>dist[u]+e.w) { dist[e.to]=dist[u]+e.w; q.push(P(dist[e.to],e.to)); } } } } struct node { int to; ///g(p)为当前从s到p所走的路径的长度;dist[p]为点p到t的最短路的长度; ll g,f;///f=g+dist,f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远; bool operator<(const node &x ) const { if(x.f==f) return x.g<g; return x.f<f; } }; ll A_star(int s,int t,int k) { priority_queue<node>Q; if(dist[s]==INF) return -1; int cnt=0; if(s==t) k++; ll g=0LL; ll f=g+dist[s]; Q.push((node) { s, g, f }); while(!Q.empty()) { node x=Q.top(); Q.pop(); int u=x.to; if(u==t) cnt++; if(cnt==k) return x.g; for(int i=0; i<G[u].size(); i++) { edge e=G[u][i]; ll g=x.g+e.w; ll f=g+dist[e.to]; Q.push((node) { e.to, g, f }); } } return -1; } void init(int n) { for(int i=0; i<=n+10; i++) G[i].clear(),T[i].clear(); } int main() { int n,m; int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { int u,v; ll w; scanf("%d%d%lld",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } for(int i=0; i<=n; i++) dist[i]=INF; dij(n); printf("%lld\n",A_star(1,n,2)); init(n); } return 0; }
相关文章推荐
- HDU 6181 Two Paths 次短路
- hdu 6181 (次短路
- HDU 6181 Two Paths【次短路】【模板题】
- hdu 6181 (次短路
- hdu 6181 Two Paths (次短路)
- hdu 6181 (次短路
- HDU --- 6181 Two Paths 2017第十场多校签到题【裸次短路】
- hdu 6181 Two Paths -最短路条数+次短路 - 多校联盟10
- [hdu 6181 Two Paths] Dijkstra求次短路
- hdu 6181 (次短路
- hdu 6181 Two Paths (次短路)
- hdu 6181 (次短路
- hdu 6181 Two Paths(最短路,spfa)
- 2017 Multi-University Training Contest 10 1011 Two Paths HDU 6181 (次短路+最短路数量)
- hdu 6181 (次短路
- hdu 6181 (次短路
- hdu 6181 (次短路
- hdu 6181 (次短路
- Hdu 6181 Two Paths【次短路】
- hdu 6181 (次短路