标签:
Description
The mobile network market in country XYZ used to be dominated by two large corporations, XYZ Telecom and XYZ Mobile. The central government recently has realized that radio frequency spectrum is a scarce resource and wants to regulate its usage. The spectrum currently in use is divided into 300,000 channels. Any wireless service provider who wishes to use certain spectrum should apply for licenses on these channels. While some services may require use of multiple channels, a single channel can not be shared by different services.
The central government wants to maximize its revenue from the spectrum by putting the channels up to an auction. The only two bidders are XYZ Telecom and XYZ Mobile. They are allowed to place bids on combinations of channels, through which their services can communicate with the customers. Furthermore, the government stipulates that a company can only place at most one bid on a specific channel.
The government can only accept a subset of the bids so none of them would conflict with each other. However, officials soon find out that it is a difficult task to determine the winning bids in order to maximize the revenue, and they are asking for your help.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T(1
Each test case has two bid description sections, which are for XYZ Telecom and XYZ Mobile, respectively. Each section starts with an integer N(1
Output
Results should be directed to standard output. Start each case with “Case # : ” on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
For each test case, print the maximized revenue the government is able to collect by issuing licenses on the channels.
Sample Input
2
3
45 1
51 2
62 3
4
54 1
15 2
33 3
2 4 5
5
20 1
18 2
23 4
54 3 5 6
17 7
4
36 1 2 3
28 5
47 4 7
16 6
Sample Output
Case 1:
169
Case 2:
139
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
const int N = 10005;
const int M = 9000005;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, m, s, t, sum;
struct BID{
int val, chn[35], len;
}B1[3005], B2[3005];
int rec[300005];
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M];
void init() {
sum = 0;
ec = 0;
memset(first, -1, sizeof(first));
memset(rec, 0, sizeof(rec));
}
void addEdge(int a,int b,int c) {
to[ec] = b;
v[ec] = c;
Next[ec] = first[a];
first[a] = ec++;
to[ec] = a;
v[ec] = 0;
Next[ec] = first[b];
first[b] = ec++;
}
int BFS() {
int kid, now, f = 0, r = 1, i;
memset(lev, 0, sizeof(lev));
que[0] = s, lev[s] = 1;
while (f < r) {
now = que[f++];
for (i = first[now]; i != -1; i = Next[i]) {
kid = to[i];
if (!lev[kid] && v[i]) {
lev[kid] = lev[now] + 1;
if (kid == t) return 1;
que[r++] = kid;
}
}
}
return 0;
}
int DFS(int now, int sum) {
int kid, flow, rt = 0;
if (now == t) return sum;
for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
head[now] = i;
kid = to[i];
if (lev[kid] == lev[now] + 1 && v[i]) {
flow = DFS(kid, min(sum - rt, v[i]));
if (flow) {
v[i] -= flow;
v[i^1] += flow;
rt += flow;
} else lev[kid] = -1;
}
}
return rt;
}
int dinic() {
int ans = 0;
while (BFS()) {
for (int i = 0; i <= t; i++) {
head[i] = first[i];
}
ans += DFS(s, INF);
}
return ans;
}
void input() {
scanf("%d\n", &n);
string a;
int val, chn;
int cnt;
for (int i = 1; i <= n; i++) {
cnt = 0;
getline(cin, a);
stringstream stream(a);
stream >> val;
sum += val;
B1[i].val = val;
while (stream >> chn) {
B1[i].chn[cnt++] = chn;
rec[chn] = i;
}
B1[i].len = cnt;
}
scanf("%d\n", &m);
for (int i = 1; i <= m; i++) {
cnt = 0;
getline(cin, a);
stringstream stream(a);
stream >> val;
sum += val;
B2[i].val = val;
while (stream >> chn) {
B2[i].chn[cnt++] = chn;
}
B2[i].len = cnt;
}
s = 0, t = n + m + 2;
}
void build() {
for (int i = 1; i <= n; i++) {
addEdge(s, i, B1[i].val);
}
for (int i = 1; i <= m; i++) {
addEdge(i + n, t, B2[i].val);
}
for (int i = 1; i <= m; i++) {
for (int j = 0; j < B2[i].len; j++) {
if (rec[B2[i].chn[j]]) {
addEdge(rec[B2[i].chn[j]], i + n, INF);
}
}
}
}
int main() {
int T, Case = 1;
scanf("%d", &T);
while (T--) {
printf("Case %d:\n", Case++);
init();
input();
build();
printf("%d\n", sum - dinic());
if (T) puts("");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/47376039