UVALive - 6868 Facility Locations 想法题

题目链接:

http://acm.hust.edu.cn/vjudge/problem/88634

Facility Locations


Time Limit: 3000MS

题意

给你一个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 0

sample 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-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5769304.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值