蒟蒻46分求教

P1462 通往奥格瑞玛的道路

MILLOPE @ 2019-05-28 21:15:21

#include <bits/stdc++.h>
using namespace std; 
typedef long long LL; 
const int maxn = 1e4 + 100;
const int maxm = 5e4 + 100; 

inline LL min(LL aa, LL bb) { return aa < bb ? aa : bb; }
inline LL max(LL aa, LL bb) { return aa > bb ? aa : bb; }

template <typename T>
inline void read(T &s) {
    s = 0; 
    T w = 1, ch = getchar(); 
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    s *= w; 
}

template <typename T>
inline void write(T s) {
    T w = 0, ch[15]; 
    if (s < 0) putchar('-'), s = -s; 
    while (s) { ch[++w] = s % 10 + 48; s /= 10; }
    while (w) { putchar(ch[w--]); } 
}

int n, m, b, tot; 
int lin[maxn];
LL l ,r; 
LL val[maxn], dis[maxn];
bool vis[maxn];  
struct node { int next, to; LL dis; } edge[maxm << 1]; 
queue <int> q; 

inline void add(int from, int to, LL dis) {
    edge[++tot].to = to; 
    edge[tot].dis = dis; 
    edge[tot].next = lin[from]; 
    lin[from] = tot; 
}

bool spfa(LL k) {
    memset(dis, 0x3f, sizeof(dis)); 
    memset(vis, false, sizeof(vis)); 
    q.push(1); 
    vis[1] = true; 
    dis[1] = 0; 
    while (!q.empty()) {
        int x = q.front(); q.pop(); vis[x] = false; 
        for (int i = lin[x]; i; i = edge[i].next) {
            int y = edge[i].to; 
            if (dis[y] > dis[x] + edge[i].dis) {
                dis[y] = dis[x] + edge[i].dis; 
                if (!vis[y] && val[y] <= k) {
                    q.push(y); 
                    vis[y] = true; 
                }
            }
        }
    }
    if (dis[n] >= b) return false; 
    else return true; 
}

int main() { 
    read(n), read(m), read(b);
    for (int i = 1; i <= n; ++i) {
        read(val[i]);
        r = max(r, val[i]); 
    }
    l = max(val[1], val[n]);  
    for (int i = 1; i <= m; ++i) { 
        int x, y, z; 
        if (x == y) continue; 
        read(x), read(y), read(z); 
        add(x, y, z); 
        add(y, x, z); 
    } 
    if (!spfa(1000010000)) { 
        puts("AFK"); 
        return 0;  
    } 
    else { 
        while (l <= r) { 
            int mid = (l + r) / 2; 
            if (spfa(mid)) r = mid - 1; 
            else l = mid + 1; 
        } 
        write(l); puts("");  
    }
    return 0;  
}

|