BZOJ 1497 最大获利
新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战。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≤5 000,M≤50 000,0≤Ci≤100,0≤Pi≤100。
考虑对于一个用户群,获得它的收益当且仅当取了相应的中转站
把每个用户群和中转站分别建点,算最大闭合权子图
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f3f3f3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (1e16)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (60000)
#define MAXM (1000000)
class Max_flow //dinic+当前弧优化
{
public:
int n,s,t;
int q[MAXN],size;
int edge[MAXM],next[MAXM],pre[MAXN];
ll weight[MAXM];
void addedge(int u,int v,ll w)
{
edge[++size]=v;
weight[size]=w;
next[size]=pre[u];
pre[u]=size;
}
void addedge2(int u,int v,ll w){addedge(u,v,w),addedge(v,u,0);}
bool b[MAXN];
ll d[MAXN];
bool SPFA(int s,int t)
{
For(i,n) d[i]=INF;
MEM(b)
d[q[1]=s]=0;b[s]=1;
int head=1,tail=1;
while (head<=tail)
{
int now=q[head++];
Forp(now)
{
int &v=edge[p];
if (weight[p]&&!b[v])
{
d[v]=d[now]+1;
b[v]=1,q[++tail]=v;
}
}
}
return b[t];
}
int iter[MAXN];
int dfs(int x,ll f)
{
if (x==t) return f;
Forpiter(x)
{
int v=edge[p];
if (weight[p]&&d[x]<d[v])
{
int nowflow=dfs(v,min(weight[p],f));
if (nowflow)
{
weight[p]-=nowflow;
weight[p^1]+=nowflow;
return nowflow;
}
}
}
return 0;
}
ll max_flow(int s,int t)
{
ll flow=0;
while(SPFA(s,t))
{
For(i,n) iter[i]=pre[i];
ll f;
while (f=dfs(s,INF))
flow+=f;
}
return flow;
}
void mem(int n,int s,int t)
{
(*this).n=n;
(*this).t=t;
(*this).s=s;
size=1;
MEM(pre)
}
}S;
int n,m;
int u[MAXM],v[MAXM],siz;
void add(int a,int b){u[++siz]=a;v[siz]=b;}
int a[MAXN];
ll max_bihe_graph(int n,int m,int *a) {
int s=n+1,t=s+1;
S.mem(t,s,t);
ll c = 0;
For(i,n)
if (a[i]>=0) c+=a[i],S.addedge2(s,i,a[i]);
else S.addedge2(i,t,-a[i]);
For(i,m) {
S.addedge2(u[i],v[i],INF);
}
return c-S.max_flow(s,t);
}
int main()
{
// freopen("K.in","r",stdin);
// freopen(".out","w",stdout);
n=read(),m=read();
For(i,n) a[i]=-read();
siz=0;
For(i,m) {
int A=read(),B=read(),C=read();
++n;
add(n,A); add(n,B);
a[n]=C;
}
cout<<max_bihe_graph(n,siz,a);
return 0;
}
BZOJ 1565 植物大战僵尸
约20%的数据满足1 ≤ N, M ≤ 5;
约40%的数据满足1 ≤ N, M ≤ 10;
约100%的数据满足1 ≤ N ≤ 20,1 ≤ M ≤ 30,-10000 ≤ Score ≤ 10000 。
显然取一个点当且仅当把保护它的点取了,取左边的点当且仅当把右边的点取完
当时可能存在保护环,保护环内的点肯定去不了,最大权闭合子图要求图中没有环
本题先用拓扑排序找出不在环中的点,然后再求
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f3f3f3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (1e16)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (700)
#define MAXM (1000000)
class Max_flow //dinic+当前弧优化
{
public:
int n,s,t;
int q[MAXN],size;
int edge[MAXM],next[MAXM],pre[MAXN];
ll weight[MAXM];
void addedge(int u,int v,ll w)
{
edge[++size]=v;
weight[size]=w;
next[size]=pre[u];
pre[u]=size;
}
void addedge2(int u,int v,ll w){addedge(u,v,w),addedge(v,u,0);}
bool b[MAXN];
ll d[MAXN];
bool SPFA(int s,int t)
{
For(i,n) d[i]=INF;
MEM(b)
d[q[1]=s]=0;b[s]=1;
int head=1,tail=1;
while (head<=tail)
{
int now=q[head++];
Forp(now)
{
int &v=edge[p];
if (weight[p]&&!b[v])
{
d[v]=d[now]+1;
b[v]=1,q[++tail]=v;
}
}
}
return b[t];
}
int iter[MAXN];
int dfs(int x,ll f)
{
if (x==t) return f;
Forpiter(x)
{
int v=edge[p];
if (weight[p]&&d[x]<d[v])
{
int nowflow=dfs(v,min(weight[p],f));
if (nowflow)
{
weight[p]-=nowflow;
weight[p^1]+=nowflow;
return nowflow;
}
}
}
return 0;
}
ll max_flow(int s,int t)
{
ll flow=0;
while(SPFA(s,t))
{
For(i,n) iter[i]=pre[i];
ll f;
while (f=dfs(s,INF))
flow+=f;
}
return flow;
}
void mem(int n,int s,int t)
{
(*this).n=n;
(*this).t=t;
(*this).s=s;
size=1;
MEM(pre)
}
}S;
vi edges[MAXN];
int indegree[MAXN];
bool b[MAXN];
int q[MAXN*4];
void topsort(int n)
{
MEM(q) MEM(b)
int head_=1,tail=0;
Fork(i,1,n)
if (indegree[i]==0)
{
q[++tail]=i;b[i]=1;
}
while (head_<=tail)
{
int now=q[head_];
Rep(j,SI(edges[now]))
{
int v=edges[now][j];
indegree[v]--;
if (indegree[v]==0)
{
q[++tail]=v;b[v]=1;
}
}
head_++;
}
}
int n,m;
int u[MAXM],v[MAXM],siz;
void add(int a,int b){u[++siz]=a;v[siz]=b;}
int a[MAXN];
ll max_bihe_graph(int n,int m,int *a) {
int s=n+1,t=s+1;
S.mem(t,s,t);
ll c = 0;
For(i,n) if (b[i])
if (a[i]>=0) c+=a[i],S.addedge2(s,i,a[i]);
else S.addedge2(i,t,-a[i]);
For(i,m) {
S.addedge2(u[i],v[i],INF);
}
return c-S.max_flow(s,t);
}
int idx(int i,int j) {return i*m+j+1;}
int cost[MAXN];
int main()
{
// freopen("L.in","r",stdin);
// freopen(".out","w",stdout);
n=read(),m=read();
Rep(i,n) Rep(j,m) {
int id=idx(i,j);
cost[id]=read();
int k=read();
while(k--) {
int a=read(),b=read();
int id2=idx(a,b);
edges[id].pb(id2); indegree[id2]++;
}
if(j<m-1) edges[id+1].pb(id),indegree[id]++;;
}
topsort(n*m);
siz=0;
For(i,n*m) if (b[i]){
int sz=SI(edges[i]);
Rep(j,sz) {
int v=edges[i][j];
if (b[v]) add(v,i);
}
}
cout<<max_bihe_graph(n*m,siz,cost)<<endl;
return 0;
}
HDU 5457 Hold Your Hand
给n个8位二进制数(含前导0),现在有m个操作,
P操作把所有前缀是某个二进制串的数删除,S操作把所有后缀是某个二进制串的数删除,每个操作有一个代价wi,求把n个数删完的最小代价。
考虑:
1.如果只有前缀
建立Trie树,删除某个前缀等于把这个节点到父亲的边割掉。把n个数到T连边容量为1,求最小割
2.考虑后缀
建立颠倒的Trie树,后缀同理,不同的是,把n个数从上面那棵到下面那棵的对应节点连边容量inf,求2个树树根间的最小割
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f3f3f3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (1e16)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (5100)
#define MAXM (2*70000)
class Max_flow //dinic+当前弧优化
{
public:
int n,s,t;
int q[MAXN],size;
int edge[MAXM],next[MAXM],pre[MAXN];
ll weight[MAXM];
void addedge(int u,int v,ll w)
{
edge[++size]=v;
weight[size]=w;
next[size]=pre[u];
pre[u]=size;
}
void addedge2(int u,int v,ll w){addedge(u,v,w),addedge(v,u,0);}
bool b[MAXN];
ll d[MAXN];
bool SPFA(int s,int t)
{
For(i,n) d[i]=INF;
MEM(b)
d[q[1]=s]=0;b[s]=1;
int head=1,tail=1;
while (head<=tail)
{
int now=q[head++];
Forp(now)
{
int &v=edge[p];
if (weight[p]&&!b[v])
{
d[v]=d[now]+1;
b[v]=1,q[++tail]=v;
}
}
}
return b[t];
}
int iter[MAXN];
int dfs(int x,ll f)
{
if (x==t) return f;
Forpiter(x)
{
int v=edge[p];
if (weight[p]&&d[x]<d[v])
{
int nowflow=dfs(v,min(weight[p],f));
if (nowflow)
{
weight[p]-=nowflow;
weight[p^1]+=nowflow;
return nowflow;
}
}
}
return 0;
}
ll max_flow(int s,int t)
{
ll flow=0;
while(SPFA(s,t))
{
For(i,n) iter[i]=pre[i];
ll f;
while (f=dfs(s,INF))
flow+=f;
}
return flow;
}
void mem(int n,int s,int t)
{
(*this).n=n;
(*this).t=t;
(*this).s=s;
size=1;
MEM(pre)
}
}S;
int n,m;
int a[MAXN];
int b[MAXN][10];
const int len =8;
int f(int i) { //suffixing id
int x=1;
For(j,len) {
x=x*2+b[i][j];
}
return x;
}
int f2(int i) { //prefixing id
int x=1;
ForD(j,len) {
x=x*2+b[i][j];
}
return x;
}
int f3(char *s) {
int x=1,sz=strlen(s);
RepD(i,sz-1) {
x=x*2+(s[i]=='1');
}
return x;
}
int f4(char *s) {
int x=1,sz=strlen(s);
Rep(i,sz) {
x=x*2+(s[i]=='1');
}
return x;
}
const int M=512;
int w[MAXN],w2[MAXN];
int main()
{
// freopen("M.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
For(kcase,T) {
MEMI(w) MEMI(w2)
n=read(),m=read();
For(i,n) a[i]=read();
For(i,n) {
For(j,len) b[i][j]=(a[i]&(1<<(j-1)))>0;
}
For(i,m) {
char c[2],s[10];int p;
scanf("%s%s%d",c,s,&p);
if(c[0]=='P') {
int t=f4(s);
w2[t]=min(w2[t],p);
} else {
int t=f3(s);
w[t]=min(w[t],p);
}
}
ll ans=0;
S.mem(M*2,1,1+M);
Fork(i,2,M-1) {
S.addedge2(i/2,i,w[i]);
}
Fork(i,2,M-1) {
S.addedge2(M+i,M+i/2,w2[i]);
}
For(i,n) S.addedge2(f(i),M+f2(i),INF);
ans=S.max_flow(1,1+M);
if (ans>500000) ans=-1;
Pr(kcase,ans)
}
return 0;
}