DIJ(次短路) - Two Paths - HDU 6181
题意:
输入:
输出:
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
数据范围:
分析:
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define ll long long
#define P pair<ll,int>
#define x first
#define y second
using namespace std;
const int N=1e5+10,M=2e5+10;
int n,m;
int st[N];
int e[M],ne[M],h[N],idx;
ll w[M];
ll dis[2][N];
void add(int a,int b,ll c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
ll dijkstra()
{
memset(dis,0x3f,sizeof dis);
memset(st,0,sizeof st);
dis[0][1]=0;
priority_queue<P,vector<P>,greater<P>> heap;
heap.push({0,1});
while(heap.size())
{
P t=heap.top();
heap.pop();
int ver=t.y;
ll d=t.x;
st[ver]++;
for(int i=h[ver];~i;i=ne[i])
{
int j=e[i];
if(st[j]==2) continue;
if(dis[0][j]>d+w[i])
{
dis[1][j]=dis[0][j];
dis[0][j]=d+w[i];
heap.push({dis[0][j],j});
}
else if(dis[1][j]>d+w[i])
{
dis[1][j]=d+w[i];
heap.push({dis[1][j],j});
}
}
}
return dis[1][n];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(h,-1,sizeof h);
idx=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b;
ll c;
scanf("%d%d%lld",&a,&b,&c);
add(a,b,c), add(b,a,c);
}
printf("%lld\n",dijkstra());
}
return 0;
}