WA+RE求助

P1983 [NOIP2013 普及组] 车站分级

YangHao @ 2022-09-15 11:31:07

我的思路是用虚点建边然后跑spfa 评测记录

#include <cstdio>
#include <cstdlib>
#include <cstring>

const int MAXN=1010;

struct edge{
    int x, y, d, next;
}e[MAXN*MAXN*2];
int len=0, a[MAXN], tmp[MAXN];
int first[MAXN*2];
int n, m, num;
int ST, ED;

void ins (int x, int y, int d){
    e[++len].x=x; e[len].y=y; e[len].d=d;
    e[len].next=first[x]; first[x]=len;
}
inline int read (){
    int x=0; char c;
    do c=getchar (); while ('0'>c||'9'<c);
    while ('0'<=c&&'9'>=c)
        x=x*10+c-48, c=getchar ();
    return x;
}
void buildmap (){
    n=read (); m=read ();
    memset (first, 0, sizeof (first));
    for (int i=1; i<=m; ++i){
        num=read ();
        memset (tmp, 0, sizeof (tmp));
        for (int j=1; j<=num; ++j){
            a[j]=read ();
            tmp[a[j]]=1;
        }
        for (int j=a[1]; j<=a[num]; ++j)
            if (tmp[j])
                ins (n+i, j, 1);
            else
                ins (j, n+i, 0);
    }
    //虚拟原点、虚拟汇点
    ST=n+m+1; ED=n+m+2;
    for (int i=1; i<=n; ++i){
        ins (ST, i, 1);
        ins (i, ED, 0);
    }
}
int q[MAXN*MAXN*2], tf[MAXN*2];
int bfs (){
    memset (a, 0, sizeof (a));
    memset (tf, 0, sizeof (tf));
    int st=1, ed=2;
    q[1]=ST; tf[ST]=1;
    while (st<ed){
        int x=q[st];
        for (int i=first[x]; i; i=e[i].next){
            int y=e[i].y;
            if (a[y]<a[x]+e[i].d){
                a[y]=a[x]+e[i].d;
                if (!tf[y]){
                    tf[y]=1;
                    q[ed++]=y;
                }
            }
        }
        ++st; tf[x]=0;
    }
    return a[ED];
}
int main (){
    buildmap ();
    printf ("%d", bfs ());
}

by Dream_Creator @ 2022-09-15 11:40:07

不是 TLE 吗


by Mark_ZZY @ 2022-09-20 20:58:16

杨师傅重出江湖


by wushanghong2017 @ 2022-09-25 23:22:53

杨师傅和ZZY重出江湖


|