RE + TLE求助

P1462 通往奥格瑞玛的道路

bryce @ 2022-08-04 08:26:41

#include<iostream>
#include<stdio.h>
#include<climits>
#include<queue>
#define N 10001
#define M 50001
#define INF LLONG_MAX

using namespace std;

inline long long read(){register long long x = 0, f = 1;register char c = getchar();while (c < '0' || c > '9'){if (c == '-') f = -1;c = getchar();}while (c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + c ^ 48;c = getchar();}return x * f;}
inline void write(long long x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}

long long n, m, b;
long long f[N];
long long head[N], cnt;

struct edge{
    long long v, w, nxt;
}e[M << 1];

void add(long long u, long long v, long long w){
    cnt++;
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt;
}

struct node{
    long long u, d;
    bool operator < (const node &x) const{
        return x.d < d;
    }
};
priority_queue<node> q;
long long dis[N];
bool vis[N];

bool check(long long x){
    for (long long i = 1; i <= n; i++){
        dis[i] = INF;
        vis[i] = 0;
    }
    dis[1] = 0;
    q.push((node){1, 0});
    if (f[1] > x){
        return 0;
    }
    while (!q.empty()){
        node t = q.top();
        long long u = t.u, d = t.d;
        q.pop();
        if (d != dis[u]){
            continue;
        }
        if (vis[u]){
            continue;
        }
        vis[u] = 1;
        for (long long i = head[u]; i; i = e[i].nxt){
            long long v = e[i].v;
            if (f[v] <= x){
                if (dis[u] + e[i].w < dis[v]){
                    dis[v] = dis[u] + e[i].w;
                    if (!vis[v]){
                        q.push((node){v, dis[v]});
                    }
                }
            }
        }
    }
    return dis[n] <= b;
}

int main(){
    n = read(); m = read(); b = read();
    for (long long i = 1; i <= n; i++){
        f[i] = read();
    }
    for (long long i = 1; i <= m; i++){
        long long u, v, w;
        u = read(); v = read(); w = read();
        add(u, v, w);
        add(v, u, w);
    }
    long long l = 1, r = 1000000000;
    while (l < r){
        long long mid = (l + r) >> 1;
        if (check(mid)){
            r = mid;
        }else{
            l = mid + 1;
        }
    }
    if (check(l)){
        write(l);
        putchar('\n');
    }else{
        puts("AFK\n");
    }
    return 0;
}

|