不能理解……

P1462 通往奥格瑞玛的道路

Brilliant11001 @ 2023-08-31 16:16:30

不开O2全RE,开了O2就AC了。。。

请各位大佬帮忙看看是为什么,谢谢!

不开O2

开O2

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 100010, M = 500010;
int n, m, hp;
int h[N], e[M], w[M], ne[M], idx;
int f[N];
int dist[N];
bool vis[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

bool dij(int limit) {
    memset(dist, 0x3f, sizeof dist);
    memset(vis, 0, sizeof vis);
    if(f[1] > limit) return false;
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII> > heap;
    heap.push({0, 1});
    while(heap.size()) {
        PII t = heap.top();
        heap.pop();
        int ver = t.second, distance = t.first;
        if(vis[ver]) continue;
        vis[ver] = true;
        for(int i = h[ver]; i != -1; i = ne[i]) {
            int j = e[i];
            if(f[j] > limit) continue;
            if(dist[j] > dist[ver] + w[i]) {
                dist[j] = dist[ver] + w[i];
                heap.push({dist[j], j});
            }
        }
    }
    if(dist[n] > hp) return false;
    else return true;
}

signed main() {
//  freopen("P1462_1.in", "r", stdin);
//  freopen("ans.out", "w", stdout);
    scanf("%d%d%d", &n, &m, &hp);
    memset(h, -1, sizeof h);
    int l = 1, r = 0;   
    for(int i = 1; i <= n; i++) {
        scanf("%d", &f[i]);
        r = max(r, f[i]);
    }
    for(int i = 1; i <= m; i++) {
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        add(a, b, w);
        add(b, a, w);
    }
    while(l < r) {
        int mid = l + r >> 1;
        if(dij(mid)) r = mid;
        else l = mid + 1;
    }
    if(!dij(l)) cout << "AFK" << endl;
    else cout << l << endl;
    return 0;
}

by _zlh4_Fishrow_ @ 2023-08-31 16:48:35

数组开太大了吧?


by _zlh4_Fishrow_ @ 2023-08-31 16:49:10

N和M可以去一个0试试看


by Brilliant11001 @ 2023-09-02 18:25:58

@_zlh4Fishrow 不行的,还是RE


by _zlh4_Fishrow_ @ 2023-09-02 21:22:19

@Brilliant11001 我看了一下,好像是h数组在add函数中初始化时溢出了


by _zlh4_Fishrow_ @ 2023-09-02 21:22:43

你再改一改


by Brilliant11001 @ 2023-09-03 08:05:56

@_zlh4Fishrow 怎么可能呢?而且我刚试了一下,开到 10^7 还是会RE。。。qwq


|