hdu6181 2017多校最短路(复习了一拨儿次短路)

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 5000+50;
const int maxm = 100000+10;
const int inf = 5000*5000;
int n;
struct node
{
    int to;
    int next;
    int w;
    node(){}
    node(int a,int b,int c):to(a),next(b),w(c){}
}edge[maxm<<1];
struct Edge
{
    int id;
    int dist;
    Edge(int a,int b):id(a),dist(b){}
    bool operator < (const Edge& T) const{
        return dist>T.dist;
    }
};
int tot,head[maxn];
int dist1[maxn]; int dist2[maxn];
void add_edge(int a,int b,int c)
{
    edge[tot] = node(b,head[a],c);
    head[a] = tot++;
}
int dij()
{
    for(int i=0;i<=n;i++) dist1[i] = inf,dist2[i] = inf;
    priority_queue<Edge>q;
    dist1[1] = 0;
    q.push(Edge(1,0));
    while(!q.empty())
    {
        Edge fr = q.top() ; q.pop();
        int u = fr.id;
        if(dist2[u]<fr.dist) continue;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v = edge[i].to;
            int d = fr.dist + edge[i].w;
            if(dist1[v]>d)
            {
                swap(dist1[v],d);
                q.push(Edge(v,dist1[v]));
            }
            if(dist1[v]<d&&dist2[v]>d)
            {
                dist2[v] = d;
                q.push(Edge(v,dist2[v]));
            }
        }
    }
    return dist2[n];
}
int main()
{
    int m,u,v,w;
    scanf("%d%d",&n,&m);
    tot = 0; memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        add_edge(u,v,w); add_edge(v,u,w);
    }
    printf("%d\n",dij());
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值