【NOIP2010】【codevs1069】关押罪犯(二分答案+二分图染色)

problem
  • 将n个罪犯分别关押进2座监狱
  • 每2个罪犯之间有一个冲突值,当他们在同一监狱时就会爆发
  • 让爆发的冲突值(最大的那个)最小,求那个最小值
solution
  • 考虑判定:是否存在一种分配方案,使最大的冲突值不超过mid。当mid较小时的可行方案对于更大的mid一定可行(在最小值的一侧都可行,另一侧不可行),换言之,本题具有单调性,可以二分。
  • 对于当前冲突值mid,任意两个冲突大于mid的罪犯都必然被安排到两个不同的监狱,所以我们染色判断是否是二分图即可。如果是二分图,r=mid,否则l=mid+1。
codes
#include<iostream>
#include<cstring>
#define maxn 20010
#define maxm 100010<<1
using namespace std;

//Grape
int tot=1, head[maxn], Next[maxm], ver[maxm], weight[maxm];
void AddEdge(int x, int y, int z){
    ver[++tot] = y;  weight[tot] = z;
    Next[tot] = head[x];  head[x] = tot;
    ver[++tot] = x;  weight[tot] = z;
    Next[tot] = head[y];  head[y] = tot;
}
int col[maxn], now;
bool dfs(int x){//染色
    for(int i = head[x]; i; i = Next[i]){
        int y = ver[i], w = weight[i];
        if(w > now){//只染大于mid的边构成的图
            if(col[x]==col[y])return false;
            if(col[y])continue;
            col[y] = 3-col[x];
            if(!dfs(y))return false;
        }
    }
    return true;
}

//Timu
int n, m;
bool check(int x){
    memset(col,0,sizeof(col));
    now = x;
    for(int i = 1; i <= tot; i++){
        if(!col[ver[i]] && weight[i]>x){
            col[ver[i]] = 1;
            if(dfs(ver[i]))continue;
            return false;
        }
    }
    return true;
}

int main(){
    cin>>n>>m;
    for(int i = 1; i <= m; i++){
        int x, y, z;
        cin>>x>>y>>z;
        AddEdge(x,y,z);
    }
    int l = 0, r = 1<<30;
    while(l < r){
        int mid = l+r>>1;
        if(check(mid))r = mid;
        else l = mid+1;
    }
    cout<<l<<'\n';
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小哈里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值