其他分享
首页 > 其他分享> > 1381:城市路(Dijkstra)

1381:城市路(Dijkstra)

作者:互联网

城市路

注意:

#include<iostream>
#include<cstring>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;

const int N=2005;
int mapp[N][N],dis[N];
bool vis[N];

int main(){
    int n,m;
    cin>>n>>m;
    memset(mapp,inf,sizeof(mapp));
    while(m--){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        mapp[a][b]=mapp[b][a]=min(mapp[a][b],c);
    }
    for(int i=2;i<=n;i++)
        dis[i]=mapp[1][i];
    memset(vis,false,sizeof(vis));
    vis[1]=true;
    for(int i=1;i<n;i++){
        int minn=inf;
        int k=0;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&minn>dis[j]){
                minn=dis[j];
                k=j;
            }            
        }
        if(!k)break;
        vis[k]=true;
        for(int j=1;j<=n;j++)
            dis[j]=min(dis[j],dis[k]+mapp[k][j]);
    }
    if(dis[n]>=inf)cout<<-1;
    else cout<<dis[n];
    return 0;
}

标签:int,城市,d%,mapp,vis,Dijkstra,inf,include,1381
来源: https://www.cnblogs.com/sxrekord/p/city_road.html