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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
int a[m][n],b[m];//b[]记录每个人可以喂养多少动物
double c[m][n]; //动物占人喂养的权重
int f[m]; //记录m人还能喂养了多少动物
int i,j,k; //k为每个人能喂养的动物数
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cin>>k;
for(i=0;i<m;i++) f[i]=k;
for(i=0;i<m;i++) b[i]=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i] +=a[i][j];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j] =1.0*a[i][j]/b[i];
int flag1=1; //存在满足条件的
for(j=0;j<n && flag1;j++)
{
double max=0;
int maxid,flag2=0;
for(i=0;i<m;i++)
if(c[i][j]>max && f[i]>0)
{
max=c[i][j];
maxid=i;//找出权值最大的并还能喂养动物的人
flag2=1;//存在
}
if(!flag2) flag1=0;
else f[maxid]--;
}
if(flag1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}