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-贪心-网络流的更多相关文章

  1. 【BZOJ3716】[PA2014]Muzeum(贪心+网络流)

    BZOJ 题意: 在二维网格图中有\(n\)个物品,每个物品有价值:但有\(m\)个警卫看管这些物品,每个警卫面朝\(y\)轴负方向,能看到一定角度(假定能够看到无穷远). 现在每个敬畏有一个贿赂价钱 ...

  2. 洛谷P1251 餐巾(网络流)

    P1251 餐巾 15通过 95提交 题目提供者该用户不存在 标签网络流贪心 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 为什么我全部10个测试点都对… 题目描述 一个餐厅在相继的N天里 ...

  3. 洛谷P2460 [SDOI2007]科比的比赛(题解)(贪心+搜索)

    科比的比赛(题解)(贪心+搜索) 标签:算法--贪心 阅读体验:https://zybuluo.com/Junlier/note/1301158 贪心+搜索 洛谷题目:P2460 [SDOI2007] ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. 转自 Good morning 的几句精辟的话

    1.志愿者招募 根据流量平衡方程来构图非常方便,而且简单易懂,以后可能成为做网络流的神法之一 简单记一下流量平衡方程构图法的步骤: a.列出需求不等式 b.通过设置松弛变量,将不等式变成等式 c.两两 ...

  6. 【HDOJ】2732 Leapin' Lizards

    贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in pos ...

  7. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  8. bzoj3661

    网络流/贪心 网络流做法是对于每一列,如果一个兔子下一天继续可以存在,那么连一条容量为1的边,然后设立一个中转站,来控制可以换的数量,容量限制l.时限100s,能跑过去我的太慢了,一个点100s 正解 ...

  9. Color a Tree & 排列

    Color a Tree 题目链接 好不可做?可以尝试一下DP贪心网络流.DP 似乎没法做,网络流也不太行,所以试一下贪心. 考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它:否则,选完它父 ...

  10. 【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)

    F. Gourmet and Banquet time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

随机推荐

  1. VB基础总结

    前段时间用VB写了一个简单窗口小应用,久了不碰VB,都忘了,下面用思维导图简单总结了一些基础的东西,方便以后快速查阅.

  2. Centos6.9安装ACFS

    安装完oracle 11GR2的RAC后,使用asmca打开图形化界面后,发现Volumes和ASM Cluster File System两个选项卡不能用 原因是因为ACFS不支持CentOS 解决 ...

  3. 为什么[] == false 为true

    首先要讲一下js的数据类型分为: 1.基本数据类型(原始数据类型):String.Boolean.Number.null.undefined.Symbol 2.引用数据类型:Object.Array. ...

  4. 利用vbs隐藏dos窗口

    方法一: option explicitdim wshshellset wshshell=wscript.createobject("wscript.shell")wshshell ...

  5. SAP中的密码输入框

    在SAP中的密码输入框,可分为两种情况: 1.用selection语句书写的选择屏幕上的密码输入框 实现的方式就是在AT SELECTION-SCREEN OUTPUT事件中写入如下代码: LOOP ...

  6. ElasticSearch极简入门总结

    一,目录 安装es 项目添加maven依赖 es客户端组件注入到spring容器中 es与mysql表结构对比 索引的删除创建 文档的crud es能快速搜索的核心-倒排索引 基于倒排索引的精确搜索. ...

  7. docker mysql 设置忽略大小写

    使用docker 安装mysql时  Linux下是默认不忽略大小写,导致操作数据库的时候会报如下错误 为了解决上面的问题,我们在创建MySQL容器的时候就需要初始化配置  lower_case_ta ...

  8. PWN_ret2text,ret2syscall,ret2shellcode

    首先了解下Linux中的保护机制(具体的绕过等后续再说) 1.canary(栈保护) 在函数开始时就随机产生一个值,将这个值CANARY放到栈上紧挨ebp的上一个位置,当攻击者想通过缓冲区溢出覆盖eb ...

  9. 今天聊点干货—关于CSS样式来源

    样式来源 CSS样式共有5个来源,它们分别是\(\color{#FF3030}{浏览器默认样式}\).\(\color{#FF3030}{用户样式}\).\(\color{#FF3030}{链接样式} ...

  10. Vue基础之插值表达式的另一种用法!附加变量的监听!

    Vue基础之插值表达式的另一种用法!附加变量的监听! 讲这个之前我们先回顾一下原来的用法! <body> <!-- Vue.js的应用可以分为两个重要的组成部分 一个是视图! 另一个 ...