codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most k kilograms.
Greg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed k.
Also Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat.
Help Greg with this problem.
The first line contains two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ 5000) — the number of people, including Greg, and the boat's weight limit. The next line contains n integers — the people's weights. A person's weight is either 50 kilos or 100 kilos.
You can consider Greg and his friends indexed in some way.
In the first line print an integer — the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer-1.
In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109 + 7). If transporting everyone to the other bank is impossible, print integer 0.
题目大意:n个人,有人的体重是50,有的人体重是100,船只只能承受k的重量。现在只有一条船,问最少须要渡河多少次,才能把这n个人都送到对岸(渡河时船上不能没人),在最小次数下问有多少种方案渡河,同一体重的两个人视为不同的人。
思路:设dis[x][y][boat]代表原来的岸上有x个体重为50的人,y个体重为100的人,有一条船在原来的岸上(其实boat不是0就是1),到结束状态dis[0][0][0]的最短距离。从状态dis[0][0][0]开始做最短路,每一步代价为1,故使用BFS,转移的时候只需要注意船上至少有一个人,重量不要超过k就好了。
那么dis[][][]数组求好了,从起始状态dis[n1][n2][1]到最终状态的最少渡河次数就能求出来了,其中n1代表原来有n1个体重为50的人,n2为原来用n2个体重为100的人。
然后求次数,dp就可以了。状态类似用dp[x][y][boat]表示到达某个状态的方案,对于下一个方案dp[i][j][!boat],当且仅当dis[i][j][!boat] + 1 = dis[x][y][boat],船不为空,不超载时转移。
dp[i][j][!boat] += dp[x][y][boat] * (可以移动的体重为50的人选需要移动的体重为50的人的方案数) * (可以移动的体重为100的人选需要移动的体重为100的人的方案数)
复杂度为O(n^4)
不过实际上可以在求dis的时候直接倒着把dp也算了……
代码(30MS):
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII; const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = ; struct State {
int x, y, boat;
State() {}
State(int x, int y, int boat):
x(x), y(y), boat(boat) {}
}; int dis[MAXN][MAXN][], vis[MAXN][MAXN][];
LL dp[MAXN][MAXN][], c[MAXN][MAXN];
int n, k, n1, n2; 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 a, b; while(cin>>a>>b) cout<<c[a][b]<<endl;
} void bfs() {
memset(dis, 0x3f, sizeof(dis));
queue<State> que; que.push(State(, , ));
dis[][][] = ;
while(!que.empty()) {
State p = que.front(); que.pop();
if(p.boat == )
for(int i = p.x; i <= n1; ++i)
for(int j = p.y; j <= n2; ++j) {
if(i == p.x && j == p.y) continue;
if(dis[i][j][] != INF || (i - p.x) + * (j - p.y) > k) continue;
dis[i][j][] = dis[p.x][p.y][] + ;
que.push(State(i, j, ));
}
else
for(int i = ; i <= p.x; ++i)
for(int j = ; j <= p.y; ++j) {
if(i == p.x && j == p.y) continue;
if(dis[i][j][] != INF || (p.x - i) + * (p.y - j) > k) continue;
dis[i][j][] = dis[p.x][p.y][] + ;
que.push(State(i, j, ));
}
}
} LL solve() {
if(dis[n1][n2][] == INF) {
puts("-1");
return ;
}
queue<State> que; que.push(State(n1, n2, ));
dp[n1][n2][] = ;
while(!que.empty()) {
State p = que.front(); que.pop();
if(p.boat == )
for(int i = ; i <= p.x; ++i)
for(int j = ; j <= p.y; ++j) {
if(i == p.x && j == p.y) continue;
if((p.x - i) + * (p.y - j) > k) continue;
if(dis[i][j][] + != dis[p.x][p.y][]) continue;
dp[i][j][] = (dp[i][j][] + dp[p.x][p.y][] * (c[p.x][p.x - i] * c[p.y][p.y - j]) % MOD) % MOD;
if(!vis[i][j][]) {
vis[i][j][] = true;
que.push(State(i, j, ));
}
}
else
for(int i = p.x; i <= n1; ++i)
for(int j = p.y; j <= n2; ++j) {
if(i == p.x && j == p.y) continue;
if((i - p.x) + * (j - p.y) > k) continue;
if(dis[i][j][] + != dis[p.x][p.y][]) continue;
dp[i][j][] = (dp[i][j][] + dp[p.x][p.y][] * (c[n1 - p.x][i - p.x] * c[n2 - p.y][j - p.y]) % MOD) % MOD;
if(!vis[i][j][]) {
vis[i][j][] = true;
que.push(State(i, j, ));
}
}
}
cout<<dis[n1][n2][]<<endl;
return dp[][][];
} int main() {
init();
scanf("%d%d", &n, &k);
k /= ;
for(int i = ; i <= n; ++i) {
int x;
scanf("%d", &x);
n1 += (x == );
}
n2 = n - n1;
bfs();
cout<<solve()<<endl;
}
codeforces 295C Greg and Friends(BFS+DP)的更多相关文章
- Codeforces Gym101201B:Buggy Robot(BFS + DP)
题目链接 题意 给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作. 思路 和上次比赛做的 ...
- ZOJ 3596Digit Number(BFS+DP)
一道比较不错的BFS+DP题目 题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数. 很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方 ...
- CodeForces - 1073E :Segment Sum (数位DP)
You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...
- 【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)
\(IDA^*\) 说实话,这道题我一开始没想出正解,于是写了一个\(IDA^*\)... 但神奇的是,这个\(IDA^*\)居然连字符串长度分别为\(2500,4000\)的数据都跑得飞快,不过数据 ...
- codeforces 591 E. Three States(bfs+思维)
题目链接:http://codeforces.com/contest/591/problem/E 题意:有3个数字表示3个城市,每种城市都是相互连通的,然后不同种的城市不一定联通,'.'表示可以建设道 ...
- Codeforces 633F - The Chocolate Spree(树形 dp)
Codeforces 题目传送门 & 洛谷题目传送门 看来我这个蒟蒻现在也只配刷刷 *2600 左右的题了/dk 这里提供一个奇奇怪怪的大常数做法. 首先还是考虑分析"两条不相交路径 ...
- CodeForces 690C2 Brain Network (medium)(树上DP)
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...
- Codeforces 758D Ability To Convert(区间DP)
题目链接:http://codeforces.com/problemset/problem/758/D 题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了.求k这个数对应的,在10进制 ...
- codeforces 633F The Chocolate Spree (树形dp)
题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...
随机推荐
- Javascript混淆与解混淆的那些事儿
像软件加密与解密一样,javascript的混淆与解混淆同属于同一个范畴.道高一尺,魔高一丈.没有永恒的黑,也没有永恒的白.一切都是资本市场驱动行为,现在都流行你能为人解决什么问题,这个概念.那么市场 ...
- vue使用axios调用豆瓣API跨域问题
最近做了一个vue小demo,使用了豆瓣开源的API,通过ajax请求时需要跨域才能使用. 封面.jpg 一.以下是豆瓣常用的开源接口: 正在热映 :https://api.douban.com/ ...
- MySQL 参数slave_pending_jobs_size_max设置
今天生产环境上从库出现SQL进程停止的异常,错误信息如下: Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate ...
- PHP的高效率写法
1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方法和非静态方法的 ...
- yii学习笔记(7),数据库操作,联表查询
在实际开发中,联表查询是很常见的,yii提供联表查询的方式 关系型数据表:一对一关系,一对多关系 实例: 文章表和文章分类表 一个文章对应一个分类 一个分类可以对应多个文章 文章表:article 文 ...
- 使用 win10 的库来组织自己的同类文件
库相当于虚拟目录,可以把不同的文件夹包含起来. 找起东西来不用东奔西跑了...
- 【C】三目运算符(先是问号之后又是冒号的那个)
// 看这个例子就可以懂了 a = b == c ? d : e ; //如果 b==c,执行 a=d //否则执行 a=e //为了方便阅读,也可以改成下方代码 a = (b == c) ? d : ...
- STM32(13)——SPI
简介: SPI,Serial Peripheral interface串行外围设备接口. 接口应用在:EEPROM, FLASH,实时时钟,AD 转换器,还有数字信号处理器和数字信号解码器之间. 特点 ...
- 『Python题库 - 填空题』151道Python笔试填空题
『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...
- Go语言入门(二)Go语言中的变量、常量、数据类型、流程控制以及函数
Go语言中的变量 通常用var关键声明变量,有常规方式和简化方式. 常规方式: var name1 type1 name1 = value1 //赋值 简化方式: var name2 = value1 ...