WOJ1008-Feeding Animals(II)

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;  
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值