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 ...
随机推荐
- 使用Groovy构建DSL
DSL(Domain Specific Language)是针对某一领域,具有受限表达性的一种计算机程序设计语言. 常用于聚焦指定的领域或问题,这就要求 DSL 具备强大的表现力,同时在使用起来要简单 ...
- Linux发行版及其目标用户
1.Debian Debian 众所周知,是Deepin,Ubuntu和Mint等流行Linux发行版的母亲,这些发行版提供了可靠的性能,稳定性和无与伦比的用户体验.最新的稳定发行版是Debian 1 ...
- WPF 基础 - Binding 的源与路径
1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...
- 图文详解:内存总是不够,我靠HBase说服了Leader为新项目保驾护航
- 基于renren-fast的快速入门项目实战(实现报表增删改查)
基于renren-fast的快速入门项目实战(实现报表增删改查) 说明:renren-fast是一个开源的基于springboot的前后端分离手脚架,当前版本是3.0 官方开发文档需付费,对于新手而言 ...
- 简历求职:STAR法则
做了近2年的大学生就业辅导工作,也接触了很多即将走出校园的大学生,做个总结与大家分享,同时也是对自己的一个总结. 最近刚听说STAR法则,这也是一直我们给学生的指导思想,百度了一下: STAR法则,即 ...
- python网络编程TCP服务多客户端的服务端开发
#服务多客户端TCP服务端开发 2 #方法说明 3 """ 4 bind(host,port)表示绑定端口号,host是ip地址,ip地址一般不进 行绑定,表示本机的任何 ...
- Java 8 Stream API 详解
Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利.高效的聚合操作(aggregate operation),或者大批量数据操作 (b ...
- 攻防世界 reverse seven
seven hctf2018 这是一个驱动文件 ida载入,查找字符串 根据字符串来到函数:sub_1400012F0 __int64 __fastcall sub_1400012F0(__int6 ...
- SpringBoot-11 扩展功能
SpringBoot-11 扩展功能 异步 同步就是一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能算完成,这是一种可靠的任务序列.要么成功都成功,失败都失败,两个任务 ...