fork download
  1. /*
  2.   I think that this problem has a wrong problem statement,
  3.   since it needs to find second shortest path,
  4.   but in a case like there is many shortest path with cost x,
  5.   it wants to print smallest cost > x (even that there is another path to take).
  6.   That's just my opinion in this.
  7.  
  8.   Here is a case:
  9.   3 3
  10.   0 1 1
  11.   1 2 1
  12.   0 2 2
  13.   1
  14.   0 2
  15.  
  16.   We can consider this a shortest path: 0->1->2 with cost 2
  17.   However, this could be second shortest path: 0->2 with cost 2,
  18.   But the intended answer is 4 (0->1->0->2).
  19.  
  20.  
  21.   Solution:
  22.   Get all pairs shortest path using floyd warshall.
  23.   Then for every pair of nodes (a,b), iterate over all edges, check if this
  24. edge is included in the second shortest path or not by calculating
  25. distance between a and first point of this edge + cost of this edge
  26. + distance between second point of this edge and b,
  27. and minimize answers of all edges that yield to a cost bigger than
  28. shortest one.
  29.  
  30.   Complexity: O(N*N*N + N*N*M + Q) for every test case.
  31.  
  32. */
  33.  
  34. #include<bits/stdc++.h>
  35.  
  36. using namespace std;
  37.  
  38. const int MAXN = 105;
  39.  
  40. int dist[MAXN][MAXN];
  41. int secondDist[MAXN][MAXN];
  42. int inf;
  43.  
  44. int main()
  45. {
  46. ios::sync_with_stdio(0);
  47. cin.tie(0);
  48. memset(&inf,0x3f,4);
  49. int T = 0;
  50. int n,m;
  51. while(cin>>n>>m)
  52. {
  53. T++;
  54. cout<<"Set #"<<T<<"\n";
  55. memset(dist,0x3f,sizeof dist);
  56. memset(secondDist,0x3f,sizeof secondDist);
  57. for(int i=0;i<n;i++)
  58. dist[i][i] = 0;
  59. vector<pair<int,int>> edges;
  60. vector<int> costs;
  61. for(int i=0;i<m;i++)
  62. {
  63. int a,b,c;
  64. cin>>a>>b>>c;
  65. dist[a][b] = c;// no multiple edges and no self loops
  66. dist[b][a] = c;
  67. edges.push_back({a,b});
  68. edges.push_back({b,a});
  69. costs.push_back(c);
  70. costs.push_back(c);
  71. }
  72. for(int k=0;k<n;k++)
  73. for(int i=0;i<n;i++)
  74. for(int j=0;j<n;j++)
  75. dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  76.  
  77. for(int i=0;i<n;i++)
  78. for(int j=0;j<n;j++)
  79. {
  80. int mn = inf;
  81. for(int k=0;k<edges.size();k++)
  82. {
  83. int f = edges[k].first;
  84. int t = edges[k].second;
  85. int d = dist[i][f] + costs[k] + dist[t][j];
  86. if(d>dist[i][j])
  87. mn = min(mn,d);
  88. }
  89. secondDist[i][j] = mn;
  90. }
  91. int q;
  92. cin>>q;
  93. while(q--)
  94. {
  95. int f,t;
  96. cin>>f>>t;
  97. assert(secondDist[f][t] >= 0);
  98. if(secondDist[f][t]!=inf)
  99. cout<<secondDist[f][t]<<"\n";
  100. else
  101. cout<<"?\n";
  102. }
  103. }
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 0s 4240KB
stdin
3 3
0 1 1
1 2 1
0 2 2
1
0 2

3 2
0 1 1
1 2 1
1
0 2

4 3
0 1 12
0 2 20
1 2 15
3
0 1
0 2
0 3

4 3
0 1 11
0 2 20
1 2 15
3
0 1
0 2
0 3
stdout
Set #1
4
Set #2
4
Set #3
35
27
?
Set #4
33
26
?