HDU3879 Base Station 最大权闭合图/最大密度子图 2011 Multi-University Training Contest 5 - Host by BNU
2011-07-28 17:00
691 查看
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 Pi (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 Ai, Bi and Ci, which means if place Ai
and Bi can communicate with each other, the company will get Ci 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, Ai, Bi and Ci, 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
2011 Multi-University Training Contest 5 - Host by BNU
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 ,dis ,q ; 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 ,dis ,q ,d ; 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 ,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); } }
相关文章推荐
- HDU 3879 Base Station 最大权闭包 2011 Multi-University Training Contest 5 - Host by BNU
- HDU3996 Gold Mine最大权闭合图 2011 Multi-University Training Contest 16 - Host by TJU
- HDU3917 Road constructions 最大权闭合图 2011 Multi-University Training Contest 8 - Host by HUST
- HDU 3917 Road constructions 2011 Multi-University Training Contest 8 - Host by HUST 最大权闭包
- HDU3998 Sequence 动态规划+最大流2011 Multi-University Training Contest 16 - Host by TJU
- HDU3832 Earth Hour 2011 Multi-University Training Contest 1 - Host by HNU
- HDU 3902 Swordsman 2011 Multi-University Training Contest 7 - Host by ECNU 计算几何
- HDU 3829 Cat VS Dog 2011 Multi-University Training Contest 1 - Host by HNU
- 【解题报告】2011 Multi-University Training Contest 1-Host by HNU
- HDU 3870 Catch the Theves 2011 Multi-University Training Contest 4 - Host by SDU
- 【转】2011 Multi-University Training Contest 3 - Host by BIT
- HDU 3911 Black And White 2011 Multi-University Training Contest 8 - Host by HUST 线段树应用
- 2011 Multi-University Training Contest 1 - Host by HNU&&R(N)
- HDU3836 Equivalent Sets 2011 Multi-University Training Contest 1 - Host by HNU
- HDU HDU 3861 The King’s Problem 2011 Multi-University Training Contest 3 - Host by BIT
- HDU 3873 Invade the Mars 2011 Multi-University Training Contest 4 - Host by SDU
- Moonfang's Birthday&&2011 Multi-University Training Contest 3 - Host by BIT
- HDU3912 Turn Right 模拟 2011 Multi-University Training Contest 8 - Host by HUST
- HDU 3974 Assign the task 2011 Multi-University Training Contest 14 - Host by FZU 线段树
- HDU 3830 Checkers 2011 Multi-University Training Contest 1 - Host by HNU