UVALive 3231 网络流
题目要求给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 网络流的更多相关文章
- uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...
- UVALive 3231 Fair Share
Fair Share Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origina ...
- uvalive 3231
3231 - Fair ShareAsia - Seoul - 2004/2005You are given N processors and M jobs to be processed. Two ...
- 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 ...
- 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 ...
- UVALive 4949 Risk(二分网络流、SAP)
n个区域,每个区域有我方军队a[i],a[i]==0的区域表示敌方区域,输入邻接矩阵.问经过一次调兵,使得我方边界处(与敌军区域邻接的区域)士兵的最小值最大.输出该最大值.调兵从i->j仅当a[ ...
- UVaLive 4597 Inspection (网络流,最小流)
题意:给出一张有向图,每次你可以从图中的任意一点出发,经过若干条边后停止,然后问你最少走几次可以将图中的每条边都走过至少一次,并且要输出方案,这个转化为网络流的话,就相当于 求一个最小流,并且存在下界 ...
- UVALive 7264 Kejin Game 网络流+最小割
Kejin Game 题意:一个人有一颗技能树, 现在它想修练到某个技能 (假设为x), 现在修一个技能有3种方式: 1, 将该技能的前置技能都学完了,才能学该技能. 2, 取消一个技能 与 另一个技 ...
- 【UVALive - 3487】 Duopoly(网络流-最小割)
Description The mobile network market in country XYZ used to be dominated by two large corporations, ...
随机推荐
- Js 类继承 extends
html 及 js 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- System.getProperty() 获取系统变量
例:获取用户当前的工作目录 String CONFPREFIXURL = System.getProperty("user.dir"); System.out.println(CO ...
- wx小程序笔记
目录 p18 事件绑定1 p19 事件绑定2 btn p20+ view 相关,wxss,less,css 零基础玩转微信小程序[黑马程序员] https://www.bilibili.com/vid ...
- C语言-断言
1 作用: 断言常做语言处理的高级形式,自动处理软件隐藏很深其且它手段不易发现的错误,快速进行异常定位.同时这也是软件单元测试必须的技术. 2 使用范围: 2.1放在函数入口对入口参数进行合法性检查( ...
- Web性能测试工具推荐
WEB性能测试工具主要分为三种: 一种是测试页面资源加载速度的: 一种是测试页面加载完毕后页面呈现.JS操作速度的: 一种是总体上对页面进行评价分析. ~~~如果谁有更好的工具也请一起分享下 1. ...
- case语句!
1.case 语句概述(1)case 语句的作用使用 case 语句改写 if 多分支可以使脚本结构更加清晰.层次分明.针对变量的不同取值,执行不同的命令序列.2.case 语句的结构:case 变量 ...
- 安装scikit-learn
首先到官网下载安装 python ,之后下载setuptools 进行安装. 'python' 不是内部或外部命令 可运行 set PATH=%PATH%;C:\Python34 安装完成之后,运行 ...
- Mybatis遇到的报错
MyBatis遇到的报错: 1.Caused by: org.xml.sax.SAXParseException; lineNumber: 35; columnNumber: 17; 元素类型为 &q ...
- 软件工程 - Test-Driven Development (TDD),测试驱动开发
参考 https://baike.baidu.com/item/%E6%B5%8B%E8%AF%95%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/3328831?fr=al ...
- Thread的join方法
一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程. Thread.Join方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行. 如下案列: 当NewT ...