Codeforces 295C Greg and Friends

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

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 using namespace std;
 6 
 7 typedef long long LL;
 8 #define maxn 55
 9 #define INF 0xffffffff
10 const LL mod = 1000000007;
11 
12 struct Point{
13     int f, t, s;
14     Point(){}
15     Point(int _f, int _t, int _s):
16     f(_f), t(_t), s(_s){}
17 };
18 
19 LL dp[maxn][maxn][2];
20 LL C[maxn][maxn];
21 int hash[maxn][maxn][2];
22 
23 int bfs(int nf, int nt, int cap){
24     queue<Point> Q;
25     dp[nf][nt][0] = 1;
26     hash[nf][nt][0] = 0;
27     Q.push(Point(nf, nt, 0));
28     while(!Q.empty()){
29         Point c = Q.front();Q.pop();
30         int cf = c.f, ct = c.t, cs = c.s;
31         for(int i = 0; i <= c.f; i ++){
32             if(i*50 > cap) break;
33             for(int j = 0; j <= c.t; j ++){
34                 if(i*50 + j*100 > cap) continue;
35                 if(i*50 + j*100 <= 0) continue;
36 
37                 int lf = c.f - i, lt = c.t - j;
38                 if(hash[nf - lf][nt - lt][!cs]==INF){
39                     hash[nf - lf][nt - lt][!cs] = hash[cf][ct][cs] + 1;
40                     LL tmp = C[cf][i]*C[ct][j]%mod;
41                     dp[nf - lf][nt - lt][!cs] += (tmp*dp[cf][ct][cs]%mod);
42                     dp[nf - lf][nt - lt][!cs]%=mod;
43                     Q.push(Point(nf-lf, nt-lt, !cs));
44                 }
45                 else if(hash[nf - lf][nt - lt][!cs]==hash[cf][ct][cs] + 1){
46                     LL tmp = C[cf][i]*C[ct][j]%mod;
47                     dp[nf - lf][nt - lt][!cs] += (tmp*dp[cf][ct][cs]%mod);
48                     dp[nf - lf][nt - lt][!cs]%=mod;
49                 }
50             }
51         }
52     }
53     return hash[nf][nt][1];
54 }
55 
56 void init(){
57     C[0][0] = 1;
58     for(int i = 1; i <= 50; i ++){
59         C[i][0] = 1;
60         for(int j = 1; j <= i; j ++){
61             C[i][j] = (C[i-1][j-1] + C[i-1][j])%mod;
62         }
63     }
64 }
65 
66 int main()
67 {
68     init();
69     //freopen("test.in", "r", stdin);
70     for(int n, k, nf, nt; scanf("%d%d", &n, &k)!=EOF; ){
71         memset(dp, 0, sizeof(dp));
72         memset(hash, 0xff, sizeof(hash));
73         nf = nt = 0;
74         for(int i = 0, x; i < n; i ++){
75             scanf("%d", &x);
76             if(x==50) nf ++;
77             else nt ++;
78         }
79         bfs(nf, nt, k);
80         printf("%d
%I64d
", hash[nf][nt][1], dp[nf][nt][1]);
81     }
82     return 0;
83 }
View Code
原文地址:https://www.cnblogs.com/bootstar/p/3209643.html