POJ1149 PIGS(最大流)
2014-08-07 15:08
302 查看
题意:
有一个人,他有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,就是以上那些,要是不明白可以自己按照上面的见图思路画个图,很容易理解。
有一个人,他有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; }
相关文章推荐
- POJ 1149 PIGS ,最大流
- 【最大流(EK)】poj 1149 PIGS
- POJ 1149-PIGS(Ford-Fulkerson 标号法求网络最大流)
- Poj 1149 PIGS【最大流Dinic+建图】
- poj 1149 pigs(最大流)
- POJ-1149-PIGS(最大流 标号法)
- PIGS (poj 1149 最大流)
- poj1149 PIGS(最大流+建模)
- poj1149 PIGS --- 最大流EK
- POJ 1149 PIGS【最大流】
- poj1149 - PIGS(最大流)
- POJ---1149 PIGS[dinic()最大流]
- [POJ 1149]PIGS[最大流][建图]
- POJ1149 PIGS [最大流 建图]
- poj 1149 PIGS (网络流最大流Dinic)
- POJ 1149 PIGS (最大流)
- poj-1149-PIGS(最大流)
- 【最大流】POJ-1149 PIGS
- POJ 1149 PIGS(最大流)
- poj 1149 PIGS 最大流 太神奇的建图方式!!!