wa了两点救命

P1462 通往奥格瑞玛的道路

SSSSSaber @ 2020-03-28 22:17:18


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,b;
struct edge {
    int from,to,val,blo,at;
} e[100100];
int que[10010];
int was[10010],cnt;
int pre[10010],head[10010];
int temp,ans,flag;
int dis[10010];
bool fer[10010];
void addedge(int x,int y,int fee) {
    e[++cnt].from =head[x];
    head[x]=cnt;
    e[cnt].blo =fee;
    e[cnt].to =y;
    e[cnt].val =was[y];
    e[cnt].at =x;
}
int minn,maxx;
int found(int x) {
    if(pre[x]==x)
        return x;
    else {
        pre[x]=found(pre[x]);
        return pre[x];
    }
}
int spfa(int x) {
    int front=0,tail=1;
    memset(dis,0x7f,sizeof(dis));
    dis[1]=0;
    que[tail]=1;
    for(int i=1;i<=n;i++)
    {
        pre[i]=i;
    }
    while(front<tail) { 
        front++;
        int u=que[front];
        fer[u]=0;
    //  cout<<"  u  "<<u<<endl;
        //cout<<head[u]<<endl;
        for(int i=head[u]; i; i=e[i].from ) {
        //  cout<<"val  "<<e[i].val <<" x  "<<x<<endl;
            if(e[i].val <=x) {
                int v=e[i].to ;
            //  printf("u  %d   v  %d   val  %d",u,v,e[i].val );
                //cout<<dis[v]<<dis[u]+e[i].blo ;
                if(dis[v]>dis[u]+e[i].blo ) {
                //  cout<<"here";
                    dis[v]=dis[u]+e[i].blo ;
                    if(found(v)!=found(u))
                    {
                        pre[v]=u;
                    }
                    if(!fer[v])
                    {
                        fer[v]=1;
                        que[++tail]=v;
                        //printf("tail %d  que[t]  %d",tail,que[tail]);
                    }
                }
            }
        }
    }//printf("fa1 %d  fan  %d\n",found(1),found(n));
    if(found(1)==found(n)&&dis[n]<b)
    {
        flag=1;
        return 1;
    }
    else
    return 0;
}
bool check(int x) {
    if(spfa(x))
        return 1;
    else
        return 0;
}
int main() {
    cin>>n>>m>>b;
    for(int i=1; i<=n; i++) {
        cin>>was[i];
        maxx=max(was[i],maxx);
    }
    minn=was[1]<was[n]?was[1]:was[n];
    for(int i=1; i<=m; i++) {
        int ai,bi,ci;
        scanf("%d%d%d",&ai,&bi,&ci);
        addedge(ai,bi,ci);
        addedge(bi,ai,ci);
    }
    long long left=minn,right=maxx;
    while(left<=right) {
    //  cout<<"l "<<left<<"r  "<<right<<endl;
        long long mid=(left+right)/2;
    //  cout<<"mid  "<<mid<<endl;
        if(!check(mid)) {
            left=mid+1;
        } else {
            ans=mid;
            right=mid-1;
        }
    }
    ans=left;
    if(flag==0)
    {
        cout<<"AFK";
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}```

by STPGUY @ 2020-03-28 23:23:08

哥们儿,Dis要用longlong,不然要爆int,然后你别用数组模拟队列,要爆内存,你就用STL就可以了


by STPGUY @ 2020-03-28 23:23:26

@SSSSSaber


|