洛谷2805 [NOI2009]植物大战僵尸 （拓扑排序+最小割）

（这里之所以$$S和T$$不能反过来，因为我们跑最小割的时候，是要保证S到T不连通，所以你要让负的权值与S相连，才会让依赖关系有意义）

$$A保护B，B保护A$$

qwq

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define mk make_pair
#define ll long long
#define pb push_back
using namespace std;
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 3010;
const int maxm = 2e6+1e2;
const int inf = 1e9;
int n,m,cnt=1;
int point[maxn],nxt[maxm],to[maxm],val[maxm];
int h[maxn],in[maxn];
int s,t;
int a[maxn][maxn];
int sum;
int num;
int dfn[maxn];
int tag[maxn];
vector<int> v[maxn];
void init()
{
cnt=1;
memset(point,0,sizeof(point));
memset(in,0,sizeof(in));
}
{
nxt[++cnt]=point[x];
to[cnt]=y;
point[x]=cnt;
in[y]++;
}
{
nxt[++cnt]=point[x];
to[cnt]=y;
val[cnt]=w;
point[x]=cnt;
}
void insert(int x,int y,int w)
{
}
queue<int> q;
bool bfs(int s)
{
memset(h,-1,sizeof(h));
h[s]=0;
q.push(s);
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i=point[x];i;i=nxt[i])
{
int p=to[i];
if (h[p]==-1 && val[i]>0)
{
h[p]=h[x]+1;
q.push(p);
}
}
}
if (h[t]==-1) return false;
return true;
}
int dfs(int x,int low)
{
if (x==t || low==0) return low;
int totflow=0;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (h[p]==h[x]+1 && val[i]>0)
{
int tmp = dfs(p,min(low,val[i]));
val[i]-=tmp;
val[i^1]+=tmp;
low-=tmp;
totflow+=tmp;
}
}
if (low>0) h[x]=-1;
}
int dinic()
{
int ans=0;
while (bfs(s))
{
ans=ans+dfs(s,inf);
}
return ans;
}
int getnum(int x,int y)
{
return (x-1)*m+y;
}
void tpsort()
{
while (!q.empty()) q.pop();
for (int i=1;i<=num;i++)
{
if (!in[i]) q.push(i),tag[i]=1,dfn[i]=1;
}
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i=point[x];i;i=nxt[i])
{
int p=to[i];
in[p]--;
if (!in[p])
{
dfn[p]=dfn[x]+1;
q.push(p);
tag[p]=1;
}
}
}
}
int main()
{
num=n*m;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
for (int k=1;k<=num;k++)
{
x++,y++;
v[getnum(i,j)].pb(getnum(x,y));
}
}
tpsort();
init();
s=maxn-10;
t=s+1;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
int now=getnum(i,j);
if(!tag[now]) continue;
if(a[i][j]>0) sum+=a[i][j];
for (int k=0;k<v[now].size();k++)
if (tag[v[now][k]]) insert(now,v[now][k],inf);
if (a[i][j]>0) insert(now,t,a[i][j]);
else insert(s,now,-a[i][j]);
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
int now = getnum(i,j);
int ri  = getnum(i,j+1);
if (j==m) continue;
if (!tag[now] || !tag[ri]) continue;
if (dfn[ri]>dfn[now]) swap(now,ri);
insert(ri,now,inf);
}
}
cout<<sum-dinic();
return 0;
}


posted @ 2018-12-28 10:48  y_immortal  阅读(263)  评论(0编辑  收藏  举报