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

用的dinic

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int n,m;
int r1[10010],r2[10010];
struct Edge
{
int from,to,cap,flow;
};
const int maxn = 20000;
struct Dinic
{ int s,t,m;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n)
{
for (int i=0;i<=n;i++){
G[i].clear();
}
edges.clear();
}
void addedge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs(){
memset(vis,0,sizeof vis);
queue<int>Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while (!Q.empty())
{
int u=Q.front();Q.pop();
for (int i=0;i<G[u].size();i++){
Edge& e=edges[G[u][i]];
if (!vis[e.to] && e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[u]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if (x==t || a==0 ) return a;
int flow=0,f;
for (int& i=cur[x];i<G[x].size();i++){
Edge& e =edges[G[x][i]];
if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if (a==0) break;
}
}
return flow;
}
int maxflow(int s,int t){
this->s=s;this->t=t;
int flow=0;
while (bfs()){
memset(cur,0,sizeof cur);
flow+=DFS(s,10000000);
}
return flow;
}
}dinic;
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
scanf("%d",&m);
for (int i=1;i<=m;i++) scanf("%d%d",&r1[i],&r2[i]); int l=0,r=m+1,mid;
int ans=0;
while (l<r)
{
mid=(l+r)>>1;
dinic.init(n+m+2);
for (int i=1;i<=m;i++){
dinic.addedge(0,i,1);
dinic.addedge(i,r1[i]+m,1);
dinic.addedge(i,r2[i]+m,1);
}
for (int i=1;i<=n;i++) dinic.addedge(i+m,n+m+1,mid);
int res=dinic.maxflow(0,n+1+m);
if (res==m){
r=mid;
ans=mid;
}
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}

  

UVALive 3231 网络流的更多相关文章

  1. uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。

    /** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...

  2. UVALive 3231 Fair Share

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

  3. uvalive 3231

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

  4. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  5. POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)

    POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...

  6. UVALive 4949 Risk(二分网络流、SAP)

    n个区域,每个区域有我方军队a[i],a[i]==0的区域表示敌方区域,输入邻接矩阵.问经过一次调兵,使得我方边界处(与敌军区域邻接的区域)士兵的最小值最大.输出该最大值.调兵从i->j仅当a[ ...

  7. UVaLive 4597 Inspection (网络流,最小流)

    题意:给出一张有向图,每次你可以从图中的任意一点出发,经过若干条边后停止,然后问你最少走几次可以将图中的每条边都走过至少一次,并且要输出方案,这个转化为网络流的话,就相当于 求一个最小流,并且存在下界 ...

  8. UVALive 7264 Kejin Game 网络流+最小割

    Kejin Game 题意:一个人有一颗技能树, 现在它想修练到某个技能 (假设为x), 现在修一个技能有3种方式: 1, 将该技能的前置技能都学完了,才能学该技能. 2, 取消一个技能 与 另一个技 ...

  9. 【UVALive - 3487】 Duopoly(网络流-最小割)

    Description The mobile network market in country XYZ used to be dominated by two large corporations, ...

随机推荐

  1. Codeforces Round #592 (Div. 2)G(模拟)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...

  2. php学习 打星星

    <?php // 输入几行 $k=9; // 打孔三角 for($i=1;$i<=$k;$i++){ if($i==$k){ for($n=1;$n<=$k*2-1;$n++){ e ...

  3. SpringBoot实现restuful风格的CRUD

    restuful风格: 百度百科: RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义.RESTFUL适用于移动互联网厂商作为业务使能接口的场 ...

  4. 深入JAVA注解-Annotation(学习过程)

    JAVA注解-Annotation学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...

  5. 电影推荐算法---HHR计划

    1,先看FM部分. 2,看看冷启动. 0,热门召回源. 1,男女召回源,年龄召回源,职业召回源,score最高. 2,男女年龄职业相互组合: 3,存入redis.天级别更新. 3,召回+排序先搞懂. ...

  6. PTA的Python练习题(二)

    继续在PTA上练习Python (从 第2章-5 求奇数分之一序列前N项和  开始) 1. x=int(input()) a=i=1 s=0 while(i<=x): s=s+1/a a=a+2 ...

  7. Hibernate框架报错:org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.mikey.hibernate.domain.Person.pid

    报错信息 org nate.PropertyAccessException:IllegalArgumentException在调用com.mikey.Hibernate.domain.Person.p ...

  8. Re库的基本使用

    # Re库的主要功能函数 """ re.search() 在一个字符串中搜索匹配正则表达式的第一个位置, 返回match对象 re.match() 在一个字符串的开始位置 ...

  9. c数据结构链式存储

    #include "stdafx.h" #include "stdio.h" #include "string.h" #include &q ...

  10. what is 'linesize alignment' meaning?

    链接: https://stackoverflow.com/questions/35678041/what-is-linesize-alignment-meaning