题意:有两个公司A和B在申请一些资源,现在给出两个公司所申请的内容,内容包括价钱和申请的资源 ,现在你做为官方,你只能拒绝一个申请或者接受一个申请,同一个资源不能两个公司都拥有,且申请的资源不能只给部分,问:作为官方,你能得到的最大利益是多少

析:就是一个最小割,因为AB两个公司,资源不能共用,只能给一个,也就是官方要舍弃一些利益让他们不共用资源,要这个舍弃的最小。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-3;
const int maxn = 6000 + 10;
const int maxm = 3e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int from, to, cap, flow;
}; struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int d[maxn];
bool vis[maxn];
int cur[maxn]; void init(int n){
this-> n = n;
FOR(i, 0, n) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int cap){
edges.pb((Edge){from, to, cap, 0});
edges.pb((Edge){to, from, 0, 0});
m = edges.sz;
G[from].pb(m - 2);
G[to].pb(m - 1);
} bool bfs(){
ms(vis, 0); vis[s] = 1;
d[s] = 0;
queue<int> q; q.push(s); while(!q.empty()){
int u = q.front(); q.pop();
for(int i = 0; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(!vis[e.to] && e.cap > e.flow){
d[e.to] = d[u] + 1;
vis[e.to] = 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int u, int a){
if(u == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[u]; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){
e.flow += f;
edges[G[u][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
} int maxflow(int s, int t){
this-> s = s;
this-> t = t;
int flow = 0;
while(bfs()){ ms(cur, 0); flow += dfs(s, INF); }
return flow;
}
}; Dinic dinic; bool vis[maxn>>1][maxn>>1];
int a[maxm], b[maxm]; int main(){
ios::sync_with_stdio(false);
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
cin >> n; cin.get();
int s = 0, t = n + 3000 + 1;
int mmax = -INF, ans = 0;
dinic.init(t + 2);
string line;
ms(vis, 0); ms(a, 0); ms(b, 0);
for(int i = 1; i <= n; ++i){
getline(cin, line);
stringstream ss(line);
int x; ss >> x;
dinic.addEdge(s, i, x);
ans += x;
while(ss >> x){
mmax = max(mmax, x);
a[x] = i;
}
}
cin >> m; cin.get();
for(int i = 1; i <= m; ++i){
getline(cin, line);
stringstream ss(line);
int x; ss >> x;
dinic.addEdge(i + n, t, x);
ans += x;
while(ss >> x){
mmax = max(mmax, x);
b[x] = i;
}
}
for(int i = 1; i <= mmax; ++i){
if(!a[i] || !b[i] || vis[a[i]][b[i]]) continue;
vis[a[i]][b[i]] = 1;
dinic.addEdge(a[i], b[i] + n, INF);
}
if(kase > 1) cout << endl;
cout << "Case " << kase << ":\n";
cout << ans - dinic.maxflow(s, t) << endl;
}
return 0;
}

  

最新文章

  1. linux 安装mysql数据库——yum安装法
  2. Redis 5种数据结构使用及注意事项
  3. Oracle的表空间和数据文件
  4. Erlang数据类型的表示和实现(4)——boxed 对象
  5. syntax error: missing &#39;;&#39; before identifier &#39;IWebBrowser&#39;
  6. head,tail,cat,more,less
  7. POJ 1755 Triathlon
  8. win7无声音显示“未插入扬声器或耳机” 怎么解决
  9. Git 的版本库创建和修改
  10. c# winform多线程实时更新控件
  11. 远程过程调用发展历程 WebAPI GRPC Hprose
  12. 省市区联动,非ajax请求。
  13. oracle对sum出来的数字进行非空补0处理
  14. Test Scenarios for Filter Criteria
  15. ModelValidator基于元数据的验证
  16. python将列表元素按指定数目分组
  17. 建立一个更高级别的查询 API:正确使用Django ORM 的方式(转)
  18. iOS-ARC_Xcode检测循环引用
  19. MobaXterm 错行,乱码
  20. 【BZOJ3790】神奇项链 Manacher+贪心

热门文章

  1. jboss 异常处理
  2. 因为链接服务器 &quot;SQLEHR&quot; 的 OLE DB 访问接口 &quot;SQLNCLI10&quot; 无法启动分布式事务
  3. [转]被玩坏的innerHTML、innerText、textContent和value属性
  4. java作用域public ,private ,protected 及不写时的区别
  5. js中常用的事件
  6. struts2 参数注入 方法拦截器
  7. mysql中binlog_format的三种模式
  8. ubuntu下搭建testlink
  9. 微信公众平台开发之基于百度 BAE3.0 的开发环境搭建(采用 Baidu Eclipse)
  10. 转---tcp三次握手四次挥手syn fin......