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 ...
随机推荐
- python异步回调顺序?是否加锁?
话不多说,直接上代码: import time from functools import partial from concurrent.futures.process import Process ...
- spring boot项目问题汇总
spring遇到的问题汇总 有关日志的打印和日志如何使用 在实际项目中,我们的程序都是运行在linux上,有错误时也不能在本地的控制台上直观看到,所有合理打印日志对于程序员迅速定位到错误. 打印日志时 ...
- floating point
记录浮点数的单精度和双精度(IEEE754) 1.单精度(float) 1.定义:单精度占4字节/32位,其中1号位符号位,其次是8位阶码/指数(阶符+阶数),23位尾数(小数). 2.双精度(d ...
- STL_list容器
一.List简介 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的. 链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每 ...
- 转 Jmeter测试实践:文件上传接口
Jmeter测试实践:文件上传接口 1.打开jmeter4.0,新建测试计划,添加线程组.根据实际情况配置线程属性. 2.添加HTTP请求. Basic部分修改如下: Advanced部分我做任何 ...
- uni-app请求uni.request封装使用
对uni.request的一些共同参数进行简单的封装,减少重复性数据请求代码.方便全局调用. 先在目录下创建 utils 和 common 这2个文件夹 utils 是存放工具类的,common 用来 ...
- TCP半连接队列和全连接
概述 如上图所示, 在TCP三次握手中,服务器维护一个半连接队列(sync queue) 和一个全连接队列(accept queue). 当服务端接收到客户端第一次SYN握手请求时,将创建的req ...
- Apache Http Server 一些资料
配置虚拟主机的例子: http://httpd.apache.org/docs/current/vhosts/examples.html
- 存储型XSS
DVWA系列(二)存储型XSS https://www.imooc.com/article/284686 网络安全:存储型XSS https://blog.csdn.net/qq_41500251/a ...
- 最简单直接地理解Java软件设计原则之单一职责原则
理论性知识 定义 单一职责原则, Single responsibility principle (SRP): 一个类,接口,方法只负责一项职责: 不要存在多余一个导致类变更的原因: 优点 降低类的复 ...