CSU 1631 Facility Locations
2015-08-29 17:05
393 查看
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).
思路:暴力。每次循环,都找到当前能改变最大的某行,取这一行。
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. Thecost 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; }
相关文章推荐
- 边线设置为阴影效果
- C++类中成员变量的初始化
- * Delphi编程时候诡异地出现ORA-00937错误,记录解决它的思路和方法
- 判断鼠标移动事件
- 1073 -- Let the Balloon Rise
- 空字符 空格字符(字符) 空字符串 NULL的区别
- Qt 学习之路 2(3):Hello, world! 笔记
- 原型模式
- iOS 移除地图所有大头针
- 1072 -- Sum Problem
- 1071 -- A+B(8)
- 1070 -- A+B(7)
- HDU1584-A strange lift-最短路(Dijkstra模板题)
- Windows风格的Loading动画Android实现(3)
- 字符串匹配的KMP算法
- POJ_3040 Allowance(greedy)
- 15 腾讯春季软件测试招聘实习 试题
- linux的I/O多路转接select的fd_set数据结构和相应FD_宏的实现分析
- 1069 -- A+B(6)
- 远控木马的事情