Two Paths

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


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
2 3 3 1 2 1 2 3 4 1 3 3 2 1 1 2 1
 

 

Sample Output
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
 

 

Source
 
题解:无向图求第k短路(非次短路) 模板
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<map>
  7 #include<queue>
  8 #include<stack>
  9 #include<vector>
 10 #include<set>
 11 using namespace std;
 12 typedef long long ll;
 13 typedef pair<ll,int> P;
 14 const int maxn=2e5+100,maxm=2e5+100,inf=0x3f3f3f3f,mod=1e9+7;
 15 const ll INF=1e17+7;
 16 struct edge
 17 {
 18     int from,to;
 19     ll w;
 20 } pre[maxn];
 21 vector<edge>G[maxn],T[maxn];
 22 priority_queue<P,vector<P>,greater<P> >q;
 23 ll dist[maxn];
 24 
 25 void addedge(int u,int v,ll w)
 26 {
 27     G[u].push_back((edge)
 28     {
 29         u,v,w
 30     });
 31     T[v].push_back((edge)
 32     {
 33         v,u,w
 34     });
 35 }
 36 void dij(int s)
 37 {
 38     dist[s]=0LL;
 39     q.push(P(dist[s],s));
 40     while(!q.empty())
 41     {
 42         P p=q.top();
 43         q.pop();
 44         int u=p.second;
 45         for(int i=0; i<T[u].size(); i++)
 46         {
 47             edge e=T[u][i];
 48             if(dist[e.to]>dist[u]+e.w)
 49             {
 50                 dist[e.to]=dist[u]+e.w;
 51                 q.push(P(dist[e.to],e.to));
 52             }
 53         }
 54     }
 55 }
 56 struct node
 57 {
 58     int to;
 59     ///g(p)为当前从s到p所走的路径的长度;dist[p]为点p到t的最短路的长度;
 60     ll g,f;///f=g+dist,f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;
 61     bool operator<(const node &x ) const
 62     {
 63         if(x.f==f) return x.g<g;
 64         return x.f<f;
 65     }
 66 };
 67 
 68 ll A_star(int s,int t,int k)
 69 {
 70     priority_queue<node>Q;
 71     if(dist[s]==INF) return -1;
 72     int cnt=0;
 73     if(s==t) k++;
 74     ll g=0LL;
 75     ll f=g+dist[s];
 76     Q.push((node)
 77     {
 78         s, g, f
 79     });
 80     while(!Q.empty())
 81     {
 82         node x=Q.top();
 83         Q.pop();
 84         int u=x.to;
 85         if(u==t) cnt++;
 86         if(cnt==k) return x.g;
 87         for(int i=0; i<G[u].size(); i++)
 88         {
 89             edge e=G[u][i];
 90             ll g=x.g+e.w;
 91             ll f=g+dist[e.to];
 92             Q.push((node)
 93             {
 94                 e.to, g, f
 95             });
 96         }
 97     }
 98     return -1;
 99 }
100 void init(int n)
101 {
102     for(int i=0; i<=n+10; i++) G[i].clear(),T[i].clear();
103 }
104 int main()
105 {
106     int n,m;
107     int t;
108     scanf("%d",&t);
109     while(t--){
110     scanf("%d%d",&n,&m);
111     for(int i=1; i<=m; i++)
112     {
113         int u,v;
114         ll w;
115         scanf("%d%d%lld",&u,&v,&w);
116         addedge(u,v,w);
117         addedge(v,u,w);
118     }
119     for(int i=0; i<=n; i++) dist[i]=INF;
120     dij(n);
121     printf("%lld\n",A_star(1,n,2));
122     init(n);
123     }
124     return 0;
125 }