hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
来源:互联网 发布:unity3d jpg 编辑:程序博客网 时间:2024/06/05 10:07
Two Paths
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 378 Accepted Submission(s): 206
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.
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.
23 31 2 12 3 41 3 32 11 2 1
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
求与 一条最短路径长度 不同的路径 的最短长度
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N= 1e5+10;typedef long long LL;struct node{ int to, next; LL w;} p[N*4];int head[N], cnt;void init(){ memset(head,-1,sizeof(head)); cnt=0; return ;}void add(int u,int v,LL w){ p[cnt].to=v,p[cnt].next=head[u],p[cnt].w=w; head[u]=cnt++; p[cnt].to=u,p[cnt].next=head[v],p[cnt].w=w; head[v]=cnt++; return ;}LL dist[N][2], vis[N][2];struct node1{ int v, mark; LL w; bool operator <(const node1 &A)const { if(w!=A.w) return w>A.w; return v>v; }};priority_queue <node1>q;LL dij(int s,int t){ memset(dist,-1,sizeof(dist)); memset(vis,0,sizeof(vis)); while(!q.empty())q.pop(); dist[s][0]=0; node1 tmp; tmp.v=s,tmp.w=0,tmp.mark=0; q.push(tmp); while(!q.empty()) { tmp=q.top();q.pop(); int u=tmp.v; LL w=tmp.w; if(vis[u][tmp.mark]) continue; vis[u][tmp.mark]=1; for(int i=head[u]; i!=-1; i=p[i].next) { int v=p[i].to; if(dist[v][0]==-1||dist[v][0]>w+p[i].w) { dist[v][1]=dist[v][0]; dist[v][0]=w+p[i].w; node1 x; x.v=v,x.w=dist[v][0],x.mark=0; q.push(x); } else if(dist[v][1]==-1||dist[v][1]>w+p[i].w) { dist[v][1]=w+p[i].w; node1 x; x.v=v,x.w=dist[v][1],x.mark=1; q.push(x); } } } return dist[t][1];}int main(){ int t, ncase=1; scanf("%d", &t); while(t--) { int n, m; scanf("%d %d", &n, &m); init(); for(int i=0; i<m; i++) { int x, y; LL w; scanf("%d %d %lld", &x, &y, &w); add(x,y,w); } printf("%lld\n", dij(1,n)); } return 0;}
Description
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersectionN.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Lines 2..R+1: Each line contains three space-separated integers: A,B, and D that describe a road that connects intersections A andB and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
求次短路长度(与最短路长度不同)
#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const int N= 1e5+10;typedef long long LL;struct node{ int to, next; LL w;} p[N*4];int head[N], cnt;void init(){ memset(head,-1,sizeof(head)); cnt=0; return ;}void add(int u,int v,LL w){ p[cnt].to=v,p[cnt].next=head[u],p[cnt].w=w; head[u]=cnt++; p[cnt].to=u,p[cnt].next=head[v],p[cnt].w=w; head[v]=cnt++; return ;}LL dist1[N], dist2[N], vis[N];typedef pair<LL,int>pi;priority_queue <pi,vector<pi>,greater<pi> >q;LL dij(int s,int t){ memset(dist1,-1,sizeof(dist1)); memset(dist2,-1,sizeof(dist2)); memset(vis,0,sizeof(vis)); while(!q.empty())q.pop(); dist1[s]=0; q.push(make_pair(0,s)); while(!q.empty()) { pi tmp=q.top();q.pop(); int u=tmp.second; LL w=tmp.first; if(dist2[u]!=-1&&dist2[u]<w) continue; for(int i=head[u]; i!=-1; i=p[i].next) { int v=p[i].to; LL d=w+p[i].w; if(dist1[v]==-1||dist1[v]>d) { swap(dist1[v],d); q.push(make_pair(dist1[v],v)); } if((d!=-1&&dist1[v]<d)&&(dist2[v]==-1||dist2[v]>d)) { dist2[v]=d; q.push(make_pair(dist2[v],v)); } } } return dist2[t];}int main(){ int t, ncase=1; int n, m; while(scanf("%d %d", &n, &m)!=EOF) { init(); for(int i=0; i<m; i++) { int x, y; LL w; scanf("%d %d %lld", &x, &y, &w); add(x,y,w); } printf("%lld\n", dij(1,n)); } return 0;}
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- POJ 3255 Roadblocks(Dijstra 求次短路长度)
- POJ, 3255 Roadblocks(次短路径)
- hdu 6181 Two Paths (次短路)
- POJ 3255 Roadblocks (次短路径 + Dijkstra算法)
- POJ 题目3255Roadblocks(次短路)
- POJ 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks(次短路)
- poj 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks (次短路问题)
- POJ 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks --次短路径
- HDU 6181 Two Paths(次短路变形)
- poj3255 Roadblocks (次短路径问题)
- POJ 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks 次短路
- POJ 3255 Roadblocks 次短路
- POJ 3255 Roadblocks 次短路
- windows10升级网络连接校园网的配置
- Android:微信和支付宝的APP支付接入
- jQueryEasyUI Messager基本使用
- 你真的理解了compileSdkVersion,minSdkVersion,targetSdkVersion含义了吗?
- 深度学习在推荐领域的应用:Lookalike 算法
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- maven+ssm整合配置文件
- Java的就业前景如何?千锋教育怎么样?
- 利用shell脚本控制树莓派GPIO
- 小程序-回调函数
- MTK SDK5020编译流程
- 高级包实现python服务器
- elasticsearch双实例部署
- Android学习-RecyclerView默认scrollbar的相关属性