# 洛谷P4003 [国家集训队2017]无限之环 网络流 最小费用最大流

## 代码实现

#include<bits/stdc++.h>
using namespace std;
#define re register int
#define in inline
#define ak *
#define inf 1e9
#define loc(i,j) ((i-1)*m+j)
int col,cnt=1,n,m,s,t,h[10005],dis[10005],l,r,q[10005],pre[10005],vis[10005],mp[10005],ans,maxflow,preflow;
struct did{
int u,next,to,f,w;
}e[1200005];
char qwq;
{
int yz=0,ioi=1;qwq=getchar();
while(qwq<'0'||qwq>'9') ioi=qwq=='-'?~ioi+1:1,qwq=getchar();
while(qwq>='0'&&qwq<='9') yz=(yz<<3)+(yz<<1)+(qwq^48),qwq=getchar();
return yz ak ioi;
}
in void add(re a,re b,re c,re d)
{
if(!col) swap(a,b);
e[++cnt]=(did){a,h[a],b,c,d},h[a]=cnt;
e[++cnt]=(did){b,h[b],a,0,-d},h[b]=cnt;
}
int spfa()
{
memset(pre,0,sizeof(pre));memset(vis,0,sizeof(vis));
for(re i=s;i<=t;i++) dis[i]=i==s?0:inf;
queue<int>q;q.push(s);vis[s]=1;
while(!q.empty())
{
re i=q.front();vis[i]=0;q.pop();
for(re j=h[i],k;k=e[j].to,j;j=e[j].next)
if(e[j].f&&dis[k]>dis[i]+e[j].w)
{
dis[k]=dis[i]+e[j].w;pre[k]=j;
if(!vis[k]) q.push(k),vis[k]=1;
}
}
return dis[t]<inf;
}
in void solve()
{
re delta=inf;
for(re i=t,p;p=pre[i],i!=s;i=e[p^1].to)
delta=min(delta,e[p].f);
for(re i=t,p;p=pre[i],i!=s;i=e[p^1].to)
e[p].f-=delta,e[p^1].f+=delta;
maxflow+=delta;ans+=delta*dis[t];
}
in int f(re i,re j) {return i*(n*m+1)+j;}
in int g(re i,re j) {return (i-1)*m+j;}
in bool ok(re i,re j) {return (i>=1&&i<=n&&j>=1&&j<=m);}
in void turn(re p,re x,re y,re z) {add(f(x,p),f(y,p),1,z);}
int main()
{
//	freopen("infinityloop.in","r",stdin);
//	freopen("infinityloop.out","w",stdout);
for(re i=1;i<=n;i++) for(re j=1;j<=m;j++)
{
re p=g(i,j);col=(i+j)%2;
if(col)
{
for(re k=0;k<=3;k++)
}
else
{
for(re k=0;k<=3;k++)
}
if(col)
{
}
switch(mp[p])
{
case 0: break; // 0000
case 5: break; // 0101
case 10: break; // 1010
case 15: break; // 1111
case 1: turn(p,0,1,1);turn(p,0,2,2);turn(p,0,3,1);break; // 0001
case 2: turn(p,1,0,1);turn(p,1,3,2);turn(p,1,2,1);break; // 0010
case 4: turn(p,2,1,1);turn(p,2,0,2);turn(p,2,3,1);break; // 0100
case 8: turn(p,3,0,1);turn(p,3,1,2);turn(p,3,2,1);break; // 1000
case 3: turn(p,0,2,1);turn(p,1,3,1);break; // 0011
case 6: turn(p,1,3,1);turn(p,2,0,1);break; // 0110
case 9: turn(p,0,2,1);turn(p,3,1,1);break; // 1001
case 12: turn(p,2,0,1);turn(p,3,1,1);break; // 1100
case 7: turn(p,0,3,1);turn(p,1,3,2);turn(p,2,3,1);break; // 0111
case 11: turn(p,0,2,2);turn(p,1,2,1);turn(p,3,2,1);break; // 1011
case 13: turn(p,0,1,1);turn(p,2,1,1);turn(p,3,1,2);break; // 1101
case 14: turn(p,3,0,1);turn(p,2,0,2);turn(p,1,0,1);break; // 1110
}
}
while(spfa()) solve();
if(maxflow!=preflow) return puts("-1"),0;
cout<<ans<<endl;
}


posted @ 2019-07-10 16:40  disangan233  阅读(185)  评论(0编辑  收藏  举报
Live2D