题目链接:
http://acm.hust.edu.cn/vjudge/problem/88634
Facility Locations
题意
给你一个m*n的矩阵,上面的数满足cij ≤ ci′j + ci′j′ + cij′。现在要选出k行,使得这k行上面的0刚好能够覆盖所有的列。
样例
sample input
3 2 2
0 2
1 1
2 0
3 3 2
0 2 2
1 1 1
2 2 0sample output
yes
no
题解
“cij ≤ ci′j + ci′j′ + cij′”决定:如果两行在同一列都有零,那么这两行其他列的零的分布情况也会完全相同。所以我们可以枚举每一列,如果该列我们还没标记过,就任意选一个在该列为0的行(如果一个都找不到,那肯定无解),并标记该行其他列为0的位置,然后继续做,如果最后选k个以内就能做完,那么就输出yes,否则输出no。
代码
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
//start----------------------------------------------------------------------
const int maxn=111;
int vis[maxn];
int arr[maxn][maxn];
int n,m,k;
int main() {
while(scanf("%d%d%d",&n,&m,&k)==3){
rep(i,1,n+1){
rep(j,1,m+1){
scanf("%d",&arr[i][j]);
}
}
clr(vis,0);
int cnt=0,su=1;
rep(j,1,m+1){
if(vis[j]) continue;
rep(i,1,n+1){
if(arr[i][j]==0){
vis[j]=1;
cnt++;
rep(k,1,m+1){
if(arr[i][k]==0) vis[k]=1;
}
break;
}
}
if(!vis[j]){
su=0; break;
}
}
if(!su||cnt>k) puts("no");
else puts("yes");
}
return 0;
}
//end-----------------------------------------------------------------------