题目大意: 给你一张网格图, 上面种着一些植物你从网格的最右侧开始进攻每个植物可以对僵尸提供能量或者消耗僵尸的能量每个植物可以保护一个特定网格内的植物, 如果一个植物被保护, 那么如果僵尸想吃掉该植物就必须先吃掉保护它的植物问: 僵尸最多能获得多少能量
注释: 1<=N(网格的宽)<=20,1<=M(网格的长)<=30,-20,000<= 代价和收益<=20,000.
想法: 前置题目 ([NOI2006] 最大获利)这道题和最大获利比较相像, 如果不仔细观察甚至可能认为 tmCCF 出了一道原题? 但是, 我们发现这个算法是完全错误的, 因为如果 A 植物保护 B 植物, B 植物也保护 A 植物, 那么这两个植物是无敌的这个无敌的定义就是从进攻开始到进攻结束这两个植物都不会有任何的问题就是说, 如果存在环的话, 整个环之内的所有植物都是绝对无敌的之后.... .... tm 咋 jb 整啊? 咳咳, 这些无敌的小东西待着也没啥用, 删了就得了怎么删呢? 拓扑排序啊! 用 toposort 删掉不可能提供收益或代价的植物, 之后裸最大权闭合子图即可
最后, 附上丑陋的代码... ...
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <queue>
- #define inf 1000000000
- #define ll long long
- #define p(x,y) (x-1)*m+y
- using namespace std;
- int n,m,T;
- int tot,top,cnt,ans;
- int v[605],cur[605],h[605],d[605],q[605];
- bool del[605];
- struct edge{
- int to,next,v;
- }e[400005],ed[800005];int last[605],last2[605];
- void insert(int u,int v)
- {
- d[v]++;
- e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
- }
- void insert2(int u,int v,int w)
- {
- ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;ed[cnt].v=w;
- ed[++cnt].to=u;ed[cnt].next=last2[v];last2[v]=cnt;ed[cnt].v=0;
- }
- void dfs(int x)
- {
- del[x]=1;
- for(int i=last[x];i;i=e[i].next)
- if(!del[e[i].to])dfs(e[i].to);
- }
- void topsort()
- {
- for(int i=1;i<=n*m;i++)
- if(!d[i])q[++top]=i;
- else del[i]=1;
- while(top)
- {
- int now=q[top];del[now]=0;
- top--;
- for(int i=last[now];i;i=e[i].next)
- {
- d[e[i].to]--;
- if(!d[e[i].to])q[++top]=e[i].to;
- }
- }
- for(int i=1;i<=n*m;i++)
- if(del[i])dfs(i);
- }
- void rebuild()
- {
- cnt=1;T=n*m+1;
- for(int x=1;x<=n*m;x++)
- if(!del[x])
- {
- if(v[x]>0)tot+=v[x],insert2(x,T,v[x]);
- else insert2(0,x,-v[x]);
- for(int i=last[x];i;i=e[i].next)
- if(!del[e[i].to])
- insert2(x,e[i].to,inf);
- }
- }
- bool bfs()
- {
- int head=0,tail=1;
- for(int i=0;i<=T;i++)h[i]=-1;
- h[0]=0;q[0]=0;
- while(head!=tail)
- {
- int now=q[head];head++;
- for(int i=last2[now];i;i=ed[i].next)
- if(ed[i].v&&h[ed[i].to]==-1)
- {
- h[ed[i].to]=h[now]+1;
- q[tail++]=ed[i].to;
- }
- }
- return h[T]!=-1;
- }
- int dfs(int x,int f)
- {
- if(x==T)return f;
- int w,used=0;
- for(int i=cur[x];i;i=ed[i].next)
- if(h[ed[i].to]==h[x]+1)
- {
- w=dfs(ed[i].to,min(ed[i].v,f-used));
- ed[i].v-=w;ed[i^1].v+=w;
- if(ed[i].v)cur[x]=i;
- used+=w;if(used==f)return f;
- }
- if(!used)h[x]=-1;
- return used;
- }
- void dinic()
- {
- while(bfs())
- {
- for(int i=0;i<=T;i++)cur[i]=last2[i];
- ans+=dfs(0,inf);
- }
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=1;i<=n;i++)
- for(int j=1;j<=m;j++)
- {
- scanf("%d",&v[p(i,j)]);
- int x;
- scanf("%d",&x);
- while(x--)
- {
- int a,b;
- scanf("%d%d",&a,&b);
- a++;b++;
- insert(p(i,j),p(a,b));
- }
- }
- for(int i=1;i<=n;i++)
- for(int j=m;j>1;j--)
- insert(p(i,j),p(i,j-1));
- topsort();
- rebuild();
- dinic();
- printf("%d",tot-ans);
- return 0;
- }
小结: 网络流真 tm 有意思
来源: http://www.bubuko.com/infodetail-2529691.html