/**
题目: uvalive 3231 Fair Share 公平分配问题
链接:https://vjudge.net/problem/UVALive-3231
题意:有m个任务,n个处理器,每个任务有两个候选处理器,只要其中一个运行,该任务就能执行。
不同任务的两个候选处理器,至少有一个不同。 求任务数最多的那个处理器所分配的任务数尽量少。 思路:二分+最大流 左边是任务,s->u,cap = 1。
如果任务u和u的候选处理器v,u->v, cap = 1.
右边是处理器,二分mi。所有处理器v,v->t, cap = mi; 求s-t最大流flow,如果flow等于任务数,那么可行解。找一个最小的mi即可。
*/ #include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = ;///n+m=1000+10000=11000;
struct Edge{
int from, to, cap, flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N];
int cur[N]; void init(int n)
{
this->n = n;
for(int i = ; i <= n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS(){
memset(vis, , sizeof vis);
queue<int> Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++){
Edge &e = edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to] = ;
d[e.to] = d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a){
if(x==t||a==) return a;
int flow = , f;
for(int &i = cur[x]; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>){
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t){
this->s = s, this->t = t;
int flow = ;
while(BFS()){
memset(cur, , sizeof cur);
flow += DFS(s,INF);
}
return flow;
}
};
int main()
{
int T, n, m;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
int s = , t = n+m+;
Dinic dinic, save;
dinic.init(t);
int u , v;
for(int i = ; i <= m; i++){
scanf("%d%d",&u,&v);
dinic.AddEdge(s,i,);
dinic.AddEdge(i,u+m,);
dinic.AddEdge(i,v+m,);
}
save = dinic;
int lo = , hi = INF, mi, ans;
while(lo<=hi){
mi = (lo+hi)/;
dinic = save;
for(int i = ; i <= n; i++){
dinic.AddEdge(i+m,t,mi);
}
int flow = dinic.Maxflow(s,t);
if(flow==m){
ans = mi;
hi = mi-;
}else
{
lo = mi+;
}
}
printf("%d\n",ans);
}
return ;
}

uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。的更多相关文章

  1. UVALive 3231 Fair Share

    Fair Share Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origina ...

  2. LA 3231 - Fair Share

    You are given N processors and M jobs to be processed. Two processors are specified to each job. To ...

  3. uvalive 3231

    3231 - Fair ShareAsia - Seoul - 2004/2005You are given N processors and M jobs to be processed. Two ...

  4. 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)

    [题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...

  5. POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)

    POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...

  6. 三:Fair Scheduler 公平调度器

    参考资料: http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/FairScheduler.html http://h ...

  7. UVALive 3231 网络流

    题目要求给m个任务分配给n个机器,但最后任务量最多的那个机器的任务量尽量少,利用最大流,在最后的汇点那里设置关卡,二分结果,把机器到最终汇点的容量设置为该值,这样就达到题目条件,这样跑最大流 还能把m ...

  8. POJ 3057 Evacuation 二分+最大流

    Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...

  9. poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点

    题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...

随机推荐

  1. Spring MVC入门Demo

    1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程. 2  ...

  2. 教你用 google-drive-ocamlfuse 在 Linux 上挂载 Google Drive

    如果你在找一个方便的方式在 Linux 机器上挂载你的 Google Drive 文件夹, Jack Wallen 将教你怎么使用 google-drive-ocamlfuse 来挂载 Google ...

  3. 封装实现JavaScript 文件的动态载入功能

    function loadScript(url, callback){ var script = document.createElement ("script") script. ...

  4. java中异常介绍

    一.异常概述 异常处理已经成为衡量一门语言是否成熟的标准之一,目前的主流编程语言如C++.C#.Ruby.Python等,大都提供了异常处理机制.增加了异常处理机制后的程序有更好的容错性,更加健壮. ...

  5. std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

  6. git笔记之安装使用

    git是什么? 简介一下,Git是一个开源的分布式版本号控制系统,用以有效.快速的处理从非常小到非常大的项目版本号管理.Git是眼下世界上最先进的分布式版本号控制系统,没有传说中的之中的一个. Git ...

  7. Linux LVM硬盘管理及LVM扩容【转】

    原文地址:http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html 一.LVM简介 LVM是 Logical Volume Manag ...

  8. Java Collection之Queue具体解释及用途

    Queue是一种常见的数据结构,其主要特征在于FIFO(先进先出),Java中的Queue是这样定义的: public interface Queue<E> extends Collect ...

  9. oracle将一个表中字段的值赋值到另一个表中字段(批量)

    面积表中数据错误,现将面积表中的sfmj字段的值改为居民信息表中匹配字段的值 update (select s.name name1,s2.name name2 from simple s,simpl ...

  10. Ansible远程执行脚本示例

    首先创建一个shell脚本 cat /tmp/df.sh #!/bin/bash df -h|grep vda|awk '{print $5}' 然后把该脚本分发到各个机器上 ansible comp ...