题意:

      有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. 人脸识别分析小Demo

    人脸识别分析 调用 腾讯AI人脸识别接口 测试应用 纯py文件测试照片 # -*- coding: utf-8 -*- import json from tencentcloud.common imp ...

  2. redis一句话木马控电脑

      (1)在redis管理工具内写入木马并保存: 输入命令行: config set dbfilename shell.php set shell "<?php @assert($_P ...

  3. 线上MySQL读写分离,出现写完读不到问题如何解决

    大家好,我是历小冰. 今天我们来详细了解一下主从同步延迟时读写分离发生写后读不到的问题,依次讲解问题出现的原因,解决策略以及 Sharding-jdbc.MyCat 和 MaxScale 等开源数据库 ...

  4. 001-HashMap源码分析

    HashMap源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如 memcached)的核心其实就是在内存中维护一张大的哈希表. 一.什 ...

  5. HDU_3359 Kind of a Blur 【浮点型高斯消元+BFS】

    一.题目 Kind of a Blur 二.分析 题目读起来挺费劲的. 主要就是要求一个矩阵,其中每个点及其于这个的曼哈顿距离小于D的点的值总和的平均值就是新生成的矩阵. 给定新生成的矩阵,求初始矩阵 ...

  6. 【ZeyFraのJavaEE开发小知识05】Mybatis-Plus & Axios

    关于如何在Mybatis-Plus中添加SQL拦截器 之前ZeyFra在MyBatis-Plus[踩坑记录01]一文中提到过,使用Mybatis-Plus时最好使用MybatisSqlSessionF ...

  7. POJ1562_Oil Deposits(JAVA语言)

    思路:bfs.水题,标记下计数就完了. Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22928 ...

  8. x64 下记事本WriteFile() API钩取

    <逆向工程核心原理>第30章 记事本WriteFile() API钩取 原文是在x86下,而在x64下函数调用方式为fastcall,前4个参数保存在寄存器中.在原代码基础上进行修改: 1 ...

  9. [换根DP]luogu P3647 [APIO2014]连珠线

    题面 https://www.luogu.com.cn/problem/P3647 不重复地取树中相邻的两条边,每次得分为两条边权和,问最大得分 分析 容易想到状态 f[i][0/1] 分别表示 i ...

  10. 使用oracle序列+oracle定时任务获取每月从1开始的流水码

    --创建序列 --入库create sequence rk_seq;--出库create sequence ck_seq;--移库create sequence yk_seq; --创建存储过程 cr ...