diji

#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
struct Edge
{
    int v,w,next;
} edge[maxn];
int head[maxn];
int cnt;
int n,m;
void addedge(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
bool vis[maxn];
int dist[maxn];
void Dijkstra(int s)
{
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
    memset(dist,-1,sizeof(dist));
    dist[s]=0;
    q.push(make_pair(0,s));
    while(!q.empty())
    {
        int u=q.top().second;
        q.pop();
        if(vis[u]==true) continue;
        vis[u]=true;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            int w=edge[i].w;
            if((dist[v]>dist[u]+w||dist[v]==-1)&&!vis[v])
            {
                dist[v]=dist[u]+w;
                q.push(make_pair(dist[v],v));
            }
        }
    }
}
int main()
{
    int st,en;
    scanf("%d%d%d%d",&n,&m,&st,&en);
    memset(head,-1,sizeof(head));
    for(int i=1; i<=m; i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        addedge(x,y,z);
        addedge(y,x,z);
    }
   Dijkstra(st);
    printf("%d\n",dist[en]);
    return 0;
}

  

posted @ 2018-10-06 10:43  勿忘安己  阅读(226)  评论(0编辑  收藏  举报