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

来源:互联网 发布:淘宝一键上传好不好 编辑:程序博客网 时间:2024/05/17 08:10
#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;}