Two Paths
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 235 Accepted Submission(s): 137
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.
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.
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 3HintFor 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
用两个数组, dis1[] 和 dis2[] dis1记录最短路,dis2记录次短路,在Dijkstra 记录最短路的同时,只要计算的距离
满足 dis2[v] > dis > dis1[v] ,那就把 dis2[v] 更新为 dis ,就这样一遍计算最短路一遍记录次短路就ok
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
#define maxn 100005
typedef pair<ll,ll> p;
const ll inf = 1000000000100100;
struct node{
ll to,val;
node(ll x,ll y):
to(x),val(y) {}
};
vector<node>e[maxn];
vector<node>len;
ll n,m,dis1[maxn],dis2[maxn];
void dijkstra(){
priority_queue<p,vector<p>,greater<p> >q;
for(int i = 1;i <= n;i++){
dis1[i] = dis2[i] = inf;
}
dis1[1] = 0;
q.push(p(dis1[1],1));
while(!q.empty()){
ll now = q.top().first;
ll to = q.top().second;
// printf("q top = %lld %lld\n",q.top().first,q.top().second);
q.pop();
if(dis2[to] < now)
continue;
for(int i = 0;i < e[to].size();i++){
ll t = e[to][i].to;
ll v = e[to][i].val;
ll len = now + v;
if(dis1[t] > len){
swap(len,dis1[t]);
q.push(p(dis1[t],t));
}
if(dis2[t] > len && dis1[t] < len){
dis2[t] = len;
q.push(p(dis2[t],t));
}
}
}
// printf("%lld\n",dis1[n]);
printf("%lld\n",dis2[n]);
}
void add(ll u,ll v,ll w){
e[u].push_back(node(v,w));
e[v].push_back(node(u,w));
}
void init(){
for(int i = 0;i < maxn;i++){
e[i].clear();
}
len.clear();
}
int main() {
int t;
ll u,v,w;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
init();
for(int i = 1;i <= m;i++){
scanf("%lld %lld %lld",&u,&v,&w);
add(u,v,w);
}
dijkstra();
}
return 0;
}