题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181
题意是有两个人比赛,第一个人一定会走最短路,问第二个人最短走多远(不能与最短路路径完全相同)。
其实就是一个次短路问题,我用的dijkstra 链式前向星...
AC代码:
代码语言:javascript复制#include <bits/stdc .h>
#define maxn 500005
#define inf 1e18
#define ll long long
using namespace std;
struct Node{
ll to,next,w;
bool operator < (const Node &a) const{
return a.w < w;
}
}Edge[maxn],Now,Next;
int head[maxn],num;
ll d1[maxn],d2[maxn];
int T,n,m;
void init(){
memset(head,-1,sizeof(head));
for(int i=0;i<=n;i ){
d1[i] = inf;
d2[i] = inf;
}
num = 0;
}
void add(ll u,ll v,ll w){
Edge[num].w = w;
Edge[num].to = v;
Edge[num].next = head[u];
head[u] = num ;
}
void dijkstra(int s){
d1[s] = 0;
Now.to = s;
priority_queue<Node> q;
q.push(Now);
while(!q.empty()){
ll v = q.top().to;
ll xx = q.top().w;
q.pop();
if(d2[v] < xx) continue;
for(int i=head[v];i!=-1;i=Edge[i].next){
ll u = Edge[i].to;
ll w = Edge[i].w xx;
if(d1[u] > w){
swap(d1[u], w);
Next.w = d1[u];
Next.to = u;
q.push(Next);
}
if(d2[u] > w && d1[u] < w){
d2[u] = w;
Next.w = d2[u];
Next.to = u;
q.push(Next);
}
}
}
printf("%lldn",d2[n]);
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i ){
ll u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
add(u, v, w);
add(v, u, w);
}
dijkstra(1);
}
return 0;
}