Base Station
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others)Total Submission(s): 394 Accepted Submission(s): 143
Problem Description
A famous mobile communication company is planning to build a new set of base stations. According to the previous investigation, n places are chosen as the possible new locations to build those new stations. However, the condition of each position varies much, so the costs to built a station at different places are different. The cost to build a new station at the ith place is P
i (1<=i<=n).
When complete building, two places which both have stations can communicate with each other.
Besides, according to the marketing department, the company has received m requirements. The ith requirement is represented by three integers A i, B i and C i, which means if place A i and B i can communicate with each other, the company will get C i profit.
Now, the company wants to maximize the profits, so maybe just part of the possible locations will be chosen to build new stations. The boss wants to know the maximum profits.
When complete building, two places which both have stations can communicate with each other.
Besides, according to the marketing department, the company has received m requirements. The ith requirement is represented by three integers A i, B i and C i, which means if place A i and B i can communicate with each other, the company will get C i profit.
Now, the company wants to maximize the profits, so maybe just part of the possible locations will be chosen to build new stations. The boss wants to know the maximum profits.
Input
Multiple test cases (no more than 20), for each test case:
The first line has two integers n (0<n<=5000) and m (0<m<=50000).
The second line has n integers, P1 through Pn, describes the cost of each location.
Next m line, each line contains three integers, A i, B i and C i, describes the ith requirement.
The first line has two integers n (0<n<=5000) and m (0<m<=50000).
The second line has n integers, P1 through Pn, describes the cost of each location.
Next m line, each line contains three integers, A i, B i and C i, describes the ith requirement.
Output
One integer each case, the maximum profit of the company.
Sample Input
5 5 1 2 3 4 5 1 2 3 2 3 4 1 3 3 1 4 2 4 5 3
Sample Output
4
Author
liulibo
Source
Recommend
lcy
同NOI2006 最大获利一样,题解详见国家集训队2007论文 胡伯涛 《最小割模型在信息学竞赛中的应用》
第一眼看到就觉得是网络流,再看数据范围傻眼了,n=5000,m=50000
这题用网络流N=55000,M=400000
而ISAP和Dinic复杂度是O(N^2M),网络流最快也才O(N^3),我以为特定超时,于是怀疑不是网络流。
最后WSN告诉我就是网络流,我拿Dinic模板提交果然TLE了。。。
WSN给了我一个Dinic模板,竟然AC了,不过没看懂优化部分!
原来O(N^2M)只是Dinic的复杂度上限,一般都达不到这个极限。
看来以后分析复杂度的时候也不能完全依赖理论复杂度啊。
论文说还可以转换为最大密度子图,不过实现麻烦,不知道效率怎么样。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 55005
#define M 320005
#define inf 999999999
using namespace std;
int n,m,s,t,num,adj[N],dis[N],q[N];
struct edge
{
int v,w,pre;
}e[M];
void insert(int u,int v,int w)
{
e[num]=(edge){v,w,adj[u]};
adj[u]=num++;
e[num]=(edge){u,0,adj[v]};
adj[v]=num++;
}
int bfs()
{
int i,x,v,tail=0,head=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head<tail)
{
x=q[head++];
for(i=adj[x];i!=-1;i=e[i].pre)
if(e[i].w&&dis[v=e[i].v]==0)
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
int dfs(int s,int limit)
{
if(s==t)
return limit;
int i,v,tmp,cost=0;
for(i=adj[s];i!=-1;i=e[i].pre)
if(e[i].w&&dis[s]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp>0)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
if(limit==cost)
break;
}
else dis[v]=-1;
}
return cost;
}
int Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(s,INT_MAX);
return ans;
}
int main ()
{
while(~scanf("%d%d",&n,&m))
{
int i,u,v,w,sum=0;
memset(adj,-1,sizeof(adj));
num=0;
s=0;
t=n+m+1;
for(i=1;i<=n;i++)
{
scanf("%d",&w);
insert(i+m,t,w);
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
insert(s,i,w);
insert(i,u+m,inf);
insert(i,v+m,inf);
sum+=w;
}
printf("%d\n",sum-Dinic());
}
}
最大密度子图:
详细分析过程见胡伯涛的论文《最小割模型在信息学竞赛中的应用》
设U=2*sum(Wv)+sum(We)
s到i连边,边权为U
原图u->v,则u到v,v到u连边,边权为We
i到t连边,边权为U+2*Wv-sum(Wvi)
答案是U*n-MaxFlow
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5005
#define M 300005
#define inf 999999999
using namespace std;
int n,m,s,t,num,adj[N],dis[N],q[N],d[N];
struct edge
{
int v,w,pre;
edge(){}
edge(int vv,int ww,int p){v=vv;w=ww;pre=p;}
}e[M];
void insert(int u,int v,int w)
{
e[num]=edge(v,w,adj[u]);
adj[u]=num++;
e[num]=edge(u,0,adj[v]);
adj[v]=num++;
}
int bfs()
{
int i,x,v,tail=0,head=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head<tail)
{
x=q[head++];
for(i=adj[x];i!=-1;i=e[i].pre)
if(e[i].w&&dis[v=e[i].v]==0)
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
int dfs(int s,int limit)
{
if(s==t)
return limit;
int i,v,tmp,cost=0;
for(i=adj[s];i!=-1;i=e[i].pre)
if(e[i].w&&dis[s]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp>0)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
if(limit==cost)
break;
}
else dis[v]=-1;
}
return cost;
}
int Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(s,INT_MAX);
return ans;
}
int main ()
{
while(~scanf("%d%d",&n,&m))
{
int i,u,v,w[N],U=1000000;
memset(d,0,sizeof(d));
memset(adj,-1,sizeof(adj));
num=0;
s=0;
t=n+1;
for(i=1;i<=n;i++)
{
scanf("%d",w+i);
insert(s,i,U);
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,w);
d[u]+=w[0];
d[v]+=w[0];
insert(u,v,w[0]);
insert(v,u,w[0]);
}
for(i=1;i<=n;i++)
insert(i,t,U+2*w[i]-d[i]);
printf("%d\n",(U*n-Dinic())/2);
}
}