HDU 6181Two Paths——————(求次短路)

妖狐艹你老母 2022-04-12 13:23 188阅读 0赞

Two Paths

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 2139 Accepted Submission(s): 834

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.

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

  1. 2
  2. 3 3
  3. 1 2 1
  4. 2 3 4
  5. 1 3 3
  6. 2 1
  7. 1 2 1

Sample Output

  1. 5
  2. 3
  3. Hint
  4. 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.
  5. For testcase 2, Bob will take route 1 - 2 - 1 - 2 and its length is 3

给定多张无向图,求出每张图中的从s到t的次短路

Input
第一行一个数T,表示数据组数
后面给出T张无向图

Output
对每一张图,给出其次短路


裸的次短路问题
G[i]忘清空了,WA了好多次,
难受。
很难受。

  1. #include<bits/stdc++.h>
  2. #include<vector>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef pair<ll,ll> P;
  6. const ll INF = 1e18;
  7. const int MAXN = 2e5+7;
  8. struct edge{
  9. ll to,cost;
  10. edge(){
  11. };
  12. edge(ll _to, ll _cost)
  13. {
  14. to = _to;
  15. cost = _cost;
  16. }
  17. };
  18. vector<edge> G[MAXN];
  19. int n,m;
  20. ll dist[MAXN];
  21. ll dist2[MAXN];
  22. void slove()
  23. {
  24. priority_queue<P, vector<P>, greater<P> >que;
  25. fill(dist, dist + n + 1, INF);
  26. fill(dist2,dist2+ n + 1, INF);
  27. dist[0] = 0;
  28. que.push(P(0,0));
  29. while(!que.empty())
  30. {
  31. P p = que.top(); que.pop();
  32. ll v = p.second, d = p.first;
  33. if(dist2[v] < d) continue;
  34. for(int i = 0; i < G[v].size(); ++i )
  35. {
  36. edge &e = G[v][i];
  37. ll d2 = d + e.cost;
  38. if(dist[e.to] > d2)
  39. {
  40. swap(dist[e.to], d2);
  41. que.push(P(dist[e.to], e.to));
  42. }
  43. if(dist2[e.to] > d2 && dist[e.to] < d2)
  44. {
  45. dist2[e.to] = d2;
  46. que.push(P(dist2[e.to],e.to));
  47. }
  48. }
  49. }
  50. printf("%lld\n",dist2[n-1]);
  51. }
  52. int main()
  53. {
  54. int t;
  55. scanf("%d",&t);
  56. while(t--)
  57. {
  58. scanf("%d %d",&n,&m);
  59. ll x,y,z;
  60. for(int i=0;i<=n;i++)
  61. G[i].clear();
  62. for(int i=0;i<m;i++)
  63. {
  64. scanf("%lld %lld %lld",&x,&y,&z);
  65. G[x-1].push_back(edge(y-1, z));
  66. G[y-1].push_back(edge(x-1, z));
  67. }
  68. slove();
  69. }
  70. return 0;
  71. }

发表评论

表情:
评论列表 (有 0 条评论,188人围观)

还没有评论,来说两句吧...

相关阅读