Two Paths
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 842 Accepted Submission(s): 396
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
Source
双向图,求出第二短路,可以允许正权回路(重复走)。
求次短路:
如果dist[v]表示s->v的最短距离,dist2[v]表示s->v的次短距离,d为s->v的第k短距离(k>1)。那么一定满足这样一个关系,dist[v] < dist2[v] <= k。看到这个等式的时候我们可以发现,如果dist2[v] > d2 > dist[v],显然这时候我们需要将dist2[v]更新为d2。那么我们只要找到不满足dist[v] < dist2[v] <= k这个的式子的dist2[v],那么我们就更新他。一直更新到所以式子都满足这个式子,那么dist2[v]就为源点s->v的次短路。
刚学的模板用上
#include <cstdio>
#include <utility>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<LL,int> pli;
int n,m;
LL dist1[100100],dist2[100100];
struct node
{
int to,cost;
node(int t,int c)
{
to=t;
cost=c;
}
};
vector<node>G[100100];
void solve()
{
//priority_queue<int,vector<int>,greater<int> > Q;
priority_queue<pli,vector<pli>,greater<pli> > Q;///从小到大优先
dist1[1] =0;
Q.push( {0,1});
while (!Q.empty())
{
pli now = Q.top();
Q.pop();
int v = now.second;
LL d1 = now.first;
if(dist2[v] < d1) //说明这个值 太大 不需要更新了
continue;
int kk=0;
for(int i=0; i<G[v].size(); i++)///遍历当前点的所有连接点
{
node e = G[v][i];///随机找一个点e
int to = e.to;
//cout<<"to="<<to<<endl;
LL d2 = d1+e.cost;///更新d2值
//cout<<d2<<endl;
if(d2 < dist1[to])///如果有d2比最短路还要小,更新最短路
{
swap(d2,dist1[to]);
Q.push( {dist1[to],to});
}
if(d2 <dist2[to]&& d2 > dist1[to])///更新次短路
{
swap(d2,dist2[to]);
Q.push( {dist2[to],to});
}
}
}
cout<<dist2[n]<<endl;
}
void init()
{
for(int i=0;i<=n;i++)
{
G[i].clear();
dist1[i]=dist2[i]=1e18;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
init();
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
G[u].push_back({v,w});
G[v].push_back({u,w});
}
solve();
}
}