Codeforces 295C Greg and Friends

BFS+DP.dp[i][j][0]表示有i个50kg,j个100kg的人在左岸,dp[i][j][1]表示有i个50kg,j个100kg的人在右岸。用BFS求最短路的时候记录到达该状态的可能情况。

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; typedef long long LL;
#define maxn 55
#define INF 0xffffffff
const LL mod = ; struct Point{
int f, t, s;
Point(){}
Point(int _f, int _t, int _s):
f(_f), t(_t), s(_s){}
}; LL dp[maxn][maxn][];
LL C[maxn][maxn];
int hash[maxn][maxn][]; int bfs(int nf, int nt, int cap){
queue<Point> Q;
dp[nf][nt][] = ;
hash[nf][nt][] = ;
Q.push(Point(nf, nt, ));
while(!Q.empty()){
Point c = Q.front();Q.pop();
int cf = c.f, ct = c.t, cs = c.s;
for(int i = ; i <= c.f; i ++){
if(i* > cap) break;
for(int j = ; j <= c.t; j ++){
if(i* + j* > cap) continue;
if(i* + j* <= ) continue; int lf = c.f - i, lt = c.t - j;
if(hash[nf - lf][nt - lt][!cs]==INF){
hash[nf - lf][nt - lt][!cs] = hash[cf][ct][cs] + ;
LL tmp = C[cf][i]*C[ct][j]%mod;
dp[nf - lf][nt - lt][!cs] += (tmp*dp[cf][ct][cs]%mod);
dp[nf - lf][nt - lt][!cs]%=mod;
Q.push(Point(nf-lf, nt-lt, !cs));
}
else if(hash[nf - lf][nt - lt][!cs]==hash[cf][ct][cs] + ){
LL tmp = C[cf][i]*C[ct][j]%mod;
dp[nf - lf][nt - lt][!cs] += (tmp*dp[cf][ct][cs]%mod);
dp[nf - lf][nt - lt][!cs]%=mod;
}
}
}
}
return hash[nf][nt][];
} void init(){
C[][] = ;
for(int i = ; i <= ; i ++){
C[i][] = ;
for(int j = ; j <= i; j ++){
C[i][j] = (C[i-][j-] + C[i-][j])%mod;
}
}
} int main()
{
init();
//freopen("test.in", "r", stdin);
for(int n, k, nf, nt; scanf("%d%d", &n, &k)!=EOF; ){
memset(dp, , sizeof(dp));
memset(hash, 0xff, sizeof(hash));
nf = nt = ;
for(int i = , x; i < n; i ++){
scanf("%d", &x);
if(x==) nf ++;
else nt ++;
}
bfs(nf, nt, k);
printf("%d\n%I64d\n", hash[nf][nt][], dp[nf][nt][]);
}
return ;
}
上一篇:HDFS Users Guide--官方文档


下一篇:TDDL与Spring Boot集成Version报错——跟踪与解决