30分SF错误求解

P1462 通往奥格瑞玛的道路

Samsara_Karma @ 2017-04-17 10:20:28

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long sum = 0,fst[100010],nxt[100010],lst[100010],des[100010],dis[100010],val[100010],s[100010];

struct xint{
    long long num,val;
}q[5000010];

void add_edge(long long x,long long y,long long v){
    if (fst[x] == 0)    fst[x] = ++sum;
    else nxt[lst[x]] = ++sum;
    lst[x] = sum;
    des[sum] = y;
    dis[sum] = v;
}
int main(){
    int n,m,b;
    cin >> n >> m >> b;
    for (int i=1;i<=n;++i){
        cin >> val[i];
        s[i] = val[i];
    }
    for (int i=1;i<=m;++i){
        int x,y,v;
        cin >> x >> y >> v;
        add_edge(x,y,v);
        add_edge(y,x,v);
    }
    sort(s+1,s+n+1);
    long long ans = 1e18;
    long long l = 1,r = s[n];
    bool boo = 0;
    while (true){
        long long mid = (l + r) / 2;
        long long mx = mid;

        bool bo = 0;
        memset(q,0,sizeof q);
        long long L = 1,R = 1;
        q[L].num = 1;
        q[L].val = b;
        if (val[1] > mx)    L = 2;
        while (L <= R){
            if (q[L].num == n){
                bo = 1;
                boo = 1;
                break;
            }
            for (int i=fst[q[L].num];i;i=nxt[i]){
                if (val[des[i]] <= mx && q[L].val - dis[i] > 0){
                    q[++R].num = des[i];
                    q[R].val = max(q[R].val,q[L].val - dis[i]);
                }
            }
            ++ L;
        }

        if (l >= r)    break;
        if (bo)    r = mid;
        else l = mid + 1;
    }
    if (boo)    cout << r << endl;
    else cout << "AFK" << endl;
    return 0;
}

用的是常规方法,求解


|