woj1008feedinganimals2-贪心-网络流
title: woj1008feedinganimals2-贪心-网络流
date: 2020-03-07
categories: acm
tags: [acm,woj,网络流,贪心]
中等题。
标准的网络流。
注意贪心只是能ac但是有反例。
description
To show your intelligence to the Lord,you must solve the problem as follows:
Suppose there are m persons and n animals. A person can feed an animal peacefully if he/she is liked by the animal.
Given a list of which person is liked by which animal,you are to determine whether all animals can be feeded peacefully on
condition that every person can feed at most k animals.
输入格式
There will be multiple test cases. For each test case,the first line contains two integers m(1 <= m <= 100) and
n(1 <= n <= 100), where m indicates the number of persons and n indicated the number of animals.The following lines
contain the m * n 0-1 matrix indicating which person is liked by which animal.The ith person is liked by the jth animal
if the jth element of the ith line is 1. The last line for each test case contains a single integer k,indicating the
maximal number of animals one person can feed.
输出格式
For each test case,output "Yes" if all animals can be feeded peacefully,"No" otherwise.
样例输入
2 8
1 1 0 1 0 0 1 1
1 1 1 0 1 1 1 1
2
7 2
1 1
1 1
1 1
1 1
1 1
1 1
1 1
2
样例输出
No
Yes
code
//1.1
// m,n 1-100,直接开就完事了
// 一开始想做DFS,每次选一列中的一个,然后往下,复杂度爆了
//换贪心和网络流
//贪心法。每个人权重=1.0*love[i][j]/maxn,也就是能喂的动物越少,权重越大,越优先安排他喂
//贪心有反例
//是网络流题
#include <iostream>
#include<vector>
#include<cstdio>
#include<stack>
using namespace std;
int person,animals,maxn;
int love[105][105];
int cnt[105]; //根据上限每个人还能喂几个 如果每个人的上限数量不同,表示每个人的数量然后计算权重就行
int summ[105]; //每个人本来能喂养的综合
bool used[105][105];
double capacity[105][105]; //每个动物占某人可喂养的权重
//复杂度 100^100 。因为这个剪枝不了,就是说如果上一层采取不同的人喂养,可能这一层原来不行的又行了
bool dfs(int tmp){ //把每一列看成树的一层,dfs
if(tmp==animals) //到了animals+1层,说明都分配好了
return true;
for (int i = 0; i <= person; i++) //
if (used[i][tmp] == false&&love[i][tmp]==1&&cnt[i]>0) { //这个人可喂并且没用过
used[i][tmp]=true;cnt[i]--;
if(dfs(tmp + 1)) //这样可行的话,true就行,因为是有一种可行情况就行
return true;
cnt[i]++; used[i][tmp]=false; //我傻了,剪枝不了
}
return false;
}
//每个动物分配可以喂养(cnt>0),权重最大的人;贪心,这样后面的动物能够喂养的机会增大
//证明: 对于某一个动物a,假如只能由某个人喂,那么他是唯一有权重的;
//会不会出现这个人权值高但是后面某个动物b只能由这个人喂,所以为了节省cnt需要保留权值高的人,要选择权值低的人喂a呢?
// 会。
/*
1 1 1 1 0 1
1 1 1 0 1 0
每人最多喂3个
解是
1 1 1
1 1 1
但是根据贪心就失败了。所以这个贪心只是可以过但是是错的
*/
bool greedy(){
double maxcap=0;int per=-1,flag=-1;
for(int i=0;i<animals;i++)
{
maxcap=0,per=-1,flag=0;
for(int j=0;j<person;j++){ //
if(capacity[j][i]>maxcap&&cnt[j]>0){ //常见错误,直接==0(double a>b) 应该用eps。但是这里数据在100,精度没那么高直接用也行
per=j;flag=1;maxcap=capacity[j][i]; //常见错误 ij反了,注意意义!!。就是这里的i是animals而main中求cap的i是person,所以要反过来。所以要注意!!
}
}
if(flag==0) return false; //常见错误 放错位置,注意在哪个结构({括号)
cnt[per]--;
}
return true;
}
int main(){
while(scanf("%d%d",&person,&animals)==2){
for(int i=0;i<person;i++)
for(int j=0;j<animals;j++)
cin>>love[i][j]; //是否喜欢
cin>>maxn;
for(int i=0;i<person;i++)
for(int j=0;j<animals;j++)
used[i][j]==false;
for(int i=0;i<person;i++) //每个人的上限
cnt[i]=maxn;
for(int i=0;i<person;i++) summ[i]=0; // 初始化
for(int i=0;i<person;i++)
for(int j=0;j<animals;j++)
summ[i]+=love[i][j]; //每个人的总和
for(int i=0;i<person;i++)
for(int j=0;j<animals;j++)
capacity[i][j] =1.0*love[i][j]/summ[i]; //权重
if(greedy())
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
//2 网上还找到了网络流的做法 https://github.com/lijiansong/acm-algorithms/blob/master/CodeForces/woj/woj1008.cpp
//好久没用,快忘了都
//复习了紫书,感觉还是挺标准的
参考《算法竞赛入门经典》第11章 网络流初步
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define N 210 //容量和流量之差=残量
int m,n,k,a[N],flow[N][N],capacity[N][N],s,t,f,p[N]; //a[N]从起点到i的可改进量(可增量),也就是残量。残量是s 到i的路径上的最小残量,而非某条边
void MaxFlow()
{
queue<int> q;
memset(flow,0,sizeof(flow));
f=0;
for(;;)
{
memset(a,0,sizeof(a));
a[s]=0xfffffff; //从外界到起点的流无限
q.push(s);
while(!q.empty())//BFS寻找增广路径
{
int u=q.front();
q.pop();
for(int v=1;v<=t;++v)
{
//找到新节点v
if(!a[v] && capacity[u][v] > flow[u][v]) //残量>0 && 容量大于流量 .
{
p[v]=u;q.push(v);//记录v的父节点,并加入队列
a[v]=min(a[u],capacity[u][v]-flow[u][v]);
}
}
// if(a[t]) break;
}
if(a[t]==0)break;//s到t 残量为0,无法改进
for(int u=t;u!=s;u=p[u])//从汇点往回走
{
flow[p[u]][u]+=a[t];//更新正向流量
flow[u][p[u]]-=a[t];//更新反向流量
}
f+=a[t];//更新从s流出的总流量
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int i,j;
while(cin>>m>>n)
{
memset(capacity,0,sizeof(capacity));
s=0;
t=m+n+1; //增加源点汇点 0 m+n+1 此时 m标号从1开始
f=0; //总的流大小
int ani=m+1,temp;
for(i=1;i<=m;++i)
{
for(j=0;j<n;++j)
{
cin>>temp;
if(temp==1)
capacity[i][ani+j]=1;
capacity[ani+j][t]=1; //反向边
}
}
cin>>k;
for(i=1;i<=m;++i)
{
capacity[0][i]=k; //源点到每个人的流大小为k,
}
MaxFlow();
if(f==n)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
woj1008feedinganimals2-贪心-网络流的更多相关文章
- 【BZOJ3716】[PA2014]Muzeum(贪心+网络流)
BZOJ 题意: 在二维网格图中有\(n\)个物品,每个物品有价值:但有\(m\)个警卫看管这些物品,每个警卫面朝\(y\)轴负方向,能看到一定角度(假定能够看到无穷远). 现在每个敬畏有一个贿赂价钱 ...
- 洛谷P1251 餐巾(网络流)
P1251 餐巾 15通过 95提交 题目提供者该用户不存在 标签网络流贪心 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 为什么我全部10个测试点都对… 题目描述 一个餐厅在相继的N天里 ...
- 洛谷P2460 [SDOI2007]科比的比赛(题解)(贪心+搜索)
科比的比赛(题解)(贪心+搜索) 标签:算法--贪心 阅读体验:https://zybuluo.com/Junlier/note/1301158 贪心+搜索 洛谷题目:P2460 [SDOI2007] ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转自 Good morning 的几句精辟的话
1.志愿者招募 根据流量平衡方程来构图非常方便,而且简单易懂,以后可能成为做网络流的神法之一 简单记一下流量平衡方程构图法的步骤: a.列出需求不等式 b.通过设置松弛变量,将不等式变成等式 c.两两 ...
- 【HDOJ】2732 Leapin' Lizards
贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in pos ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- bzoj3661
网络流/贪心 网络流做法是对于每一列,如果一个兔子下一天继续可以存在,那么连一条容量为1的边,然后设立一个中转站,来控制可以换的数量,容量限制l.时限100s,能跑过去我的太慢了,一个点100s 正解 ...
- Color a Tree & 排列
Color a Tree 题目链接 好不可做?可以尝试一下DP贪心网络流.DP 似乎没法做,网络流也不太行,所以试一下贪心. 考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它:否则,选完它父 ...
- 【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)
F. Gourmet and Banquet time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
随机推荐
- bat批处理积累
1 ::所有命令不回显,包含echo off自身也不回显 2 @echo off 3 4 ::rem或双冒号都为注释行 5 6 rem 变量赋值,注意变量和等号之间不能有空格,等号后的空格会作为变量值 ...
- postgresql中权限介绍
postgresql权限分为实例的权限,数据库的权限,模式的权限,对象的权限,表空间的权限 实例的权限:由pg_hba.conf文件控制,控制那些用户那些IP以哪种方式连接数据库 数据库的权限:是否允 ...
- 史上最全postgreSQL体系结构(转)
原文链接:https://cloud.tencent.com/developer/article/1469101 墨墨导读:本文主要从日志文件.参数文件.控制文件.数据文件.redo日志(WAL).后 ...
- 1.5V升3.3V芯片电路图,稳压3.3V供电MCU模块等
干电池1.5V可以升到3.3V,通过PW5100干电池升压IC,于外围3个元件:2个电容和一个电感即可组成1.5V升3.3V的电路系统. 干电池属于低能量的电池产品,不过一般使用到干电池的产品也是输出 ...
- .NET Core 问题记录
前言: 最近在项目中遇到了遇到了写部署步骤过多的问题,为了减少.net core项目部署步骤:需要对一些基础问题进行验证: 如端口设置.单页应用程序(angluar)合并部署方式等相关问题,特将解决过 ...
- 查看内核打印信息指令dmesg
linux系统启动的时候打印的的信息非常重要,有时候需要看这些信息但是又不想重启,可以用dmesg这条指令.
- MonkeyScript
MonkeyScript的简单使用 一. 什么是MonkeyScript MS 是官方提供的,除了像猴子一样随机乱点之外,还可以通过编写脚本的形式,完成一系列固定的操作.MS 提供一整套完善的 API ...
- 慕课网金职位 Python工程师 百度网盘下载
百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...
- IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示
IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示哈,在使用 @Autowired 时,今天又遇一坑,这俩 ...
- Git轻松入门1:本地仓库篇
什么是Git 版本控制系统 首先我们要明白,什么是版本控制系统(version control systems)? 版本控制系统,顾名思义,就是能控制文件处于哪个版本的系统. e.g. 你在博客园里编 ...