2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 F题 Clever King(最小割)

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227

题目链接:https://nanti.jisuanke.com/t/26172

Clever King

Description:

In order to increase the happiness index of people's lives, King Y has decided to develop the manufacturing industry vigorously. There are total n kinds of products that King can choose to produce, different products can improve the happiness index of poeple's lives in different degrees, of course, the production of goods needs raw materials, different products need different ore or other products as raw materials. There are total m mines, and each mine can exploit different ore, Therefore, there are m types of ores, the cost of each mining for each mine is different, king Y want to maximize the income, the calculation method of income is:∑increased happiness index - ∑mining costs.

If you choose to exploit a mine, there will be an unlimited number of this kind of ore. What's more, if you produce one product, the happiness index  will definitely increase, no matter how many you produce.

Input:

The first line of the input has an integer T(1<=T<=50), which represents the number of test cases.

In each test case, the first line of the input contains two integers n(1<=n<=200)--the number of the products and m(1<=m<=200)--the number of mines. The second line contains n integers, val[i] indicates the happiness index that number i product can increase. The third line contains m integers, cost[i] indicates the mining cost of number i mine. The next n lines, each line describes the type of raw material needed for the number i product, in each line, the first two integers n1(1<=n1<=m)--the number of ores that this product needs, n2(1<=n2<=n)--the number of products that this product needs, the next n1 + n2 integers indicate the id of ore and product that this product needs. it guarantees that ∑n1+∑n2<=2000.

Output:

Each test case output an integer that indicates the maximum value ∑val[i]-∑cost[i].

忽略每行输出的末尾多余空格

样例输入

2
3 3
600 200 400
100 200 300
1 2 1 2 3
1 0 2
1 0 3
3 4
600 400 200
100 200 300 1000
2 1 1 2 3
1 0 1
1 0 1

样例输出

600
900

ACM-ICPC Asia Training League   宁夏理工学院

题解:

  最大权闭合子图,跑最小割裸题。

  源点向产品连边,权值为产品的幸福值;矿石向汇点连边,权值为矿石需要的花费;

  然后产品向需要的矿石连边,权值为inf,产品向需要的子产品连边,权值同理也为inf,然后跑最小割。

  最后答案为  所有产品幸福值的和  减去  最小割。(割掉源点向产品的边表示不生产此产品,割掉矿石向汇点的边表示使用此矿石)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = ;
int n, m, S, T;
int dep[N], cur[N];
int head[N];
struct Edge{
int v, c, nex;
Edge(int _v=,int _c=,int _nex=):v(_v),c(_c),nex(_nex){}
};
vector<Edge>E;
void add(int u,int v,int c){E.push_back(Edge(v,c,head[u]));head[u]=E.size()-;}
bool bfs() {
queue<int> q;
memset(dep, -, sizeof(dep));
q.push(S); dep[S] = ;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = E[i].nex) {
int v = E[i].v;
if(E[i].c && dep[v] == -) {
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return dep[T] != -;
}
int dfs(int u, int flow) {
if(u == T) return flow;
int w, used=;
for(int i = head[u]; ~i; i = E[i].nex) {
int v = E[i].v;
if(dep[v] == dep[u] + ) {
w = flow - used;
w = dfs(v, min(w, E[i].c));
E[i].c -= w; E[i^].c += w;
if(v) cur[u] = i;
used += w;
if(used == flow) return flow;
}
}
if(!used) dep[u] = -;
return used;
}
ll dinic() {
ll ans = ;
while(bfs()) {
for(int i = ; i <= T;i++)
cur[i] = head[i];
ans += dfs(S, inf);
}
return ans;
}
int main() {
int t, i, j, k, x, n1, n2;
ll s = ;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
E.clear();
s = ;
S = n+m+; T = n+m+;
for(i = ; i <= n; ++i) {//产品
scanf("%d", &x);
add(S, i, x); add(i, S, );
s += x;
}
for(i = ; i <= m; ++i) {//矿石
scanf("%d", &x);
add(i+n, T, x); add(T, i+n, );
}
for(i = ; i <= n; ++i) {
scanf("%d %d", &n1, &n2);
while(n1--) {//矿石
scanf("%d", &x);
add(i, x+n, inf); add(x+n, i, );
}
while(n2--) {//产品
scanf("%d", &x);
add(i, x, inf); add(x, i, );
}
}
ll ans = dinic();
s = s - ans;
printf("%lld\n", s);
}
return ;
}
上一篇:2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)


下一篇:不逃离WIndows,Asp.Net就只能写写进销存管理系统