求助 dij+二分 20分(我好菜啊)

P1462 通往奥格瑞玛的道路

JoeZ009 @ 2022-07-09 00:07:44

求助 dij+二分 20分(我好菜啊) WA2~6,8~10

#include<bits/stdc++.h>
using namespace std;
const int N=10010,M=5*N;

void read(long long &f) {
    f=0;
    char c;
    bool d=0;
    while (c=getchar(),c<'0'||c>'9') if (c=='-') d=1;
    f=f*10+c-48;
    while (c=getchar(),c>='0'&&c<='9') f=f*10+c-48;
    if (d) f*=-1;
}

long long e[M*2],ne[M*2],ve[M*2],h[N],idx;
void add(long long x,long long y,long long t) {
    idx++;
    e[idx]=y;
    ne[idx]=h[x];
    ve[idx]=t;
    h[x]=idx;

    idx++;
    e[idx]=x;
    ne[idx]=h[y];
    ve[idx]=t;
    h[y]=idx;
}

long long n,m,b;
long long t[N];
long long ll=1,rr,mm;

priority_queue<pair<long long,long long> >q;
bool c[N];
long long d[N];
bool dij(int o) {
    if(t[1]>o)return 0;

    memset(c,0,sizeof c);
    while(q.size())q.pop();
    for(int i=1; i<=n; i++)d[i]=N*N;
    d[1]=0;

    q.push(make_pair(0,1));
    while(q.size()) {
        long long x=q.top().second;
        q.pop();
        if(c[x])continue;
        c[x]=1;
        for(int i=h[x]; i!=-1; i=ne[i]) {
            if(t[e[i]]<=o&&d[e[i]]>d[x]+ve[i]) {
                d[e[i]]=d[x]+ve[i];
                q.push(make_pair(-d[e[i]],e[i]));
            }
        }
    }
    //for(int i=1; i<=n; i++)cout<<d[i]<<" ";
    //cout<<endl;
    if(d[n]>b)return 0;
    return 1;
}
int main() {
    read(n);
    read(m);
    read(b);
    for(int i=1; i<=n; i++) {
        read(t[i]);
        rr=max(rr,t[i]);
    }
    long long x,y,t;
    memset(h,-1,sizeof h);
    while(m--) {
        read(x);
        read(y);
        read(t);
        if(x==y)continue;
        add(x,y,t);
    }
    if(!dij(rr)) {
        cout<<"AFK"<<endl;
        return 0;
    }
//  for(int i=1; i<=n; i++) {
//      for(int j=h[i]; j!=-1; j=ne[j]) {
//          cout<<e[j]<<"+"<<ve[j]<<"  ";
//      }
//      cout<<endl;
//  }

    while(ll<=rr) {
        int mm=ll+rr>>1;
        if (dij(mm)) rr=mm-1;
        else ll=mm+1;
    }
    cout<<ll<<endl;
    return 0;
}

by metaphysis @ 2022-07-09 07:50:46

@zjy090801

一个常见的错误,对于本题的数据范围来说,“表示距离无限的值不够大”:

for(int i=1; i<=n; i++)d[i]=N*N;
\rightarrow
for(int i=1; i<=n; i++)d[i]=0x3F3F3F3F3F3F3F3F;

by JoeZ009 @ 2022-07-09 14:52:13

@metaphysis 谢谢大佬,已AC


|