【CodeForces】【311E】Biologist
网络流/最大权闭合图
题目:http://codeforces.com/problemset/problem/311/E
嗯这是最大权闭合图中很棒的一道题了~
能够1A真是开心~也是我A掉的第一道E题吧……(其实是这题放在E偏水了吧……)
题目大意:有n个0/1变量,给定每个变量的初值,以及每个变量取反的费用$v_i$,有m组需求,对于第 i 组需求,如果集合$S_i$里面的每个变量的值都为给定的v(0 or 1),则获得$\omega_i$的利润,求最大总利润。(其中某些请求若是无法满足还需额外付出g的代价)
这题其实用的思路有点类似最小割……假定跟S相连的变量是选1,跟T相连的是选0,那么对于初值为1的点连i->T,容量为$v_i$,初值为0的连S->i,容量为$v_i$,表示将这些变量取反的代价。
然后考虑需求,如果是要求全为1,则对每个$S_i$中的元素$i$连边 i->n+j ,费用为INF,同时连n+j->T,容量为$\omega_i$;如果是全为0的请求则反过来。
另外这题有个特殊的要求:一些无法满足的请求需要额外付出g的代价。其实这很好解决,我们的答案是$\sum \omega_i-max_flow$,那么我们对于“朋友的请求”,就在tot+了$\omega_i$后,建边时改成容量为$\omega_i+g$即可。
| # | When | Who | Problem | Lang | Verdict | Time | Memory |
|---|---|---|---|---|---|---|---|
| 10516710 | 2015-03-29 17:21:31 | Tunix | E - Biologist | GNU C++ | Accepted | 92 ms | 28188 KB |
//CF 311E
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
using namespace std;
inline int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>''){ if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=1e6+,M=,INF=~0u>>;
typedef long long LL;
/******************tamplate*********************/
int n,m,g,tot,ans;
int a[N],v[N];
struct edge{int to,v;};
struct Net{
edge E[M];
int head[N],next[M],cnt;
void ins(int x,int y,int v){
E[++cnt]=(edge){y,v};
next[cnt]=head[x]; head[x]=cnt;
}
void add(int x,int y,int v){
ins(x,y,v); ins(y,x,);
}
int S,T,cur[N],d[N],Q[N];
bool mklevel(){
memset(d,-,sizeof d);
d[S]=;
int l=,r=-;
Q[++r]=S;
while(l<=r){
int x=Q[l++];
for(int i=head[x];i;i=next[i])
if (d[E[i].to]==- && E[i].v){
d[E[i].to]=d[x]+;
Q[++r]=E[i].to;
}
}
return d[T]!=-;
}
int dfs(int x,int a){
if (x==T) return a;
int flow=;
for(int &i=cur[x];i && flow<a;i=next[i])
if (E[i].v && d[E[i].to]==d[x]+){
int f=dfs(E[i].to,min(a-flow,E[i].v));
E[i].v-=f;
E[i^].v+=f;
flow+=f;
}
if (!flow) d[x]=-;
return flow;
}
void Dinic(){
while(mklevel()){
F(i,S,T) cur[i]=head[i];
ans+=dfs(S,INF);
}
}
void init(){
n=getint(); m=getint(); g=getint();
F(i,,n) a[i]=getint();
F(i,,n) v[i]=getint();
cnt=; S=; T=n+m+; ans=tot=;
F(i,,n) if(a[i]) add(S,i,v[i]);else add(i,T,v[i]);
F(i,,m){
int x=getint(), w=getint(), k=getint(); tot+=w;
F(j,,k){
int y=getint();
if (!x) add(y,n+i,INF);
else add(n+i,y,INF);
}
int y=getint();
if(y) w+=g;//if friend
if (!x) add(n+i,T,w);
else add(S,n+i,w);
}
}
}G1; int main(){
#ifndef ONLINE_JUDGE
freopen("311E.in","r",stdin);
freopen("311E.out","w",stdout);
#endif
G1.init(); G1.Dinic();
printf("%d\n",tot-ans);
return ;
}
【CodeForces】【311E】Biologist的更多相关文章
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 【Codeforces Round #438 C】 Qualification Rounds
[链接]h在这里写链接 [题意] 给你n个问题,每个人都知道一些问题. 然后让你选择一些问题,使得每个人知道的问题的数量,不超过这些问题的数量的一半. [题解] 想法题. 只要有两个问题. 这两个问题 ...
- 【Codeforces Round #438 B】Race Against Time
[链接]h在这里写链接 [题意] 时针.分钟.秒针走不过去. 问你从t1时刻能不能走到t2时刻 [题解] 看看时针.分钟.秒针的影响就好. 看看是不是在整时的位置就好. 然后看看影响到x不能到y; 然 ...
- 【Codeforces Round #438 A】Bark to Unlock
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举它是在连接处,还是就是整个字符串就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc+ ...
- 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry
C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...
- 【Codeforces 321E / BZOJ 5311】【DP凸优化】【单调队列】贞鱼
目录 题意: 输入格式 输出格式 思路: DP凸优化的部分 单调队列转移的部分 坑点 代码 题意: 有n条超级大佬贞鱼站成一行,现在你需要使用恰好k辆车把它们全都运走.要求每辆车上的贞鱼在序列中都是连 ...
- 【codeforces contest 1119 F】Niyaz and Small Degrees
题目 描述 \(n\) 个点的树,每条边有一个边权: 对于一个 \(X\) ,求删去一些边后使得每个点的度数 \(d_i\) 均不超过 \(X\) 的最小代价: 你需要依次输出 \(X=0 \to n ...
- 【codeforces #282(div 1)】AB题解
A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
随机推荐
- 【漫画解读】HDFS存储原理(转载)
以简洁易懂的漫画形式讲解HDFS存储机制与运行原理. 一.角色出演 如上图所示,HDFS存储相关角色与功能如下: Client:客户端,系统使用者,调用HDFS API操作文件;与NN交互获取文件元数 ...
- Yii整合ZF2及soap实例
一)如何整合? // change the following paths if necessary $yii = dirname(__FILE__).'/framework/yii.php'; $c ...
- php中的日期
1.在PHP中获取日期和时间 time()返回当前时间的 Unix 时间戳. getDate()返回日期/时间信息. gettimeofday()返回当前时间信息.date_sunrise()返回给定 ...
- 【转】MessageBox
MessageBox对话框是比较常用的一个信息对话框,其不仅能够定义显示的信息内容.信息提示图标,而且可以定义按钮组合及对话框的标题,是一个功能齐全的信息对话框. 1.函数原型及参数 function ...
- USB总线介绍
•USB 1.0出现在1996年的,速度只有1.5Mb/s1998年升级为USB 1.1,速度也提升到12Mb/s,称之为”full speed” •USB2.0规范是由USB1.1规范演变而来的.它 ...
- EXCLE使用宏生成目录
宏代码: Sub mu() Dim i As Integer Dim ShtCount As Integer Dim SelectionCell As Range ShtCount = Workshe ...
- hdu 5154 Harry and Magical Computer
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5154 Harry and Magical Computer Description In reward ...
- 【javascript】html5中使用canvas编写头像上传截取功能
[javascript]html5中使用canvas编写头像上传截取功能 本人对canvas很是喜欢,于是想仿照新浪微博头像上传功能(前端使用canvas) 本程序目前在谷歌浏览器和火狐浏览器测试可用 ...
- 在线演示平台 | Highcharts中文网 (曲线图、区域图、3D图等等)
http://www.hcharts.cn/ 在线演示平台 | Highcharts中文网
- Winform之ListView
ListView表示 Windows 列表视图控件,该控件显示可用四种不同视图之一显示的项集合.