题意:

      有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二分最大流的更多相关文章

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

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

  2. poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点

    题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...

  3. HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    ...

  4. HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流

    二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...

  5. hdu4560 不错的建图,二分最大流

    题意: 我是歌手 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Subm ...

  6. POJ3228 并查集或二分最大流枚举答案

    忘记写题意了.这题题意:给出每个地点的金矿与金库的数量,再给出边的长度.求取最大可通过边长的最小权值使每个金矿都能运输到金库里. 这题和之前做的两道二分枚举最大流答案的问法很相识,但是这里用最大流速度 ...

  7. BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流

    1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...

  8. loj 1167(二分+最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26881 思路:我们可以二分最大危险度,然后建图,由于每个休息点只能 ...

  9. BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图

    这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...

随机推荐

  1. 冗余网络构建方案对比:VRRP协议、多网卡绑定及WN202冗余链路网卡

    在组建网络时为网络设计冗余方案已经成为提高网络可用性必不可少的一环,伴随着网络技术的发展实现网络冗余的技术方案也是层出不穷,例如应用于服务器端的HA.LB,应用于存储的SAN.DAS.NAS等.本文重 ...

  2. 微服务架构Day16-SpringBoot之监控管理

    监控管理使用步骤 通过引入spring-boot-starter-actuator,可以使用SpringBoot提供应用监控和管理的功能.可以通过HTTP,JMX,SSH协议来进行操作,自动得到审计, ...

  3. asp.net core 实现支持自定义 Content-Type

    asp.net core 实现支持自定义 Content-Type Intro 我们最近有一个原本是内网的服务要上公网,在公网上有一层 Cloudflare 作为网站的公网流量提供者,CloudFla ...

  4. RSA典型非对称加密算法

    私钥加密-->公钥解密,反之亦然,但不安全.也可以当做数字签名. public class RSACoder {         //非对称加密算法         public static  ...

  5. CSS水平布局

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  6. 一键自签本地 TLSv3 多域名 SAN 域名证书工具 HTTPS(最新版 Chrome 浏览器策略测试通过)

    一键自动生成本地自签名SAN域名证书工具 原生OpenSSL生成自签名SAN CA域名(V3签名),在Linux.MacOS系统下签发测试通过. 用于一键快速生成开发和测试场景证书,内部平台授权和私有 ...

  7. apktool 回编译报错:No resource identifier found for attribute 'xxxxxx' in package 'android' W:

    C:\xxxx\app-release\res\layout-v26\xxxx.xml:5: error: No resource identifier found for attribute 'xx ...

  8. 总结下js排序算法和乱序算法

    其实本人最怕的就是算法,大学算法课就感觉老师在讲天书,而且对于前端来说,算法在实际的应用中实在是很有限.毕竟算法要依靠大量的数据为基础才能发挥出算法的效率,就浏览器那性能,......是吧,退一万步说 ...

  9. 对象存储服务-Minio

    Mino 目录 Mino 对象存储服务 Minio 参考 Minio 架构 为什么要用 Minio 存储机制 纠删码 MinIO概念 部署 单机部署: Docker 部署Minio 分布式Minio ...

  10. 003-Java中的变量和数据类型

    @ 目录 一.变量 1.什么变量 2.变量的三要素 3.变量的命名规范 4.变量的分类 5.变量的作用域 6.变量的注意事项 二.数据类型 1.什么是数据类型 2.数据类型有什么用 3.数据类型的分类 ...