# CSU 1948: 超级管理员(普通费用流&&zkw费用流)

## Sample Input

1
2 3 5
1 2 9
6 5 4


## Sample Output

5

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

int mp[100][100];
int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1}};
const int INF = 999999999;
const int N = 3000;
const int M = 6000000;
struct Edge{
int u,v,cap,cost,next;
}edge[M];
int total;
bool vis[N];
void addEdge(int u,int v,int cap,int cost,int &k){
edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
edge[k].u=v,edge[k].v=u,edge[k].cap = 0,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
tot = 0;
}
bool spfa(int s,int t,int n){
memset(vis,false,sizeof(vis));
for(int i=0;i<=n;i++){
low[i] = (i==s)?0:INF;
pre[i] = -1;
}
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
int v = edge[k].v;
if(edge[k].cap>0&&low[v]>low[u]+edge[k].cost){
low[v] = low[u] + edge[k].cost;
pre[v] = k; ///v为终点对应的边
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-1) return false;
return true;
}
int MCMF(int s,int t,int n){
int mincost = 0,minflow,flow=0;
while(spfa(s,t,n))
{
minflow=INF+1;
for(int i=pre[t];i!=-1;i=pre[edge[i].u])
minflow=min(minflow,edge[i].cap);
flow+=minflow;
for(int i=pre[t];i!=-1;i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^1].cap+=minflow;
}
mincost+=low[t]*minflow;
}
total=flow;
return mincost;
}
int main()
{
int n,m,h,tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d%d",&n,&m,&h);
init();
memset(mp,0,sizeof(mp));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&mp[i][j]);
}
}
int s = 0,t = n*m+5;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(int k=0;k<4;k++){
int nexti = i+dir[k][0];
int nextj = j+dir[k][1];
if(nexti>=1&&nexti<=n&&nextj>=1&&nextj<=m){
//printf("%d %d %d\n",nexti,nextj,(nexti-1)*n+nextj);
}
}
}
}
int mincost = MCMF(s,t,n*m+5);
cout<<mincost<<endl;
}
return 0;
}

尝试了一下传说中的zkw费用流(劲啊!)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define inf 0x7fffffff
#define N 2805
#define ll long long
using namespace std;

int n,m,s,t,ans,vis[N],a[100][100],last[N],cnt;
ll dis[N];
struct edge{int from,to,c,w,op,next;}e[4000000];

int dfs(int x,int maxf)
{
if (x==t||maxf==0) return maxf;
int ret=0;
vis[x]=1;
for (int i=last[x];i;i=e[i].next)
if (e[i].c&&dis[e[i].to]+e[i].w==dis[x]&&!vis[e[i].to])
{
int f=dfs(e[i].to,min(e[i].c,maxf-ret));
ans+=f*e[i].w;
e[i].c-=f;
e[e[i].op].c+=f;
ret+=f;
if (ret==maxf) break;
}
return ret;
}

bool change()
{
ll mn=inf;
for (int i=s;i<=t;i++)
if (vis[i])
for (int j=last[i];j;j=e[j].next)
if (!vis[e[j].to]&&e[j].c) mn=min(mn,-dis[i]+e[j].w+dis[e[j].to]);
if (mn==inf) return 0;
for (int i=s;i<=t;i++)
if (vis[i]) dis[i]+=mn;
return 1;
}

void zkw()
{
do{
for (int i=s;i<=t;i++) vis[i]=0;
while (dfs(s,inf)) for (int i=s;i<=t;i++) vis[i]=0;
}while (change());
}

void insert(int u,int v,int c,int w) /// c是 容量 ,w是费用
{
e[++cnt].from=u;e[cnt].to=v;e[cnt].c=c;e[cnt].w=w;e[cnt].op=cnt+1;e[cnt].next=last[u];last[u]=cnt;
e[++cnt].from=v;e[cnt].to=u;e[cnt].c=0;e[cnt].w=-w;e[cnt].op=cnt-1;e[cnt].next=last[v];last[v]=cnt;
}
void init(){
memset(last,0,sizeof(last));
memset(dis,0,sizeof(dis));
cnt = 0;
ans = 0;
}
int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
int n,m,h,tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d%d",&n,&m,&h);
memset(a,0,sizeof(a));
init();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
s = 0,t = n*m+5;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]!=0) insert(s,(i-1)*n+j,a[i][j],0);
insert((i-1)*n+j,t,h,0);
for(int k=0;k<4;k++){
int nexti = i+dir[k][0];
int nextj = j+dir[k][1];
if(nexti>=1&&nexti<=n&&nextj>=1&&nextj<=m){
insert((i-1)*n+j,(nexti-1)*n+nextj,inf,1);
}
}
}
}
zkw();
cout<<ans<<endl;
}
return 0;
}

posted @ 2017-08-26 10:00  樱花庄的龙之介大人  阅读(189)  评论(0编辑  收藏