Two Paths
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.
//http://blog.csdn.net/qq_37025443/article/details/77541137 这里有一些讲解,不钻牛角尖了,会用就行,最近要补题了
就是让求次短路,从网上学来的模板 (加油,能多学就多学嘛,反正又没坏处
#include<bits/stdc++.h> using namespace std; int n,m; typedef long long ll; typedef pair<ll,int> pli;//前dis 后node ll dis1[100100],dis2[100100]; struct node{ int to,cost; node(int t,int c):to(t),cost(c){} }; vector<node>G[100100]; void solve() { priority_queue<pli,vector<pli>,greater<pli> > Q; dis1[1] =0; Q.push({0,1}); while (!Q.empty()) { pli now = Q.top(); Q.pop(); int v = now.second; ll d1 = now.first; if(dis2[v] < d1) //说明这个值 太大 不需要更新了 continue; for(int i=0;i<G[v].size();i++) { node e = G[v][i]; int to = e.to; ll d2 = d1+e.cost; if(d2 < dis1[to]) { swap(d2,dis1[to]); Q.push({dis1[to],to}); } if(d2 <dis2[to]&& d2 > dis1[to]) { swap(d2,dis2[to]); Q.push({dis2[to],to}); } } } cout<<dis2[n]<<endl; } int main () { int t; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&m); for(int i=0;i<=n;i++) { dis1[i]=dis2[i]=1e18; G[i].clear(); } for(int i=1;i<=m;i++) { int x,y,cost; scanf("%d %d %d",&x,&y,&cost); G[x].push_back({y,cost}); G[y].push_back({x,cost}); } solve(); } }