Category:模板
Article From:https://www.cnblogs.com/zyf3855923/p/9127494.html
#include <bits/stdc++.h>
#define maxn 5000005
using namespace std;
struct edge
{
    int u=0;
    int v=0;
    int w=0;
    int next=0;
}edge[2*maxn];
int head[maxn];
int cnt=0;
int n;
void addedge(int u,int v,int w)
{
     edge[cnt].u=u;
     edge[cnt].v=v;
     edge[cnt].w=w;
     edge[cnt].next=head[u];
     head[u]=cnt++;
}
int dist[maxn];
priority_queue < pair<int,int>,vector < pair<int,int> >,greater<pair<int,int> > >q;
void dijkstra(int s,int t)
{
    int i,now;
    for(i=1;i<=n;i++)
    {
        dist[i]=0x7fffffff;
    }
    dist[s]=0;
    q.push(make_pair(0,s));
    while(!q.empty())
    {
        now=q.top().second;
        q.pop();
        for(i=head[now];i!=-1;i=edge[i].next)
        {
            if(dist[now]+edge[i].w<dist[edge[i].v])
            {
                dist[edge[i].v]=dist[now]+edge[i].w;
                q.push(make_pair(dist[edge[i].v],edge[i].v));

            }
        }
    }
}
int main()
{

    int m,t,i;
    scanf("%d%d%d",&n,&m,&t);
    memset(head,-1,sizeof(head));
    for(i=0;i<m;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w);
        addedge(v,u,w);
    }
    dijkstra(1,t);
    cout<<dist[t]<<endl;
    return 0;
}

  

Link of this Article: Dijkstra nlogn Edition

Leave a Reply

Your email address will not be published. Required fields are marked *