HDU 3986 Harry Potter and the Final Battle

Harry Potter and the Final Battle

题意

1.求删除一条边后的最短路中的最大值

题解

1.建任意一棵最小路径树,边权等于长度
2.树上1->n的链标记
3.枚举链上一条边被删除
4.dj求最小值
5.所有最小值中取max
6.复杂度:$nmlogm$

具体代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int>P;
const int M=1005;
int n,m;
int asdf,head[M];
bool mark[M*100];
struct edge {
    int to,cost,nxt,id;
} G[M*100];
void add_edge(int a,int b,int c,int d) {
    G[++asdf].to=b;
    G[asdf].nxt=head[a];
    G[asdf].cost=c;
    G[asdf].id=d;
    head[a]=asdf;
}
int dis[M],fa[M],cost[M],mi[M];
void dj(int S) {
    priority_queue<P,vector<P>,greater<P> >Q;
    for(int i=1; i<=n; i++) {
        dis[i]=1e9;
    }
    Q.push(P(dis[S]=0,S));
    while(!Q.empty()) {
        P p=Q.top();
        Q.pop();
        int x=p.second;
        if(p.first>dis[x])continue;
        for(int i=head[x]; i; i=G[i].nxt) {
            int y=G[i].to;
            if(dis[y]>dis[x]+G[i].cost) {
                dis[y]=dis[x]+G[i].cost;
                fa[y]=x;
                cost[y]=G[i].id;
                Q.push(P(dis[y],y));
            }
        }
    }
}
void Dj(int S) {
    priority_queue<P,vector<P>,greater<P> >Q;
    for(int i=1; i<=n; i++) {
        dis[i]=1e9;
    }
    Q.push(P(dis[S]=0,S));
    while(!Q.empty()) {
        P p=Q.top();
        Q.pop();
        int x=p.second;
        if(p.first>dis[x])continue;
        for(int i=head[x]; i; i=G[i].nxt) {
            if(mark[G[i].id])continue;
            int y=G[i].to;
            if(dis[y]>dis[x]+G[i].cost) {
                dis[y]=dis[x]+G[i].cost;
                Q.push(P(dis[y],y));
            }
        }
    }
}
int main() {
    int cas;
    int a,b,c;
    scanf("%d",&cas);
    while(cas--) {
        scanf("%d %d",&n,&m);
        asdf=0;
        for(int i=1; i<=n; i++)head[i]=0,mi[i]=1e9,mark[i]=0;
        for(int i=1; i<=m; i++) {
            scanf("%d %d %d",&a,&b,&c);
            add_edge(a,b,c,i);
            add_edge(b,a,c,i);
        }
        dj(1);
        int now=n,ans=0;
        while(now!=1) {
            mark[cost[now]]=1;
            Dj(1);
            ans=max(ans,dis[n]);
            mark[cost[now]]=0;
            now=fa[now];
        }
        printf("%d\n",ans==1e9?-1:ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值