CF295C Greg and Friends
首先
我们考虑每次船来回运人时都可以看成一种dp状态
又因为人的体重只有50kg和100kg两种,
所以我们可以开一个三维数组dp[i][j][k],第1维表示在出发岸50kg有i个,第2维表示在出发岸100kg有j个,第3维表示船在哪一岸
又考虑到每一个人都是不同的,所以我们需要对在船岸的这一边的人数和上船的人数去组合数,可以打一个杨辉三角的表b[i][j],用来快速查询组合数
所以状态转移就可以枚举在此岸上船50kg和100kg的人数并取组合数乘上自己的原值(乘法原理)
其次
本题还要求求最短运送次数,我们就可以用广搜来遍历枚举答案
同理dp的状态转移也可以在广搜中一起完成
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct kk
{
ll i,j,k;
}sh[1000001];//数组模拟队列
ll n,w,step[100][100][3],dp[100][100][3];
ll a[100],t50,t100,b[1000][1000],mod;
void bfs(ll i1,ll j1,ll wh)
{
ll l,r;
l=0;r=0;
sh[l].i=i1;
sh[l].j=j1;
sh[l].k=wh;
r++;
while (l<r)
{
ll nowi,nowj,nowh;
nowi=sh[l].i;
nowj=sh[l].j;
nowh=sh[l].k;
l++;
if (nowh==1)//枚举出发岸
for (ll x=0;x<=nowi;x++)
{
for (ll y=0;y<=nowj;y++)
{
if (50*x+100*y>w || (x==0 && y==0))
continue;//判断体重是否超过船载重
ll xi,xj,xwh;
xi=nowi-x;
xj=nowj-y;
xwh=1-nowh;
if (step[xi][xj][xwh]!=0)//重点,此处与一般的广搜不同,当一种状态被遍历过了不能直接标记掉,还要取最优情况
{
if (step[nowi][nowj][nowh]+1<step[xi][xj][xwh])
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
if (step[nowi][nowj][nowh]+1==step[xi][xj][xwh])//步数相同时,更新dp值
{
dp[xi][xj][xwh]+=(b[nowi+1][x+1]%mod)*(b[nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
}
}
else
{
dp[xi][xj][xwh]=(b[nowi+1][x+1]%mod)*(b[nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
sh[r].i=xi;
sh[r].j=xj;
sh[r].k=xwh;
r++;
}
}
}
if (nowh==0)//枚举对岸
for (ll x=0;x<=t50-nowi;x++)
{
for (ll y=0;y<=t100-nowj;y++)
{
if (50*x+100*y>w || (x==0 && y==0))
continue;
ll xi,xj,xwh;
xi=nowi+x;
xj=nowj+y;
xwh=1-nowh;
if (step[xi][xj][xwh]!=0)
{
if (step[nowi][nowj][nowh]+1<step[xi][xj][xwh])
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
if (step[nowi][nowj][nowh]+1==step[xi][xj][xwh])
{
dp[xi][xj][xwh]+=(b[t50-nowi+1][x+1]%mod)*(b[t100-nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
}
}
else
{
dp[xi][xj][xwh]=(b[t50-nowi+1][x+1]%mod)*(b[t100-nowj+1][y+1]%mod)*(dp[nowi][nowj][nowh]%mod);
dp[xi][xj][xwh]%=mod;
step[xi][xj][xwh]=step[nowi][nowj][nowh]+1;
sh[r].i=xi;
sh[r].j=xj;
sh[r].k=xwh;
r++;
}
}
}
}
}
int main()
{
scanf("%lld%lld",&n,&w);
for (ll x=1;x<=n;x++)
scanf("%lld",&a[x]);
memset(dp,0,sizeof(dp));
memset(step,0,sizeof(step));
for (ll x=1;x<=n;x++)
{
if (a[x]==50)
t50++;
if (a[x]==100)
t100++;
}
b[1][1]=1;
for (ll x=2;x<=50;x++)
{
for (ll y=1;y<=x;y++)
b[x][y]=b[x-1][y]+b[x-1][y-1];//杨辉三角
}
mod=1000000007;
dp[t50][t100][1]=1;//令1表示出发岸,0表示到达岸
step[t50][t100][1]=0;//初始化
bfs(t50,t100,1);//广搜函数
if (step[0][0][0]==0)//如何到达的步数为0就不能到达
{
printf("-1\n");
printf("0\n");
}
else
{
printf("%lld\n",step[0][0][0]);//答案即为出发岸无人且船在对岸的情况
printf("%lld\n",dp[0][0][0]);
}
}
CF295C Greg and Friends的更多相关文章
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改
A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...
- Codeforces Round #156 (Div. 2)---A. Greg's Workout
Greg's Workout time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 295C Greg and Friends BFS
Greg and Friends BFS的过程中维护一下方案数. 我个人感觉不是很好想, 但是写出来之后怎么感觉这题这么SB啊啊. #include<bits/stdc++.h> #def ...
- Codeforces 295 B. Greg and Graph
http://codeforces.com/problemset/problem/295/B 题意: 给定一个有边权的有向图.再给定一个1~n的排列. 按排列中的顺序依次删除点,问每次删除后,所有点对 ...
- codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...
- Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
随机推荐
- js自定义属性的操作
<body> <div id = "demo" index = "1" class = "nav"></div ...
- python数据结构树和二叉树简介
一.树的定义 树形结构是一类重要的非线性结构.树形结构是结点之间有分支,并具有层次关系的结构.它非常类似于自然界中的树.树的递归定义:树(Tree)是n(n≥0)个结点的有限集T,T为空时称为空树,否 ...
- MYSQL 50 基础题 (转载)
MYSQL 50 基础题 (转载) 前言:最近在强化MYSQL 能力 答案在(也是转载处) https://www.cnblogs.com/kangxinxin/p/11585935.html 下面是 ...
- 创建好maven项目以后发现无法创建scala文件
今天创建了一个maven项目 然后准备创建scala文件的时候发现没有Scala 然后只好上网上找方法了 下面是一种解决方法 1.点击file,选择settings 进去之后,选择build 进去 ...
- cocos creator屏幕适配的一些知识点
一. cocos creator 提供的几种适配策略 EXACT_FIT: 整个应用程序在指定区域可见,无需尝试保留原始纵横比.可能会出现失真,应用程序会被拉伸或压缩.也就是说设计分辨率的长和宽不会等 ...
- .Net Core中使用Grpc
一.Grpc概述 gRPC 基于如下思想:定义一个服务, 指定其可以被远程调用的方法及其参数和返回类型.gRPC 默认使用protocol buffers作为接口定义语言,来描述服务接口和有效载荷消息 ...
- python 不可变类型
不可变类型有:字符串,元祖,数字 可变类型:列表,字典 字典中,可变类型不能为key值 #在函数中 可变类型,为全局变量时,会变化 不可变类型,为全局变量时,不会变化
- python 字典使用——增删改查
创建字典 dict= {key1 : value1, key2 : value2 } key : value 为键值对 增: dict[key] = value 删: del dict[key] 改: ...
- scrapy基本爬虫,采集多页
# -*- coding: utf-8 -*- import csv import scrapy class GjSpider(scrapy.Spider): name = 'gj' allowed_ ...
- centOS7永久关闭防火墙(防火墙的基本使用(转)
查看防火墙状态: systemctl status firewalld.service 如图 绿的running表示防火墙开启 执行关闭命令: systemctl stop firewalld.ser ...