您的位置:首页 > 其它

CSU 1631 Facility Locations

2015-08-29 17:05 393 查看


Description

The HDWBP Inc. has n clients and needs to service these clients by opening k facilities. Each opened facility can serve any number of clients and each client must be served by an open facility. There are m potential locations for these k facilities. The
cost of serving client j at potential location i is a non-negative integer cij . These costs satisfy a locality property: for two clients j and j’ and two facilities i and i’, we have cij ≤ ci’j + ci’j’ + cij’ . Given the costs, the CEO of HDWBP Inc. ultimately
wants to know the cheapest way to open k facilities and assign clients to these open facilities. For now, he needs your help to determine if it is possible to do this task without any cost (i.e. with cost zero).

Input

The input consists of a single test case. The first line contains three integers m, n, k where 1 ≤ m ≤ 100, 1 ≤ n ≤ 100 and 1 ≤ k ≤ m. Each of the next m lines contains n non-negative integers where the jth integer in the ith line is cij ≤ 10000.

Output

Display yes if it is possible to do the task with cost zero; otherwise, display no.

Sample Input

3 2 2
0 2
1 1
2 0

Sample Output

yes

HINT

Source

题意:从n行m列的矩阵中选出至多k行,使得每一列都有0

思路:暴力。每次循环,都找到当前能改变最大的某行,取这一行。

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int a[200][200];
int vis1[20000];
int vis2[20000];
int main(){
	int m,n,k;
	while(scanf("%d%d%d",&m,&n,&k) != EOF)
	{
		memset(vis1,0,sizeof(vis1));
		memset(vis2,0,sizeof(vis2));
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin >> a[i][j];
			}
		}
		int ans = 0;
		int flag;
		while(1)
		{
		int maxn = 0;
		int choose = -1;
		int cnt;
		for(int i = 0; i < m; i++)
		{
			cnt = 0;
			if(vis2[i]==1)continue;
			for (int j = 0; j < n; j++)
			{
				if(vis1[j]==1)continue;
				if (a[i][j] == 0)
					cnt++;
			}
			if(cnt>maxn)
			{
				maxn = cnt;
				choose = i;
			}
		}
		if(choose==-1)
		{
			flag = -1;
			break;
		}
		vis2[choose] = 1;
		for(int i=0;i<n;i++)
		if(a[choose][i]==0)vis1[i] = 1;
		ans++;
		
		int p;
		for(p = 0;p<n;p++)
		if(vis1[p]==0)break;
		if(p>=n)
		{
			flag = 1;
			break;
		}
		if(ans>=k)
		{
			flag = -1;
			break;
		}
	    }
		if (flag==1) cout << "yes\n";
		else cout << "no\n"; 
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: