[CF311E]Biologist 题解
题目地址:Codeforces:Problem – 311E – …
May all the beauty be blessed.
题目地址:洛谷:【P4174】[NOI2006]最大获利 – 洛谷、BZOJ:Problem 1497. — [NOI2006]最大获利
新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战。THU 集团旗下的 CS&T 通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需要完成前期市场研究、站址勘测、最优化等项目。
在前期市场调查和站址勘测之后,公司得到了一共 N 个可以作为通讯信号中转站的地址,而由于这些地址的地理位置差异,在不同的地方建造通讯中转站需要投入的成本也是不一样的,所幸在前期调查之后这些都是已知数据:建立第 i个通讯中转站需要的成本为 Pi(1≤i≤N)。
另外公司调查得出了所有期望中的用户群,一共 M 个。关于第 i 个用户群的信息概括为 Ai, Bi和 Ci:这些用户会使用中转站 Ai 和中转站 Bi 进行通讯,公司可以获益 Ci。(1≤i≤M, 1≤Ai,Bi≤N)
THU 集团的 CS&T 公司可以有选择的建立一些中转站(投入成本),为一些用户提供服务并获得收益(获益之和)。那么如何选择最终建立的中转站才能让公司的净获利最大呢?(净获利 = 获益之和 – 投入成本之和)
输入格式:
输入文件中第一行有两个正整数 N 和 M 。
第二行中有 N 个整数描述每一个通讯中转站的建立成本,依次为 P1,P2,…,PN。
以下 M 行,第(i + 2)行的三个数 Ai,Bi和 Ci描述第 i 个用户群的信息。
所有变量的含义可以参见题目描述。
输出格式:
你的程序只要向输出文件输出一个整数,表示公司可以得到的最大净获利。
输入样例#1:
5 5 1 2 3 4 5 1 2 3 2 3 4 1 3 3 1 4 2 4 5 3
输出样例#1:
4
样例:选择建立 1、2、3 号中转站,则需要投入成本 6,获利为 10,因此得到最大收益 4。
100%的数据中:N≤5 000,M≤50 000,0≤ Ci ≤100,0≤ Pi ≤100。
这类问题有一个统一的模型,叫最大权闭合子图。
我们把中转站向汇连边,容量为建设成本。把客户向源连边,容量为获利。再把客户和用到的中转站之间连边,容量无限。求一次最小割,然后用所有获利减去这个最小割的权值和即为答案。
最小割肯定不会去割中间容量无限的边,因此割的是与源汇相连的边。割掉客户与源相连的边的含义是不要这个客户了,割掉中转站与汇相连的边的含义是不建这个中转站了。因此最小割的含义是发展客户净利润为负的收益加上建设中转站的总成本,也就是损失的收益+建设的开支,直接用总收益减去它就是答案。
// Code by KSkun, 2018/4
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF
: *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
register char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 100005, INF = 1e9;
struct Edge {
int to, cap, nxt;
} gra[MAXN << 3];
int head[MAXN], tot;
inline void addedge(int u, int v, int cap) {
gra[tot] = Edge {v, cap, head[u]}; head[u] = tot++;
gra[tot] = Edge {u, 0, head[v]}; head[v] = tot++;
}
int level[MAXN];
inline bool bfs(int s, int t) {
memset(level, -1, sizeof(level));
std::queue<int> que;
level[s] = 0; que.push(s);
while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = gra[i].nxt) {
int v = gra[i].to;
if(level[v] == -1 && gra[i].cap > 0) {
level[v] = level[u] + 1;
if(v == t) return true;
que.push(v);
}
}
}
return level[t] != -1;
}
int cur[MAXN];
inline int dfs(int u, int t, int left) {
if(u == t || left <= 0) return left;
int flow = 0;
for(int &i = cur[u]; ~i; i = gra[i].nxt) {
int v = gra[i].to;
if(gra[i].cap > 0 && level[v] == level[u] + 1) {
int d = dfs(v, t, std::min(left, gra[i].cap));
if(d > 0) {
left -= d; flow += d;
gra[i].cap -= d; gra[i ^ 1].cap += d;
if(left <= 0) {
//level[u] = -1; 不注释掉会TLE
return flow;
}
}
}
}
return flow;
}
inline int dinic(int s, int t) {
int flow = 0;
while(bfs(s, t)) {
memcpy(cur, head, sizeof(head));
int f;
while(f = dfs(s, t, INF)) {
flow += f;
}
}
return flow;
}
int n, m, pi, ai, bi, ci, S, T, ans;
// 1 ~ n device
// n+1 ~ n+m custom
int main() {
memset(head, -1, sizeof(head));
n = readint(); m = readint();
S = n + m + 1; T = S + 1;
for(int i = 1; i <= n; i++) {
pi = readint();
addedge(i, T, pi);
}
for(int i = 1; i <= m; i++) {
ai = readint(); bi = readint(); ci = readint();
ans += ci;
addedge(S, n + i, ci);
addedge(n + i, ai, INF);
addedge(n + i, bi, INF);
}
ans -= dinic(S, T);
printf("%d", ans);
return 0;
}
题目地址:洛谷:【P2891】[USACO07OPEN]吃饭Dining – 洛谷、BZOJ:Problem 1711. — [Usaco2007 Open]Dining吃饭、POJ:3281 — Dining、OpenJudge百练:OpenJudge – 3479:Dining
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)
输入格式:
Line 1: Three space-separated integers: N, F, and D
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
输出格式:
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
输入样例#1:
4 3 3 2 2 1 2 3 1 2 2 2 3 1 2 2 2 1 3 1 2 2 1 1 3 3
输出样例#1:
3
One way to satisfy three cows is:
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
“三分图匹配”?!
考虑把饮料的点放左边,食物的点放右边,建图为源→喜欢的饮料→牛→喜欢的食物→汇这样跑最大流。
然后你WA了。
这是因为你没有限制牛这个点内流过的流量,有可能给一只牛配了超过一套食物+饮料。因此我们要对牛拆点限流。
// Code by KSkun, 2018/4
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF
: *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
register char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 405, MAXM = 100005, INF = 1e9;
struct Edge {
int to, cap, nxt;
} gra[MAXM << 1];
int head[MAXN], tot;
inline void addedge(int u, int v, int cap) {
gra[tot] = Edge {v, cap, head[u]}; head[u] = tot++;
gra[tot] = Edge {u, 0, head[v]}; head[v] = tot++;
}
int level[MAXN];
std::queue<int> que;
inline bool bfs(int s, int t) {
memset(level, -1, sizeof(level));
level[s] = 0; que.push(s);
while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = gra[i].nxt) {
int v = gra[i].to;
if(gra[i].cap > 0 && level[v] == -1) {
level[v] = level[u] + 1;
que.push(v);
}
}
}
return level[t] != -1;
}
int cur[MAXN];
bool vis[MAXN];
inline int dfs(int u, int t, int left) {
if(u == t || !left)
return left;
int flow = 0; vis[u] = true;
for(int &i = cur[u]; ~i; i = gra[i].nxt) {
int v = gra[i].to;
if(gra[i].cap > 0 && !vis[v] && level[v] == level[u] + 1) {
int d = dfs(v, t, std::min(left, gra[i].cap));
if(d > 0) {
left -= d; flow += d;
gra[i].cap -= d; gra[i ^ 1].cap += d;
if(!left) {
level[u] = -1;
return flow;
}
}
}
}
return flow;
}
inline int dinic(int s, int t) {
int flow = 0;
while(bfs(s, t)) {
memset(vis, 0, sizeof(vis));
memcpy(cur, head, sizeof(head));
int f;
while(f = dfs(s, t, INF)) {
flow += f;
}
}
return flow;
}
int n, f, d, fi, di, t, S, T;
// 1~n n+1~2n cow
// 2n+1~2n+f food
// 2n+f+1~2n+f+d drink
int main() {
memset(head, -1, sizeof(head));
n = readint(); f = readint(); d = readint();
S = 2 * n + f + d + 1; T = S + 1;
for(int i = 1; i <= n; i++) {
addedge(i, i + n, 1);
}
for(int i = 1; i <= f; i++) {
addedge(S, 2 * n + i, 1);
}
for(int i = 1; i <= d; i++) {
addedge(2 * n + f + i, T, 1);
}
for(int i = 1; i <= n; i++) {
fi = readint(); di = readint();
while(fi--) {
t = readint();
addedge(2 * n + t, i, 1);
}
while(di--) {
t = readint();
addedge(i + n, 2 * n + f + t, 1);
}
}
printf("%d", dinic(S, T));
return 0;
}
题目地址:POJ:2391 — Ombrophobic Bovines、OpenJudge百练:OpenJudge – 2391:Ombrophobic Bovines
FJ’s cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter.
The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction.
Some of the farm’s fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse.
Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.
给定一张的无向图,每条边有长度。点i处有ai头牛,以及一个能容纳bi头牛的牛棚。牛可以沿边移动,每条边上可以同时有任意头牛经过。一头牛经过一条边所需时间即为道路长度。
给出一个将牛分配到牛棚的方案,并最小化所需移动时间T。
输入格式:
* Line 1: Two space-separated integers: F and P
* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i.
* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.
输出格式:
* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output “-1”.
输入样例#1:
3 4 7 2 0 4 2 6 1 2 40 3 2 70 2 3 90 1 3 120
输出样例#1:
110
OUTPUT DETAILS:
In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.
我们不好直接求这个移动时间,但是如果固定了一个时间,我们可以通过构图最大流来判断是否可行。
如果说已知了时间,我们可以将每个位置拆点,分别代表牛和牛棚,分别向源汇连边,容量控制牛的数量或牛棚的容量。牛与牛棚之间的边由最长时间来决定。我们可以预先跑一遍Floyd,处理出最短路。如果最短路并未超过二分的时间,那么就可以加边。如果代表牛的点满流,则所有牛都可以分配进牛棚里,即满足条件。
注:下面的代码在POJ上TLE了,但是可以通过OpenJudge百练的评测以及官方数据的测试。官方数据下载
// Code by KSkun, 2018/4
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF
: *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
register char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXE = 400005, MAXV = 405;
const int INF = 1e9;
struct Edge {
int to, cap, nxt;
} gra[MAXE << 1];
int head[MAXV], tot;
inline void reset() {
memset(head, -1, sizeof(head)); tot = 0;
}
inline void addedge(int u, int v, int cap) {
gra[tot] = Edge {v, cap, head[u]}; head[u] = tot++;
gra[tot] = Edge {u, 0, head[v]}; head[v] = tot++;
}
int n, m;
LL dis[MAXV][MAXV];
inline void floyd() {
for(register int k = 1; k <= n; k++) {
for(register int i = 1; i <= n; i++) {
for(register int j = 1; j <= n; j++) {
dis[i][j] = std::min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
}
int level[MAXV], cur[MAXV];
std::queue<int> que;
inline bool bfs(int s, int t) {
memset(level, -1, sizeof(level));
level[s] = 0;
que.push(s);
while(!que.empty()) {
register int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = gra[i].nxt) {
int v = gra[i].to;
if(level[v] == -1 && gra[i].cap > 0) {
level[v] = level[u] + 1;
que.push(v);
}
}
}
return level[t] != -1;
}
bool vis[MAXV];
inline int dfs(int u, int t, int left) {
if(u == t || left == 0) return left;
register int flow = 0; vis[u] = true;
for(int &i = cur[u]; ~i; i = gra[i].nxt) {
register int v = gra[i].to;
if(gra[i].cap > 0 && !vis[v] && level[v] == level[u] + 1) {
int d = dfs(v, t, std::min(left, gra[i].cap));
if(d > 0) {
flow += d; left -= d;
gra[i].cap -= d; gra[i ^ 1].cap += d;
if(left == 0) {
level[u] = -1;
return flow;
}
}
}
}
return flow;
}
inline int dinic(int s, int t) {
register int flow = 0;
while(bfs(s, t)) {
memcpy(cur, head, sizeof(head));
memset(vis, 0, sizeof(vis));
register int f;
while(f = dfs(s, t, INF)) {
flow += f;
}
}
return flow;
}
int cnum[MAXV], snum[MAXV], csum;
int S, T;
// 1~n cow
// n+1~2n shelter
inline bool check(LL mid) {
reset();
for(register int i = 1; i <= n; i++) {
addedge(S, i, cnum[i]);
addedge(i + n, T, snum[i]);
addedge(i, i + n, INF);
}
for(register int i = 1; i <= n; i++) {
for(register int j = i + 1; j <= n; j++) {
if(dis[i][j] <= mid) {
addedge(i, j + n, INF);
addedge(j, i + n, INF);
}
}
}
return dinic(S, T) >= csum;
}
int ut, vt;
LL wt;
int main() {
n = readint(); m = readint(); S = 2 * n + 1; T = S + 1;
for(register int i = 1; i <= n; i++) {
cnum[i] = readint(); snum[i] = readint();
csum += cnum[i];
}
memset(dis, 0x3f, sizeof(dis));
for(register int i = 1; i <= m; i++) {
ut = readint(); vt = readint(); wt = readint();
dis[ut][vt] = dis[vt][ut] = std::min(dis[ut][vt], wt);
}
for(register int i = 1; i <= n; i++) {
dis[i][i] = 0;
}
floyd();
register LL l = 0, r = 1e16, mid;
while(r - l > 1) {
mid = (l + r) >> 1;
if(check(mid)) r = mid; else l = mid;
}
if(r != 1e16) printf("%lld", r); else puts("-1");
return 0;
}