首页 > 其他 > 详细

Dijkstra

时间:2019-04-23 22:08:38      阅读:90      评论:0      收藏:0      [点我收藏+]

HNOI前复习ing QAQ

/*
    Name: Dijkstra
    Author: Jack
    Date: 05/04/19 15:29
    Description:
      1.dist[start]=0,dist[1~n except start]=inf,sign[start]
      2.find x which is unmarked and dist[x]=min{dist[i]}
      3.scan the out edge of node x which is like (x,y,z),
        if(dist[y]>dist[x]+z) dist[y]=dist[x]+z
      4.do 2 and 3 until every node is marked
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5,M=1e7+5;
const int inf=1e9;
struct edge{int y,z;};
vector<edge>e[N];
bool v[N];
int n,m,s,d[N];
priority_queue< pair<int,int> >q;
void add(int x,int y,int z){
    edge tmp1,tmp2;
    tmp1=(edge){y,z};e[x].push_back(tmp1);
}
void dij(int start){
    for(int i=1;i<=n;i++)d[i]=inf;
    memset (v,0,sizeof(v));
    d[start]=0;q.push(make_pair(0,s));
    while(q.size()){
        int x=q.top().second;q.pop();
        if(v[x])continue;v[x]=1;
        for(int i=0;i<e[x].size();i++){
            int y=e[x][i].y,z=e[x][i].z;
            if(d[y]>d[x]+z){
                d[y]=d[x]+z;
                q.push(make_pair(-d[y],y));
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d%c",d[i]==inf? 2147483647:d[i],i==n?'\n':' ');
    }
}
int main(){
    int x,y,z;
    scanf("%d%d%d",&n,&m,&s);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
    }
    dij(s);
    return 0;
}

Dijkstra

原文:https://www.cnblogs.com/yangxuejian/p/10759323.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!