POJ1149 PIGS
想了好久啊。。。(#-.-)
开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数
我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人。(不懂可以开代码 (・(ェ)・) )
/**
* Problem:POJ1149
* Author:Shun Yao
* Time:2013.9.13
* Result:
* Memo:
*/ #include <cstring>
#include <cstdio> #define INF 0x7fffffff
#define MAXT 1111 long m, n, s, t, f[MAXT], p2[MAXT], h[MAXT], sum[MAXT]; class Edge {
public:
long v, f;
Edge *op, *next;
Edge() {}
~Edge() {}
Edge(long V, long F, Edge *o, Edge *ne) : v(V), f(F), op(o), next(ne) {}
} *g[MAXT], *now[MAXT], *p1[MAXT]; void add(long x, long y, long c) {
g[x] = new Edge(y, c, 0, g[x]);
g[y] = new Edge(x, 0, g[x], g[y]);
g[x]->op = g[y];
} long *l, *r, q[MAXT];
void SAP() {
static char f;
static Edge *e;
static long i, ans, ww, w;
memset(h, -1, sizeof h);
memset(sum, 0, sizeof sum);
l = r = q;
*r++ = t;
h[t] = 0;
sum[0] = 1;
while (l < r) {
for (e = g[*l]; e; e = e->next)
if (h[e->v] == -1) {
++sum[h[e->v] = h[*l] + 1];
*r++ = e->v;
}
++l;
}
for (i = 0; i <= t; ++i) {
now[i] = g[i];
p1[i] = 0;
p2[i] = -1;
}
i = s;
ans = 0;
while (h[s] <= t) {
f = 0;
for (e = now[i]; e; e = e->next)
if (e->f > 0 && h[i] == h[e->v] + 1) {
now[i] = e;
p1[e->v] = e;
p2[e->v] = i;
i = e->v;
if (i == t) {
ww = INF;
for (w = t; w != s; w = p2[w])
if (ww > p1[w]->f)
ww = p1[w]->f;
for (w = t; w != s; w = p2[w]) {
p1[w]->f -= ww;
p1[w]->op->f += ww;
}
ans += ww;
i = s;
}
f = 1;
break;
}
if (!f) {
ww = t + 1;
for (e = g[i]; e; e = e->next)
if (e->f > 0 && ww > h[e->v]) {
ww = h[e->v];
now[i] = e;
}
++sum[ww > t ? ww : ++ww];
if (!--sum[h[i]])
break;
h[i] = ww;
if (i != s)
i = p2[i];
}
}
printf("%ld", ans);
} int main() {
static long i, j, k, A, B; #ifndef ONLINE_JUDGE
freopen("poj1149.in", "r", stdin);
freopen("poj1149.out", "w", stdout);
#endif scanf("%ld%ld", &m, &n);
s = 0;
t = n + m + 1;
for (i = 1; i <= m; ++i) {
scanf("%ld", &A);
add(s, i, A);
f[i] = i;
}
for (i = 1; i <= n; ++i) {
scanf("%ld", &A);
for (j = 1; j <= A; ++j) {
scanf("%ld", &k);
add(f[k], i + m, INF);
f[k] = i + m;
}
scanf("%ld", &B);
add(i + m, t, B);
}
SAP(); fclose(stdin);
fclose(stdout);
return 0;
}
POJ1149 PIGS的更多相关文章
- POJ1149 PIGS 【最大流 + 构图】
题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ1149 PIGS [最大流 建图]
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20662 Accepted: 9435 Description ...
- POJ1149 PIGS (网络流)
PIGS Time Limit: 1000MS M ...
- POJ1149 PIGS 【最大流量】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16555 Accepted: 7416 Description ...
- poj1149 PIGS 最大流(神奇的建图)
一开始不看题解,建图出错了.后来发现是题目理解错了. if Mirko wants, he can redistribute the remaining pigs across the unlock ...
- 题解 POJ1149 Pigs
先翻译一下吧(题面可以在原OJ上找) Mirko在一个由M个锁着的猪舍组成的养猪场工作,Mirko无法解锁任何猪舍,因为他没有钥匙.客户纷纷来到农场.他们每个人都有一些猪舍的钥匙,并想购买一定数量的猪 ...
- POJ1149 PIGS(最大流)
题意: 有一个人,他有m个猪圈,每个猪圈里面有一定数量的猪,但是每个猪圈的门都是锁着的,他自己没有钥匙,只有顾客有钥匙,一天依次来了n个顾客,(记住是依次来的)他们每个人都有一些钥匙,和他 ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
- [BZOJ1280][POJ1149]Emmy卖猪pigs
[BZOJ1280][POJ1149]Emmy卖猪pigs 试题描述 Emmy在一个养猪场工作.这个养猪场有 \(M\) 个锁着的猪圈,但Emmy并没有钥匙.顾客会到养猪场来买猪,一个接着一个.每一位 ...
随机推荐
- 在ECLIPSE中用MAVEN和TOMCAT来建立WEBAPP
找了很多示例,结合以下两个URL,比较简单的测试了一下. http://blog.csdn.net/clj198606061111/article/details/20221133 http://ww ...
- HDU4627+LCM
思路是想到了一些 不过愣是没敢写........... /* 题意:给定一个整数n(2 <= n <= 109),满足a+b=n并且[a,b]的最小公倍数最大. */ #include&l ...
- HDU1353+贪心
注意精度. /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<al ...
- Oracle 学习笔记(一)
1.连接数据库命令: conn 用户名/密码,当用特权身份连接时,要加上as sysdba 2.修改密码: passw(ord),如果要修改其他人的密码,需要用sys或者system登录 3.显示当前 ...
- Java 向Hbase表插入数据异常org.apache.hadoop.hbase.client.HTablePool$PooledHTable cannot be cast to org.apache.client.HTable
出错代码如下: //1.create HTablePool HTablePool hp=new HTablePool(con, 1000); //2.get HTable from HTablepoo ...
- Git教程之远程仓库(9)
有个叫GitHub的神奇的网站,呵呵,从名字就可以看出,这个网站就是提供Git仓库托管服务的,所以,只要注册一个GitHub账号,就可以免费获得Git远程仓库. 由于本地Git仓库和GitHub仓库之 ...
- Android 通过 Intent 传递类对象
Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...
- “WIZnet杯”以太网技术竞赛即将开始!
- 比nerdtree更好的文件浏览器:vimfiler
通过:VimFilerExplorer来打开一个文件浏览器 h:收起 t:展开 -:close 回车:进入或展开 空格:收起
- Playing with cubes II
Description: Hey Codewarrior! You already implemented a Cube class, but now we need your help again! ...