CODEVS 1069 关押罪犯

CODEVS 1069

题意 给你n个人 m条关系

每个关系代表如果 这两个人在一个监狱 那么会产生一个v的贡献 监狱长会看见最大的贡献 问你如何使得分配监狱贡献最小

我们这样来想 贡献具有单调性 如果mid 满足 那么mid + 1 肯定也是满足的 所以我们首先二分

然后二分的时候比这个值大的我们要建一条边 代表放两个监狱 然后加完边的无向图跑一个二分图判别即可判断这个情况是否合法 

合法就 使得 r = mid – 1 否则 l = mid + 1

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 20025;
const int MAX_M = 100025;
vector<int > G[MAX_N];
struct node
{
    int a,b,c;
}arr[MAX_M];
int col[MAX_N],n,m;
bool flag;
void dfs(int x,int v)
{
    col[x] = v;
    int sz = G[x].size();
    for(int i = 0;i<sz;++i)
    {
        int to =G[x][i];
        if(!col[to])
        {
            dfs(to,3-v);
        }
        else if(col[to]==col[x])
        {
            flag = false;
            break;
        }
    }
}
bool check(int mid)
{
    flag = true;
    vector<int > M[MAX_N];
    swap(G,M);
    for(int i = 1;i<=m;++i)
    {
        if(arr[i].c>mid)
        {
            G[arr[i].a].push_back(arr[i].b);
            G[arr[i].b].push_back(arr[i].a);
        }
    }
    memset(col,0,sizeof(int)*(n+1));
    for(int i = 1;i<=n;++i) if(!col[i]&&flag) dfs(i,1);
    if(flag)    return true;
    return false;
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=m;++i)
    {
        scanf("%d%d%d",&arr[i].a,&arr[i].b,&arr[i].c);
    }
    int l = 0,r = inf;
    while(l<=r)
    {
        int mid = (l+r)>>1;
        if(check(mid)) r = mid - 1;
        else l = mid + 1;
    }
    printf("%d\n",l);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

    原文作者:犯罪团伙问题
    原文地址: https://blog.csdn.net/heucodesong/article/details/88749209
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