zoukankan      html  css  js  c++  java
  • BZOJ4531: [Bjoi2014]路径

    Description

     

    在一个N个节点的无向图(没有自环、重边)上,每个点都有一个符号,
    可能是数字,也可能是加号、减号、乘号、除号、小括号。你要在这个图上数
    一数,有多少种走恰好K个节点的方法,使得路过的符号串起来能够得到一
    个算数表达式。路径的起点和终点可以任意选择。
    所谓算数表达式,就是由运算符连接起来的一系列数字。括号可以插入在
    表达式中以表明运算顺序。
    注意,你要处理各种情况,比如数字不能有多余的前导0,减号只有前面
    没有运算符或数字的时候才可以当成负号,括号可以任意添加(但不能有空括
    号),0可以做除数(我们只考虑文法而不考虑语意),加号不能当正号。
    例如,下面的是合法的表达式:
    -0/0
    ((0)+(((2*3+4)+(-5)+7))+(-(2*3)*6))
    而下面的不是合法的表达式:
    001+0
    1+2(2)
    3+-3
    --1
    +1
    ()
     

    Input

     第一行三个整数N,M,K,表示点的数量,边的数量和走的节点数。

    第二行一个字符串,表示每个点的符号。
    接下来M行,每行两个数,表示一条边连的两个点的编号。
    1≤N≤20,0≤M≤N×(N-1)/2,0≤K≤30
     

    Output

    输出一行一个整数,表示走的方法数。这个数可能比较大,你只需要输出

    它模1000000007的余数即可。
     

    Sample Input

    6 10 3
    )(1*+0
    1 2
    1 3
    1 4
    2 3
    3 4
    2 5
    3 5
    3 6
    4 6
    5 6

    Sample Output

    10
    //一共有10条路径,构成的表达式依次是101, (1), 1+1, 1+0, 1*1, 1*0, 0+0,
    0+1, 0*0, 0*1
     
    就是一道简单的DP辣(好容易写错的呢)。(WA了5发要是考场懵逼了就跪了)
    设f[x][k][c][S]表示当前在节点x,已经走了k步,“(”-“)”数量为c,有没有前缀0的路径数量。
    转移挺复杂且容易写错的,详见code(也可以用来对拍)。
     
    #include<cstdio>
    #include<cctype>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    #define rep(i,s,t) for(int i=s;i<=t;i++)
    #define dwn(i,s,t) for(int i=s;i>=t;i--)
    #define ren for(int i=first[x];i;i=next[i])
    using namespace std;
    inline int read() {
        int x=0,f=1;char c=getchar();
        for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
        for(;isdigit(c);c=getchar()) x=x*10+c-'0';
        return x*f;
    }
    typedef long long ll;
    const int mod=1000000007;
    int f[25][35][35][2];//f[x][t][v] '(' - ')'
    int n,k,m,e[25][25];
    char s[25];
    int iscal(char c) {return c=='+'||c=='-'||c=='*'||c=='/';}
    int dp(int x,int t,int c,int pre) {
    	if(t>=k) return (!c)&&!iscal(s[x]);
    	int& ans=f[x][t][c][pre];
    	if(ans>=0) return ans;
    	ans=0;
    	rep(i,1,n) if(e[x][i]) {
    		if(s[i]>='0'&&s[i]<='9') {
    			if(s[x]>='0'&&s[x]<='9') {
    				if(!pre) (ans+=dp(i,t+1,c,0))%=mod;
    			}
    			else if(iscal(s[x])||s[x]=='(') (ans+=dp(i,t+1,c,s[i]=='0'))%=mod;
    		}
    		else if(s[i]=='('||s[i]==')') {
    			if(s[i]=='('&&(s[x]=='('||iscal(s[x]))) (ans+=dp(i,t+1,c+1,0))%=mod;
    			else if(s[i]==')'&&c&&(isdigit(s[x])||s[x]==')')) (ans+=dp(i,t+1,c-1,0))%=mod;
    		}
    		else {
    			if(s[x]>='0'&&s[x]<='9') (ans+=dp(i,t+1,c,s[i]=='0'))%=mod;
    			if(s[x]==')') (ans+=dp(i,t+1,c,0))%=mod;
    			if(s[x]=='('&&s[i]=='-') (ans+=dp(i,t+1,c,0))%=mod;
    		}
    	}
    	return ans;
    }
    int main() {
    	memset(f,-1,sizeof(f));
    	n=read();m=read();k=read();
    	scanf("%s",s+1);
    	rep(i,1,m) {
    		int x=read(),y=read();
    		e[x][y]=e[y][x]=1;
    	}
    	int ans=0;
    	rep(i,1,n) {
    		if(s[i]=='(') (ans+=dp(i,1,1,0))%=mod;
    		if(s[i]=='-') (ans+=dp(i,1,0,0))%=mod;
    		if(s[i]>='1'&&s[i]<='9') (ans+=dp(i,1,0,0))%=mod;
    		if(s[i]=='0') (ans+=dp(i,1,0,1))%=mod;
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
    

      

  • 相关阅读:
    C#三元运算符
    WIN系统查询版本
    C# switch 语句
    C#反编译
    AssemblyInfo.cs 文件信息
    win系统如何在桌面显示我的电脑
    MVC传值前台
    js去除html标记
    打开页面跳转到区域下的控制器
    Hive常用操作之数据导入导出
  • 原文地址:https://www.cnblogs.com/wzj-is-a-juruo/p/5391688.html
Copyright © 2011-2022 走看看