80分求助

P1462 通往奥格瑞玛的道路

Annie07 @ 2023-10-03 10:14:41

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 1e4 + 5, M = 1e5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Edge{
    ll to, next, val;
}a[M];
ll n, m, tot, f[N], maxn;
ll head[N], cnt, vis[N], dis[N];
void AddEdge(ll x, ll y, ll z) {
    cnt++;
    a[cnt].to = y, a[cnt].val = z;
    a[cnt].next = head[x], head[x] = cnt;
}
struct node{
    ll num, val;
    bool operator < (const node &A) const{
        return val > A.val;
    }
};
bool Dijkstra(ll r) {
    memset(vis, 0, sizeof(vis));
    priority_queue<node> q;
    for (ll i = 1; i <= n; i++) dis[i] = INF;
    dis[1] = 0;
    q.push((node){1, 0});
    if (f[1] > r) return 0;
    while(!q.empty()) {
        node now = q.top(); q.pop();
        ll u = now.num, d = now.val;
        if (vis[u]) continue;
        vis[u] = 1;
        for (ll i = head[u]; i; i = a[i].next) {
            ll v = a[i].to, w = a[i].val;
            if (d + w < dis[v] && f[v] <= r) {
                dis[v] = d + w;
                q.push((node){v, d + w});
            }
        }
    }
    if (dis[n] <= tot) return 1;
    return 0; 
}
ll l, r, mid, ans = -1;
int main() {
    cin >> n >> m >> tot; //tot就是题目中的b
    for (int i = 1; i <= n; i++) {
        cin >> f[i];
        maxn = max(f[i], r);
    }
    int a, b, c;
    for (int i = 1; i <= m; i++) {
        cin >> a >> b >> c;
        AddEdge(a, b, c);
        AddEdge(b, a, c);
    }
    l = 1;
    r = maxn;
    while(l <= r) {
        mid = (l + r) >> 1;
        //cout << mid << " " << dis[n] << "\n";
        if (Dijkstra(mid)) {
            r = mid - 1, ans = mid;
        } else {
            l = mid + 1;
        }
    }

    if (ans == -1) {
        cout << "AFK";
        return 0;
    }
    cout << ans;
    return 0;
}

by xiaofu15191 @ 2023-10-03 10:25:55

第50行写成 maxn=max(maxn,f[i]) 试试


|