您的位置:首页 > 其它

Hdu 6181 Two Paths【次短路】

2017-08-24 21:51 429 查看

Two Paths

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)

Total Submission(s): 229    Accepted Submission(s): 134


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each test case print length of valid shortest path in one line.
 

[align=left]Sample Input[/align]

2
3 3
1 2 1
2 3 4
1 3 3
2 1
1 2 1

 

[align=left]Sample Output[/align]

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

题目大意:

求从点1到点n的次短路。

思路:

这里我们O(2*SPFA+m)去暴力找次短路即可。

我们做一个从起点的单源最短路,设定为D【j】,我们再做一个从终点出发的最短路,设定为F【j】;

那么每一次我们枚举出来一条边,使得维护出来D【u】+F【v】+w(u,v)的次小值就是次短路长度。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
ll vis[150000];
ll head[150000];
struct EdgeNode
{
ll from;
ll to;
ll w;
ll next;
} e[550000];
ll dis[2][250000];
ll n,m,cont;
void add(ll from,ll to,ll w)
{
e[cont].from=from;
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA(ll ss,ll d)
{
for(ll i=1; i<=n; i++)dis[d][i]=1000000000000000000;
dis[d][ss]=0;
queue<ll >s;
memset(vis,0,sizeof(vis));
vis[ss]=1;
s.push(ss);
while(!s.empty())
{
ll u=s.front();
s.pop();
vis[u]=0;
for(ll i=head[u]; i!=-1; i=e[i].next)
{
ll v=e[i].to;
ll w=e[i].w;
if(dis[d][v]>dis[d][u]+w)
{
dis[d][v]=dis[d][u]+w;
if(vis[v]==0)
{
s.push(v);
vis[v]=1;
}
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&m);
cont=0;
memset(head,-1,sizeof(head));
for(ll i=0; i<m; i++)
{
ll x,y,w;
scanf("%I64d%I64d%I64d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
SPFA(1,0);
SPFA(n,1);
ll minn;
ll ciminn;
minn=ciminn=1000000000000000000;
for(ll i=0; i<cont; i++)
{
ll tmp;
ll w=e[i].w+dis[0][e[i].from]+dis[1][e[i].to];
if(w<minn)
{
tmp=minn;
minn=w;
w=tmp;
}
if(w<ciminn&&w!=minn)
{
ciminn=min(ciminn,w);
}
}
printf("%I64d\n",ciminn);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hdu 6181