洛谷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\n", dis[t]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Benjamin-cpp/p/10408895.html