• 洛谷P1396 营救


    稍作修改的最短路

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    //Mystery_Sky
    //
    #define maxn 1000010
    #define maxm 5000050
    struct Road{
        int to;
        int next;
        int w;
    }road[maxn];
    int head[maxn], cnt, dis[maxn];
    int vis[maxn];
    //int ans[maxm];
    int n, m, s, t;
    inline void add_edge(int u, int v, int w)
    {
        road[++cnt].to = v;
        road[cnt].w = w;
        road[cnt].next = head[u];
        head[u] = cnt;
    }
    
    struct node{
        int dis;
        int pos;
        bool operator <(const node &x) const {
            return x.dis < dis;
        }
    };
    
    priority_queue <node> q;
    inline void dijkstra()
    {
        dis[s] = 0;
    //     ans[s] = 0;
        q.push((node) {0, s});
        while(!q.empty()) {
            node top = q.top();
            q.pop();
            int x = top.pos, d = top.dis;
            if(vis[x]) continue;
            vis[x] = 1;
            for(int i = head[x]; i; i = road[i].next) {
                int y = road[i].to;
                int dist = max(dis[x], road[i].w);//不同之处
                if(dis[y] > dist) {
                    dis[y] = dist;
                    if(!vis[y]) {
                        q.push((node) {dis[y], y}); 
                    }
                }
            }
        } 
    }
    
    int main() {
    //    memset(ans, -1, sizeof(ans));
        scanf("%d%d%d%d", &n, &m, &s, &t);
        for(int i = 1; i <= n; i++) dis[i] = 0x3f3f3f3f;
        for(int i = 1; i <= m; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            add_edge(u, v, w);
            add_edge(v, u, w);
        }
        dijkstra();
        printf("%d
    ", dis[t]);
        return 0;
    }
    唯愿,青春不辜负梦想,未来星辰闪耀
  • 相关阅读:
    c#之字段,属性,索引器,常量
    c#类,对象,类成员简介
    c#之接口,依赖反转,单元测试
    c#之 抽象类与开闭原则
    c#重写与多态
    c#之类
    c#之类的继承,类成员的访问控制
    c#之委托
    c# try catch用法思路
    js的全局变量
  • 原文地址:https://www.cnblogs.com/Benjamin-cpp/p/10408895.html
Copyright © 2020-2023  润新知