POJ3228二分最大流
题意:
有n个点,每个点有两个权值,金子数量还有仓库容量,金子可以存在自己的仓库里或者是别的仓库里,仓库和仓库之间有距离,问所有金子都必须存到库里最大距离的最小是多少?
思路:
比较简单,方法也不唯一,大体可以这样,先二分,然后用最大流或者匹配..来判断是不是满足就行了,我用的是二分最大流,具体代码在下面。
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N_node 400 + 50
#define N_edge 200 * 200 * 2 + 10000
#define INF 1000000000
using namespace std;
typedef struct
{
int to ,cost ,next;
}STAR;
typedef struct
{
int a ,b ,c;
}EDGE;
typedef struct
{
int x ,t;
}DEP;
STAR E[N_edge];
EDGE edge[N_edge];
DEP xin ,tou;
int list[N_node] ,listt[N_node] ,tot;
int deep[N_node];
int c1[N_node] ,c2[N_node];
int num[N_edge] ,numt[N_edge];
void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
E[++tot].to = a;
E[tot].cost = c;
E[tot].next = list[b];
list[b] = tot;
}
bool BFS_DEEP(int s ,int t ,int n)
{
memset(deep ,255 ,sizeof(deep));
xin.x = s ,xin.t = 0;
queue<DEP>q;
q.push(xin);
deep[s] = 0;
while(!q.empty())
{
tou = q.front();
q.pop();
for(int k = list[tou.x] ;k ;k = E[k].next)
{
xin.x = E[k].to;
xin.t = tou.t + 1;
if(deep[xin.x] != -1 || !E[k].cost)
continue;
deep[xin.x] = xin.t;
q.push(xin);
}
}
for(int i = 0 ;i <= n ;i ++)
listt[i] = list[i];
return deep[t] != -1;
}
int minn(int x ,int y)
{
return x < y ? x : y;
}
int DFS_Flow(int s, int t ,int flow)
{
if(s == t) return flow;
int nowflow = 0;
for(int k = listt[s] ;k ;k = E[k].next)
{
listt[s] = k;
int c = E[k].cost ,to = E[k].to;
if(!c || deep[to] != deep[s] + 1)
continue;
int tmp = DFS_Flow(to ,t ,minn(c ,flow - nowflow));
nowflow += tmp;
E[k].cost -= tmp;
E[k^1].cost += tmp;
if(nowflow == flow) break;
}
if(!nowflow) deep[s] = 0;
return nowflow;
}
int DINIC(int s ,int t ,int n)
{
int ans = 0;
while(BFS_DEEP(s ,t ,n))
{
ans += DFS_Flow(s ,t ,INF);
}
return ans;
}
int GetMaxFlow(int mid ,int n ,int m)
{
memset(list ,0 ,sizeof(list));
tot = 1;
for(int i = 1 ;i <= n ;i ++)
{
add(0 ,i ,c1[i]);
add(i ,i + n ,INF);
add(i + n ,n + n + 1 ,c2[i]);
}
for(int i = 1 ;i <= m ;i ++)
{
if(edge[i].c <= mid)
{
add(edge[i].a ,edge[i].b + n ,INF);
add(edge[i].b ,edge[i].a + n ,INF);
}
}
return DINIC(0 ,n + n + 1 ,n + n + 1);
}
int main ()
{
int n ,m ,i ,s1 ,s2;
while(~scanf("%d" ,&n) && n)
{
s1 = s2 = 0;
for(i = 1 ;i <= n ;i ++)
{
scanf("%d" ,&c1[i]);
s1 += c1[i];
}
for(i = 1 ;i <= n ;i ++)
{
scanf("%d" ,&c2[i]);
s2 += c2[i];
}
scanf("%d" ,&m);
for(i = 1 ;i <= m ;i ++)
{
scanf("%d %d %d" ,&edge[i].a ,&edge[i].b ,&edge[i].c);
numt[i] = edge[i].c;
}
if(s2 < s1)
{
printf("No Solution\n");
continue;
}
sort(numt + 1 ,numt + m + 1);
int numid = 0;
for(i = 1 ;i <= m ;i ++)
if(i == 1 || numt[i] != numt[i-1])
num[++numid] = numt[i];
num[0] = 0;
int low = 0 ,up = numid ,mid ,ans = -1;
while(low <= up)
{
mid = (low + up) >> 1;
if(GetMaxFlow(num[mid] ,n ,m) == s1)
{
ans = num[mid];
up = mid - 1;
}
else low = mid + 1;
}
if(ans == -1) printf("No Solution\n");
else printf("%d\n" ,ans);
}
return 0;
}
POJ3228二分最大流的更多相关文章
- uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流
二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...
- hdu4560 不错的建图,二分最大流
题意: 我是歌手 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Subm ...
- POJ3228 并查集或二分最大流枚举答案
忘记写题意了.这题题意:给出每个地点的金矿与金库的数量,再给出边的长度.求取最大可通过边长的最小权值使每个金矿都能运输到金库里. 这题和之前做的两道二分枚举最大流答案的问法很相识,但是这里用最大流速度 ...
- BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流
1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
- loj 1167(二分+最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26881 思路:我们可以二分最大危险度,然后建图,由于每个休息点只能 ...
- BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图
这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...
随机推荐
- 创建一个scrapy爬虫框架的项目
第一步:打开pycharm,选择"terminal",如图所示: 第二步:在命令中端输入创建scrapy项目的命令:scrapy startproject demo (demo指的 ...
- java 流程控制学习
https://www.kuangstudy.com/course 用户交互Scanner import java.util.Scanner; public class Demo01 { public ...
- 使用当前主流的github管理项目代码(记我的第一次项目创建)
先创建一个github的账号 网址:https://github.com/ 然后下载一个git工具并安装 网址:https://gitforwindows.org/ 下载安装注册完成后, 创建一个新的 ...
- FreeBSD 开发已经迁移至 git
FreeBSD 开发已经迁移至 git 全部预计于 2021 年 3 月完成迁移. https://git.freebsd.org/src.git 或者 https://cgit.freebsd.or ...
- FreeBSD ports 基本用法
首先获取portsnap#portsnap fetch extract---------------------------------------使用whereis 查询软件地址如#whereis ...
- Python读写配置文件模块--Configobj
一.介绍 我们在项目的开发过程中应该会遇到这样的问题:我们的项目读取某个配置文件,然后才能按照配置的信息正常运行服务,当我们需要对修改服务的某些信息时,可以直接修改这个配置文件,重启服务即可,不用再去 ...
- 七种join的书写规范
在mysql中的两表进行连接时,总共有7种连接情况,具体可见下图 由图的从左到右的顺序 图1.左连接(left join):返回左表中的所有记录和右表中的连接字符字段相等的记录,若右表没有匹配值则补N ...
- PTA 冒泡排序
6-4 冒泡排序 (10 分) 编程实现冒泡排序函数.void bubbleSort(int arr[], int n);.其中arr存放待排序的数据,n为数组长度(1≤n≤1000). 函数接口 ...
- 80%的人都不会的,15个Linux实用技巧
熟悉 Linux 系统的同学都知道,它高效主要体现在命令行.通过命令行,可以将很多简单的命令,通过自由的组合,得到非常强大的功能. 命令行也就意味着可以自动化,自动化会使你的工作更高效,释放很多手工操 ...
- Istio 故障注入之延时(fixedDelay)
Istio 故障注入 Istio 故障注入与其他在网络层引入错误(例如延迟数据包或者直接杀死 Pod)的机制不同,Istio 允许在应用程序层注入故障.这使得可以注入更多相关的故障,比如 HTTP 错 ...