(vector+Dij) sub0全RE,sub1全AC(悬关求调)

P1462 通往奥格瑞玛的道路

Joker_IV @ 2024-08-13 09:04:17

rt
看了很多警钟题解,一直找不到问题所在,代码如下,求大佬帮帮

#include <bits/stdc++.h>
#define MAXN 100010
#define INF 2147483647
using namespace std;
struct edge{
    int v;
   long long w;
};
struct node{
    int v;
   long long d;
    bool operator <(const node &x)const{
        return x.d > d;
    }
};
int cost[MAXN], vis[MAXN], n, m, b;
long long D[MAXN];
vector <edge>G[MAXN];
priority_queue <node> ans; 
bool Dij(int s){
    if(cost[1] > s)return false;
    fill(D, D + MAXN, INF);
    fill(vis, vis + MAXN, 0);
    D[1] = 0;
    ans.push((node) {1, 0});
    while(!ans.empty()){
        int u = ans.top().v;
        ans.pop();
        if(vis[u])continue;
        vis[u] = 1;
        for(int i = 0; i < G[u].size(); i++){
            int v = G[u][i].v;
            long long w = G[u][i].w;
            if(D[u] + w < D[v] && cost[v] <= s){
                D[v] = D[u] + w;
                ans.push((node) {v, D[v]});
            }
        }
    }
    if(D[n] <= b)return true;
    else return false;
}
int main(){
    scanf("%d%d%d", &n, &m, &b);
    int ma = 0, mi = INF;
    for(int i = 1; i <= n;  i++){
        scanf("%d", &cost[i]);
        ma = max(ma, cost[i]);
        mi = min(mi, cost[i]);
    }
    for(int i = 1; i <= m; i++){
        int a, b;
        long long c;
        scanf("%d%d%lld", &a, &b, &c);
        G[a].push_back((edge){b, c});
        G[c].push_back((edge){a, c});
    }
    if(!Dij(INF)){
        printf("AFK");
        return 0;
    }
    while(ma > mi){
        int mid = (ma + mi) >> 1;
        if(Dij(mid))ma = mid;
        else mi = mid + 1;
    }
    printf("%d", mi);
    return 0;
}

by Eden2027 @ 2024-08-15 15:09:22

@Zrf3383279502168841 二分写错了


by Eden2027 @ 2024-08-15 15:14:59

@Zrf3383279502168841

    int l=0,r=1e9+5,ans;
    while(l<=r){
        int mid=(l+r)/2;
        if(dijk(mid))r=mid-1, ans = mid;
        else
            l=mid+1;
    }
    cout<<ans;

用一个ans记录答案


by Eden2027 @ 2024-08-15 16:30:42

@Zrf3383279502168841 而且你operator比较时写反了


by Joker_IV @ 2024-08-17 17:01:40

@Eden2027
我如你所说修改后,仍然存在RE问题
记录 不过还是谢谢了


|