【NOIP2017】Day1代码

T1 math

#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;
LL a,b,x,y;
LL tmp,ans;

int main()
{
    freopen("math.in","r",stdin);
    freopen("math.out","w",stdout);

    scanf("%lld%lld",&a,&b);
    if(a>b)
        swap(a,b);
    tmp=(a-1)*a-1;
    ans=tmp+1ll*(b-(a+1))*(a-1);

    printf("%lld\n",ans);

    return 0;
}

T2 Complexity

#include
using namespace std;

int Stack[200];
char Stack2[200],tmp[20];
int tail;
bool defined[30];

int main()
{
    freopen("complexity.in","r",stdin);
    freopen("complexity.out","w",stdout);

    int t;
    scanf("%d", &t);
    for(int i = 1; i <= t; i++)
    {
        memset(defined, 0, sizeof(defined));
        int l;
        char O[20];
        int w;
        scanf("%d O(%s", &l, &O);
        if(O[0] == '1')
            w = 0;
        else
        {
            w = 0;
            int len = strlen(O);
            for(int j = 2; j < len-1; j++)
            {
                w*=10;
                w+=O[j]-'0';
            }
        }
        tail=0;
        int cur =0,ans=0;
        for(int j = 1; j <= l; j++)
        {
            gets(tmp); //get'\n'
            char c;
            scanf("%c", &c);
            if(c == 'F')
            {
                char st[10],en[10],name;
                scanf(" %c %s %s", &name, &st, &en);
                if(defined[name - 'a'])
                {
                    tail = -1;
                    while(j <= l)
                    {
                        gets(tmp);
                        j++;
                    }
                    break;
                }
                defined[name-'a']=true;
                Stack2[tail]=name;
                Stack[tail++]=(st[0] == 'n' && en[0] == 'n') || (st[0] != 'n' && en[0] != 'n') ? 0 : 1;
                if(st[0] == 'n' && en[0] != 'n')
                    Stack[tail-1] = -20000;
                if(st[0] != 'n' && en[0] != 'n')
                {
                    int stnum = 0, ennum = 0;
                    int len = strlen(st);
                    for(int k = 0; k < len; k++)
                    {
                        stnum *= 10;
                        stnum += st[k] - '0';
                    }
                    len = strlen(en);
                    for(int k = 0; k < len; k++)
                    {
                        ennum *= 10;
                        ennum += en[k] - '0';
                    }
                    if(stnum > ennum)
                        Stack[tail-1] = -20000;
                }
                cur += Stack[tail-1];
                if(cur > ans)
                    ans = cur;
            }
            else
            {
                cur -= Stack[--tail];
                defined[Stack2[tail]-'a']=false;
                if(tail < 0)
                {
                    while(j <= l)
                    {
                        gets(tmp);
                        j++;
                    }
                    break;
                }
            }
        }
        if(tail!=0)
            printf("ERR\n");
        else
        {
            if(ans == w)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

T3 Park

#include
using namespace std;

const int MAXN=1e5+10;

int n,m,k,p,T,num;
int cnt,fcnt,lingcnt;
int que[MAXN],inq[MAXN],f[60][MAXN];
int head[MAXN],fhead[MAXN],linghead[MAXN];
int dis[MAXN],dis2[MAXN];
bool flag;

struct edge
{
    int v,nex,w;
}e[MAXN<<1],fe[MAXN<<1],ling[MAXN<<1];

inline void add(int u,int v,int w)
{
    e[++cnt]=(edge){v,head[u],w};
    head[u]=cnt;
}

inline void addf(int u,int v,int w)
{
    fe[++fcnt]=(edge){v,fhead[u],w};
    fhead[u]=fcnt;
}

inline void addling(int u,int v)
{
    ling[++lingcnt]=(edge){v,linghead[u],0};
    linghead[u]=lingcnt;
}

struct hnd
{
    int u,d;
    bool operator<(const hnd&A)const
    {
        return d>A.d;
    }
};
priority_queueq;

inline void dij(int n)
{
    dis[n]=0;
    q.push((hnd){n,dis[n]});
    while(!q.empty())
    {
        hnd x=q.top();q.pop();
        int u=x.u;
        if(x.d!=dis[u])continue;
        for(int i=head[u];i;i=e[i].nex)
        {
            int v=e[i].v,w=e[i].w;
            if(dis[u]+wq.push((hnd){v,dis[v]});
            }
        }
    }
}

inline void dij2(int n)//in fact we can merge it with dij
{
    dis2[n]=0;
    q.push((hnd){n,dis2[n]});
    while(!q.empty())
    {
        hnd x=q.top();q.pop();
        int u=x.u;
        if(x.d!=dis2[u])
            continue;
        for(int i=fhead[u];i;i=fe[i].nex)
        {
            int v=fe[i].v,w=fe[i].w;
            if(dis2[u]+wq.push((hnd){v,dis2[v]});
            }
        }
    }
}

inline void topsort()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j;j=e[j].nex)
        {
            int v=e[j].v,w=e[j].w;
            if(!w)
            {
                addling(i,v);
                inq[v]++;   
            }
        }
    }
    for(int i=1;i<=n;i++)
        if(!inq[i])
            que[++num]=i;

    for(int i=1;i<=num;i++)
    {
        int u=que[i];
        for(int j=linghead[u];j;j=ling[j].nex)
        {
            int v=ling[j].v;
            inq[v]--;
            if(!inq[v])
                que[++num]=v;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(inq[i]&&dis[i]+dis2[i]<=dis[n]+k)
        {
            printf("-1\n");
            flag=1;
            return;
        }
    }
}

inline int dp()
{
    memset(ling,0,sizeof(ling));
    memset(linghead,0,sizeof(linghead));
    memset(que,0,sizeof(que));
    lingcnt=0,num=0;

    for(int i=1;i<=n;i++)
        for(int j=head[i];j;j=e[j].nex)
        {
            int v=e[j].v,w=e[j].w;
            if(w+dis[i]==dis[v])
            {
                inq[v]++;
                addling(i,v);
            }
        }

    for(int i=1;i<=n;i++)
        if(!inq[i])
            que[++num]=i;
    for(int i=1;i<=num;i++)
    {
        int u=que[i];
        for(int j=linghead[u];j;j=ling[j].nex)
        {
            int v=ling[j].v;
            inq[v]--;
            if(!inq[v])
                que[++num]=v;
        }
    }

    f[0][1]=1;
    for(int i=0;i<=k;i++)
    {
        for(int x=1;x<=num;x++)
        {
            int u=que[x];
            for(int z=head[u];z;z=e[z].nex) 
            {
                int v=e[z].v,w=e[z].w;
                int tmp=i+dis[u]+w-dis[v];
                if(tmp<=k) 
                    f[tmp][v]=(f[tmp][v]+f[i][u])%p;
            }
        }
    }

    int ans=0;
    for(int i=0;i<=k;i++) 
        ans=(ans+f[i][n])%p;
    return ans;
}

inline void _reset()
{
    memset(e,0,sizeof(e));
    memset(fe,0,sizeof(fe));
    memset(dis,0x3f,sizeof(dis));
    memset(dis2,0x3f,sizeof(dis2));
    memset(f,0,sizeof(f));
    memset(head,0,sizeof(head));
    memset(fhead,0,sizeof(fhead));
    memset(ling,0,sizeof(ling));
    memset(linghead,0,sizeof(linghead));
    memset(que,0,sizeof(que));
    memset(inq,0,sizeof(inq));
    lingcnt=0,cnt=0,fcnt=0,num=0;
    flag=0;
}

inline void work()
{
    _reset();

    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i=1;i<=m;i++)
    {
        int tem1,tem2,tem3;
        scanf("%d%d%d",&tem1,&tem2,&tem3);
        add(tem1,tem2,tem3);
        addf(tem2,tem1,tem3);
    }

    dij(1);
    dij2(n);
    topsort();
    if(!flag)
        printf("%d\n",dp());
}

int main()
{
    freopen("park.in","r",stdin);
    freopen("park.out","w",stdout);

    scanf("%d",&T);
    while(T--)
        work();

    return 0; 
}

你可能感兴趣的:(NOIP)