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 ...
随机推荐
- 订单和产品的多对多表关系在crudapi系统零代码实现
表关系管理 在上一篇序列号管理中,产品和销售订单都是孤立的单表,本文通过crudapi中表关系(relation)管理将多个表连接起来,形成一个整体. 概要 关系类型 表与表之间的关系(relatio ...
- Shell脚本控制docker容器启动顺序
1.遇到的问题 在分布式项目部署的过程中,经常要求服务器重启之后,应用(包括数据库)能够自动恢复使用.虽然使用docker update --restart=always containerid能够让 ...
- MySQL入门(5)——运算符
MySQL入门(5)--运算符 算术运算符 MySQL支持的算数运算符包括加.减.乘.除.求余. 符号 作用 + 加法运算 - 减法运算 * 乘法运算 / 除法运算 % 求余运算 DIV 除法运算,返 ...
- clickhouse 亿级数据性能测试
clickhouse 在数据分析技术领域早已声名远扬,如果还不知道可以 点这里 了解下. 最近由于项目需求使用到了 clickhouse 做分析数据库,于是用测试环境做了一个单表 6 亿数据量的性能测 ...
- Ignatius and the Princess III HDU - 1028
题目传送门:https://vjudge.net/problem/HDU-1028 思路:整数拆分构造母函数的模板题 1 //#include<bits/stdc++.h> 2 #incl ...
- HDU-6862 Hexagon (2020HDU 多校 D8 H)
1008 题意:半径为n的六边形(由半径为1的小六边形组成),从某一个小六边形出发有六个方向,找到一条转向次数最多的路径(用方向表示)遍历所有的六边形(一个六边形只访问一次). 题解:先画出n=3/4 ...
- Java中遍历集合的常用方法
一.List 1.普通for循环 for (int i = 0; i < list.size(); i++)){ String temp = (String)list.get(i); Syste ...
- Cloudreve 自建云盘实践,我说了没人能限得了我的容量和速度!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 为啥要用自建网盘,市面上的云盘不香了? 每一个用户需求的背后都是因为有场景存在,而这 ...
- 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之创建实例-12
自动化kolla-ansible部署ubuntu20.04+openstack-victoria之创建实例-12 欢迎加QQ群:1026880196 进行交流学习 实例创建 1. 创建 2. 查 ...
- GoF设计模式合集
1 概述 这篇文章是对GoF23种设计模式+1种非GoF模式的合集,由笔者自己的笔记整理而来,每个模式都详细描述了步骤,角色等,以及使用Java实现的具体的例子. 2 基础 设计模式概述 UML与面向 ...