蒟蒻81分求助

P1462 通往奥格瑞玛的道路

樱初音斗橡皮 @ 2019-03-09 20:15:33

RT,简单的spfa+二分,WA*2,莫名输出AFK,求助

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int n, m;
long long b;
long long f[10002];
int head[10002];
struct node
{
    int to;
    int nxt;
    long long wei;
} edge[100002];
int cnt;
void add_edge(int f, int t, long long w)
{
    ++cnt;
    edge[cnt].to=t;
    edge[cnt].nxt=head[f];
    edge[cnt].wei=w;
    head[f]=cnt;
    return;
}
bool vis[10002];
long long dist[10002];
const long long inf=99999999999999ll;
bool spfa(long long yu/*阈值*/)
{
    if (f[1]>yu)
        return false;
    dist[1]=0;
    for (int i=2; i<=n; ++i)
        dist[i]=inf;
    queue<int> q;
    q.push(1);
    for (int i=1; i<=n; ++i)
        vis[i]=false;
    vis[1]=true;
    while (!q.empty())
    {
        int fr=q.front();
        q.pop();
        vis[fr]=false;
        for (int i=head[fr]; i!=-1; i=edge[i].nxt)
        {
            int t=edge[i].to;
            if (f[t]<=yu&&!vis[t])
            {
                if (dist[t]>dist[fr]+edge[i].wei)
                {
                    dist[t]=dist[fr]+edge[i].wei;
                    q.push(t);
                    vis[t]=true;
                }
            }
        }
    }
    return dist[n]<=b;
}
int main()
{
    scanf("%d%d%lld", &n, &m, &b);
    for (int i=1; i<=n; ++i)
        scanf("%lld", &f[i]), head[i]=-1;
    for (int i=1; i<=m; ++i)
    {
        int f, t;
        long long w;
        scanf("%d%d%lld", &f, &t, &w);
        add_edge(f, t, w);
        add_edge(t, f, w);
    }
    long long l=0, r=1000000000ll, ans=10000000001ll;
    while (l<=r)
    {
        long long mid=(l+r)/2;
        if (spfa(mid))
            ans=mid, r=mid-1;
        else
            l=mid+1;
    }
    if (ans==10000000001ll)
        printf("AFK\n");
    else
        printf("%lld\n", ans);
    return 0;
}

by yuy_ @ 2019-03-09 20:30:10

59行

    return dist[n]<b;

by yuy_ @ 2019-03-09 20:38:25

@樱初音斗橡皮


by yuy_ @ 2019-03-09 20:44:32

@樱初音斗橡皮 似乎还存在问题。


by 樱初音斗橡皮 @ 2019-03-09 20:46:41

@yuy_ 已经知道是什么问题了,谢谢


by yuy_ @ 2019-03-09 20:53:34

@樱初音斗橡皮 哪里问题0.0


by 樱初音斗橡皮 @ 2019-03-09 20:55:08

@yuy_ 如果已经在队列中错误代码不松弛


by yuy_ @ 2019-03-09 21:00:10

@樱初音斗橡皮 2333看来是SPFA的原因。。


|