uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/**
题目: 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 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。的更多相关文章
- UVALive 3231 Fair Share
Fair Share Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origina ...
- LA 3231 - Fair Share
You are given N processors and M jobs to be processed. Two processors are specified to each job. To ...
- uvalive 3231
3231 - Fair ShareAsia - Seoul - 2004/2005You are given N processors and M jobs to be processed. Two ...
- 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)
[题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...
- 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 ...
- 三:Fair Scheduler 公平调度器
参考资料: http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/FairScheduler.html http://h ...
- UVALive 3231 网络流
题目要求给m个任务分配给n个机器,但最后任务量最多的那个机器的任务量尽量少,利用最大流,在最后的汇点那里设置关卡,二分结果,把机器到最终汇点的容量设置为该值,这样就达到题目条件,这样跑最大流 还能把m ...
- POJ 3057 Evacuation 二分+最大流
Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
随机推荐
- utf8汉字编码16进制对照(转载)
utf8汉字编码16进制对照 GB Unicode UTF-8 Chinese CharacterCode code# Code (coded in UTF-8)D2BB 4E00 E4 B8 80 ...
- UVa 615 - Is It A Tree?
题目:给你一些有向边(端点,长度为1).推断给定的图是否是一棵树. 分析:图论.并查集.树是一个全部点都连接的有向无环图(不连接的是森林). 依照树的定义推断是否有环就可以,有环分成两种: 1.链状环 ...
- Index 和 Type 的区别
原文: Index vs. Type By Adrien Grand 译者: fengchang 对于 ES 的新用户来说,有一个常见的问题:要存储一批新的数据时,应该在已有 index 里新建一个 ...
- MR 数据过滤
MR: package com.euphe.filter; import com.euphe.util.HUtils; import com.euphe.util.Utils; import com. ...
- IT词汇表
本人采集到了数十万篇中文技术类博客,进行分词后根据出现的词频手工整理了一份IT词汇表,共计12000个,基本囊括了常见的中英文IT词汇,欢迎各位提出交流意见. 点此 下载
- [Functional Programming Moand] Update The State Of A State Monad (put)
Stateful computations require the ability for their state to change overtime. We take a look on one ...
- Short与Integer互转
int 是4字节, short 是2字节的, 如果将int(Integer)转成short(Short), 那么必须强制转换,否则会报编译异常. 但是, 当int(Integer)是一个final时, ...
- redis学习笔记——RDB和AOF持久化一
为防止数据丢失,需要将 Redis 中的数据从内存中 dump 到磁盘,这就是持久化.Redis 提供两种持久化方式:RDB 和 AOF.Redis 允许两者结合,也允许两者同时关闭. RDB 可以定 ...
- 文字溢出显示省略号,兼容ie9以上浏览器
最近遇上一个问题,文字溢出展示省略号在ie上不管用,经过查找,是少了word-wrap: normal;这个css属性 width:100px; overflow: hidden; white-spa ...
- 关于iOS上使用WWW引起的内存泄漏的临时解决方案
原地址:http://www.unity蛮牛.com/thread-16493-1-1.html 目前,在的4.3.3.和4.3.4版本中存在一个iOS平台上的内存泄漏问题,即当使用WWW来下载和加载 ...