题意:

      有一个人,他有m个猪圈,每个猪圈里面有一定数量的猪,但是每个猪圈的门都是锁着的,他自己没有钥匙,只有顾客有钥匙,一天依次来了n个顾客,(记住是依次来的)他们每个人都有一些钥匙,和他们想买猪的数量,他们可以用自己的钥匙打开猪圈,最后卖给他多少是你自己决定的,当顾客打开它能打开的这几个猪圈的时候你可以调整打开的这几个猪圈里面的猪的数量,等他走了,猪圈又被锁上了,就这样,最后问你最多能卖给这n个人多少头猪。

思路:

      最大流,建图挺简单的(很早以前我做过这个题目,当时写的很麻烦,哎!记得那天我在不停的修改又难看又笨拙的代码,那是我逝去的青春...哈哈不扯了,继续),我的做法是先开一个数组hash[],hash[i]表示的是当前猪圈对应的虚拟的点是那个点,一会在解释虚拟的点的问题,然后我们虚拟出来两个点,原点s,和汇点t

然后:

(1) s 向所有的顾客连接一条边 流量是顾客要买的猪的数量

(2) 所有的猪圈向t连接一条边,流量是猪圈的猪的数量

(3) 对于每一个顾客,我们首先虚拟出来一个点,然后把这个顾客所有连接的点都指向这个    虚拟的点,比如当      前的这个顾客有三把钥匙1,2,3,因为这三个猪圈可以直接调整数量    了,所以我们可以让虚拟出来的这个点       a代替当前这步的三个点,1->a ,2->a ,3->a,然    后在更新上面说的那个hash[],hash[1] = a ,hash[2] = a      
         ,hash[3] = a,以后只要是    访问1,2,3中的任何一个,直接访问a,就行了,然后在建立一条当前顾客到新虚拟      出来     的这个点的边,流量INF。

ok,就是以上那些,要是不明白可以自己按照上面的见图思路画个图,很容易理解。


