畅通工程续

#include<bits/stdc++.h>
using namespace std;

const int Max=200;
const int INF=INT_MAX;

struct Edge {
    int to;
    int l;
    Edge(int x,int y):to(x),l(y) {}
};

struct Point {
    int n;
    int distance;
    Point(int a,int b):n(a),distance(b) {};
    bool operator < (const Point & a) const {
        return distance>a.distance;
    }
};

vector<Edge> graph[Max];
int dis[Max];

void Dijkstra(int s) {
    priority_queue<Point> q;
    dis[s]=0;
    q.push(Point(s,dis[s]));
    while(!q.empty()) {
        int u=q.top().n;
        q.pop();
        for(int i=0; i<graph[u].size(); i++) {
            int v=graph[u][i].to;
            int l=graph[u][i].l;
            if(dis[u]+l<dis[v]) {
                dis[v]=dis[u]+l;
                q.push(Point(v,dis[v]));
            }
        }
    }
    return ;
}

int main() {
    int n,m;
    while(cin>>n>>m) {
        memset(graph,0,sizeof(graph));
        fill(dis,dis+n,INF);
        for(int i=0; i<m; i++) {
            int from,to,l;
            cin>>from>>to>>l;
            graph[from].emplace_back(Edge(to,l));
            graph[to].emplace_back(Edge(from,l));
        }
        int s,t;
        cin>>s>>t;
        Dijkstra(s);
        if(dis[t]==INF) {
            cout<<"-1"<<endl;
        } else {
            cout<<dis[t]<<endl;
        }
    }
    return 0;
}
全部评论
就一个字,牛牛牛啊
点赞 回复 分享
发布于 2022-10-15 15:26 山西

相关推荐

不愿透露姓名的神秘牛友
07-11 11:21
被夸真的超级开心,好可爱的姐姐
码农索隆:老色批们不用脑补了,我把金智妮的图找来了查看图片
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
小叮当411:应该是1-3个月吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务