NOIP2017提高组D1

D1
T1.math数论
T2.complexity模拟(栈)
T3.park spfa+tarjan缩点找0环+拓扑序dp

A

#include <cstdio>
#include <cstring>
#define ll long long
#define inf 0x3f3f3f3f
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m;
int main(){
//  freopen("math.in","r",stdin);
//  freopen("math.out","w",stdout);
    n=read();m=read();
    printf("%lld\n",(ll)n*m-n-m);
    return 0;
}

B

#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 110
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int tst,n,f[N],res=0;
bool inq['z'+1];
char re[N],op[N][3],var[N][3],a[N][10],b[N][10];
inline bool check(){
    stack<int>q;memset(inq,0,sizeof(inq));
    for(int i=1;i<=n;++i){
        if(op[i][1]=='F'){
            if(inq[var[i][1]]) return 0;
            q.push(var[i][1]),inq[var[i][1]]=1;
        }
        else{
            if(q.empty()) return 0;
            int x=q.top();q.pop();inq[x]=0;
        }
    }if(!q.empty()) return 0;return 1;
}
int main(){
//  freopen("complexity.in","r",stdin);
//  freopen("complexity.out","w",stdout);
    tst=read();
    while(tst--){
        n=read();scanf("%s",re+1);memset(f,0,sizeof(f));res=0;
        if(re[3]=='n'){
            if(re[5]>='0'&&re[5]<='9') res=res*10+re[5]-'0';
            if(re[6]>='0'&&re[6]<='9') res=res*10+re[6]-'0';
        }
        for(int i=1;i<=n;++i){
            scanf("%s",op[i]+1);
            if(op[i][1]=='F'){
                scanf("%s%s%s",var[i]+1,a[i]+1,b[i]+1);
                if(a[i][1]=='n'&&b[i][1]=='n') f[i]=0;
                else if(a[i][1]=='n') f[i]=-1;
                else if(b[i][1]=='n') f[i]=1;
                else{
                    int x=a[i][1]-'0',y=b[i][1]-'0';
                    if(a[i][2]>='0'&&a[i][2]<='9') x=x*10+a[i][2]-'0';
                    if(b[i][2]>='0'&&b[i][2]<='9') y=y*10+b[i][2]-'0';
                    if(x<=y) f[i]=0;else f[i]=-1;
                }
            }
        }if(!check()){puts("ERR");continue;}
        stack<int>q;int sum=0,ans=0,fail=0;
        for(int i=1;i<=n;++i){
            if(op[i][1]=='F'){
                q.push(f[i]);if(f[i]==-1) fail++;
                else if(!fail) sum+=f[i];ans=max(ans,sum);
            }else{
                int x=q.top();q.pop();if(x==-1) fail--;
                else if(!fail) sum-=x;
            }
        }
        puts(res==ans?"Yes":"No");
    }return 0;
}

C

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100020
#define pa pair<int,int>
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,k,mod,h[N],num=0,dis[N],dp[N][60],tst,h1[N],dis1[N],w[N<<1];
int dfnum=0,dfn[N],low[N],scc=0,sz[N],du[N][60];
bool inq[N],bad[N<<1];
struct edge{
    int fr,to,next,val;
}data[N<<1],data1[N<<1];
void spfa(){
    queue<int>q;memset(dis,0x3f,sizeof(dis));
    q.push(1);inq[1]=1;dis[1]=0;
    while(!q.empty()){
        int x=q.front();q.pop();inq[x]=0;
        for(int i=h[x];i;i=data[i].next){
            int y=data[i].to;
            if(dis[y]>dis[x]+data[i].val){
                dis[y]=dis[x]+data[i].val;
                if(!inq[y]) inq[y]=1,q.push(y);
            }
        }
    }
}
void spfa1(){
    queue<int>q;memset(dis1,0x3f,sizeof(dis));
    q.push(n);inq[n]=1;dis1[n]=0;
    while(!q.empty()){
        int x=q.front();q.pop();inq[x]=0;
        for(int i=h1[x];i;i=data1[i].next){
            int y=data1[i].to;
            if(dis1[y]>dis1[x]+data1[i].val){
                dis1[y]=dis1[x]+data1[i].val;
                if(!inq[y]) inq[y]=1,q.push(y);
            }
        }
    }
}stack<int>qq;
void tarjan(int x){
    dfn[x]=low[x]=++dfnum;qq.push(x);inq[x]=1;
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(data[i].val||bad[i]) continue;
        if(!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);
        else if(inq[y]) low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x]){
        ++scc;sz[scc]=0;while(1){
            int y=qq.top();qq.pop();inq[y]=0;
            sz[scc]++;if(y==x) break;
        }
    }
}
int main(){
//  freopen("a.in","r",stdin);
    tst=read();
    while(tst--){
        n=read();m=read();k=read();mod=read();memset(bad,0,sizeof(bad));
        memset(h,0,sizeof(h));num=0;memset(dp,0,sizeof(dp));dp[1][0]=1;
        bool flag=0;memset(du,0,sizeof(du));memset(h1,0,sizeof(h1));
        for(int i=1;i<=m;++i){
            int x=read(),y=read(),val=read();
            data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;data[num].fr=x;
            data1[num].to=x;data1[num].next=h1[y];h1[y]=num;data1[num].val=val;data1[num].fr=y;
        }spfa();spfa1();dfnum=0;memset(dfn,0,sizeof(dfn));scc=0;
        for(int i=1;i<=m;++i)//删去不可能存在在答案中的边 
            if(dis[data[i].fr]+data[i].val+dis1[data[i].to]>dis[n]+k) bad[i]=1;
            else w[i]=dis[data[i].fr]+data[i].val-dis[data[i].to];
        for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i);//缩点标记0环
        for(int i=1;i<=scc;++i) if(sz[i]>=2){flag=1;break;}
        if(flag){puts("-1");continue;}//如果还有0环,肯定无穷多解
        for(int x=1;x<=n;++x)
            for(int i=h[x];i;i=data[i].next){
                if(bad[i]) continue;int y=data[i].to;
                for(int j=0;j+w[i]<=k;++j) du[y][j+w[i]]++;
            }queue<pa>q;
        for(int i=1;i<=n;++i)
            for(int j=0;j<=k;++j) if(!du[i][j]) q.push(make_pair(i,j));
        while(!q.empty()){
            int x=q.front().first,j=q.front().second;q.pop();
            for(int i=h[x];i;i=data[i].next){
                if(bad[i]) continue;int y=data[i].to;
                if(j+w[i]>k) continue;dp[y][j+w[i]]=(dp[y][j+w[i]]+dp[x][j])%mod;
                if(--du[y][j+w[i]]==0) q.push(make_pair(y,j+w[i]));
            }
        }int ans=0;for(int j=0;j<=k;++j) ans=(ans+dp[n][j])%mod;
        printf("%d\n",ans);
    }return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值