#include<stdio.h>
#include<string.h>
#include<queue> #define N_node 5000
#define N_edge 500000
#define INF 1000000000 using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; typedef struct
{
int x ,t;
}DEP; STAR E[N_edge];
DEP xin ,tou;
int list[N_node] ,listt[N_node] ,tot;
int deep[N_node] ,hash[N_node] ,key[N_node]; 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 = 0;
E[tot].next = list[b];
list[b] = tot;
} int minn(int x ,int y)
{
return x < y ? x : y;
} bool BFS_Deep(int s ,int t ,int n)
{
memset(deep ,255 ,sizeof(deep));
xin.x = s ,xin.t = 0;
deep[xin.x] = xin.t;
queue<DEP>q;
q.push(xin);
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 DFS_Flow(int s ,int t ,int flow)
{
if(s == t) return flow;
int nowflow = 0;
for(int k = list[s] ;k ;k = E[k].next)
{
listt[s] = k;
int to = E[k].to ,c = E[k].cost;
if(deep[to] != deep[s] + 1 || !E[k].cost)
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 main ()
{
int n ,m ,k ,i ,j;
int a ,b ,c;
while(~scanf("%d %d" ,&m ,&n))
{
int s = 0 ,t = n + m + 1 ,now_node = n + m + 1;
memset(list ,0 ,sizeof(list)) ,tot = 1;
for(i = 1 ;i <= m ;i ++)
{
scanf("%d" ,&a);
add(n + i ,t ,a);
}
for(i = 1 ;i <= N_node ;i ++)
hash[i] = i;
for(i = 1 ;i <= n ;i ++)
{
scanf("%d" ,&k);
now_node ++;
for(j = 1 ;j <= k ;j ++)
{
scanf("%d" ,&a);
int tmp = a + n;
add(now_node ,hash[tmp] ,INF);
hash[tmp] = now_node;
}
scanf("%d" ,&a);
add(s ,i ,a);
add(i ,now_node ,INF);
}
printf("%d\n" ,DINIC(s ,t ,now_node));
}
return 0;
}

POJ1149 PIGS(最大流)的更多相关文章

  1. POJ1149 PIGS [最大流 建图]

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 Description ...

  2. poj1149 PIGS 最大流(神奇的建图)

    一开始不看题解,建图出错了.后来发现是题目理解错了.  if Mirko wants, he can redistribute the remaining pigs across the unlock ...

  3. POJ1149 PIGS 【最大流 + 构图】

    题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  4. POJ 1149 - PIGS - [最大流构图]

    Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...

  5. 【BZOJ1280】Emmy卖猪pigs 最大流

    [BZOJ1280]Emmy卖猪pigs Description Emmy在一个养猪场工作.这个养猪场有M个锁着的猪圈,但Emmy并没有钥匙.顾客会到养猪场来买猪,一个接着一个.每一位顾客都会有一些猪 ...

  6. POJ-1149 PIGS---最大流+建图

    题目链接: https://vjudge.net/problem/POJ-1149 题目大意: M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买这些有钥匙的猪圈里的猪,而且要买一定数量的猪,每 ...

  7. POJ1149 PIGS (网络流)

                                                                             PIGS Time Limit: 1000MS   M ...

  8. POJ1149 PIGS

    想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...

  9. poj 1149 pigs ---- 最大流

    题意以及分析:http://ycool.com/post/zhhrrm6#rule3 主要是建图,简化图,然后在套最大流的模板. #include <iostream> #include& ...

随机推荐

  1. MySQL时间戳unix_timestamp

    函数:FROM_UNIXTIME作用:将MYSQL中以INT(11)存储的时间以"YYYY-MM-DD"格式来显示.语法:FROM_UNIXTIME(unix_timestamp, ...

  2. 反射的常用API

    反射的常用API 加载程序集 Assembly assembly = Assembly.Load("程序集名称"); // 从前目录加载程序集,提供程序集名称,无后缀 Assemb ...

  3. 【python+selenium的web自动化】- Selenium WebDriver原理及安装

    简单介绍 selenium ​ selenium是一个用于测试web网页的自动化测试工具,它直接运行在浏览器中,模拟用户的操作.

  4. Node.js 模块化你所需要知道的事

    一.前言 我们知道,Node.js是基于CommonJS规范进行模块化管理的,模块化是面对复杂的业务场景不可或缺的工具,或许你经常使用它,但却从没有系统的了解过,所以今天我们来聊一聊Node.js模块 ...

  5. go中sync.Mutex源码解读

    互斥锁 前言 什么是sync.Mutex 分析下源码 Lock 位运算 Unlock 总结 参考 互斥锁 前言 本次的代码是基于go version go1.13.15 darwin/amd64 什么 ...

  6. python网络编程-TCP服务端的开发

    #TCP服务端开发 2 #方法说明 3 """ 4 bind(host,port)表示绑定端口号,host是ip地址,ip地址一般不进 行绑定,表示本机的任何一个ip地址 ...

  7. c++ 反汇编 循环结构

    debug do···while 23: int nSum = 0; 00A572AE C7 45 F8 00 00 00 00 mov dword ptr [nSum],0 24: int nInd ...

  8. Git - 使用命令和P4Merge进行diff

    P4Merge P4Merge是Git的一个第三发Diff和Merge工具(可视化冲突解决工具). 下载地址: https://www.perforce.com/downloads/visual-me ...

  9. Redis入门到放弃系列-redis安装

    Redis是什么? Redis is an open source (BSD licensed), in-memory data structure store, used as a database ...

  10. 【Makefile】2-Makefile的介绍及原理

    目录 前言 概念 Chapter 2:介绍 2.1 makefile的规则 2.3 make 是如何工作的 ** 2.5 让 make 自动推导 2.8 Makefile 里面有什么 2.9 Make ...