题意:NxM的格子有些上面有数字,现在要把奇数跟偶数配对连起来,其他的格子连成一个个回路,

单独的相邻两个格子相连也算是一个回路按两条边算,连线不能相交,

给出相邻两个格子相连的费用,求最小的总费用,无解输出-1

n,m<=50

保证答案在int范围之内

思路:费用流神仙建模

From https://blog.csdn.net/ahi219/article/details/51454133

把每个格子拆成两个点,然后如下连边:

源点向左边的奇数格子和空格子连容量为1,费用为0的边。

右边的偶数格子和空格子向汇点连容量为1,费用为0的边。

左边的格子向右边相邻的格子连容量为1,费用为cost的边。

这样求一遍最小费用最大流即可,如果不是满流,则为无解。

这样建模的原因是把每空格子拆成入点和出点,奇数只有入点,偶数只有出点。

这样保证了每个空格子会有一个入度和一个出度,要么成环,要么参与到奇偶相连中。

奇数只有出度偶数只有出度保证了一定是奇数连向偶数。

这样求一遍费用流最后就是结果,如果不是满流说明有的点没连上所以无解。

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 31000
#define M 51
#define MOD 1000000007
#define eps 1e-8
#define pi acos(-1)
#define oo 1010000000 bool inq[N];
int num[M][M][],a[M][M],
q[N],dis[N],head[N],vet[N],nxt[N],len1[N],len2[N],fan[N],pre[N][],
n,source,src,tot,s,ans,flow; void add(int a,int b,int c,int d)
{
nxt[++tot]=head[a];
vet[tot]=b;
len1[tot]=c;
len2[tot]=d;
head[a]=tot; nxt[++tot]=head[b];
vet[tot]=a;
len1[tot]=;
len2[tot]=-d;
head[b]=tot;
} bool spfa()
{
for(int i=;i<=s;i++)
{
dis[i]=oo;
inq[i]=false;
}
int t=; int w=;
q[]=source; dis[source]=; inq[source]=true;
while(t<w)
{
t++; int u=q[t%(s+)]; inq[u]=false;
int e=head[u];
while(e)
{
int v=vet[e];
if(len1[e]&&dis[u]+len2[e]<dis[v])
{
dis[v]=dis[u]+len2[e];
pre[v][]=u;
pre[v][]=e;
if(!inq[v])
{
w++; q[w%(s+)]=v; inq[v]=true;
}
}
e=nxt[e];
}
}
return (dis[src]!=oo);
} void mcf()
{
int k=src;
int t=oo;
while(k!=source)
{
int e=pre[k][];
t=min(t,len1[e]);
k=pre[k][];
}
k=src;
while(k!=source)
{
int e=pre[k][];
len1[e]-=t;
len1[fan[e]]+=t;
ans+=t*len2[e];
k=pre[k][];
}
flow+=t;
} void init()
{
memset(head,,sizeof(head));
memset(pre,,sizeof(pre));
tot=;
} int main()
{
freopen("hdoj5520.in","r",stdin);
//freopen("hdoj5520.out","w",stdout);
for(int i=;i<N;i++)
if(i&) fan[i]=i+;
else fan[i]=i-;
int cas;
scanf("%d",&cas);
for(int v=;v<=cas;v++)
{
int n,m;
scanf("%d%d",&n,&m);
int F=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==) F++;
if(a[i][j]&&a[i][j]%==) F++;
}
s=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
for(int k=;k<=;k++) num[i][j][k]=++s;
init();
source=s+; src=s+; s+=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(a[i][j]==||a[i][j]%==) add(source,num[i][j][],,);
if(a[i][j]==||a[i][j]%==) add(num[i][j][],src,,);
}
for(int i=;i<=n-;i++)
for(int j=;j<=m;j++)
{
int x;
scanf("%d",&x);
add(num[i][j][],num[i+][j][],,x);
add(num[i+][j][],num[i][j][],,x);
}
for(int i=;i<=n;i++)
for(int j=;j<=m-;j++)
{
int x;
scanf("%d",&x);
add(num[i][j][],num[i][j+][],,x);
add(num[i][j+][],num[i][j][],,x);
}
ans=; flow=;
while(spfa()) mcf();
if(flow==F) printf("Case #%d: %d\n",v,ans);
else printf("Case #%d: -1\n",v);
}
return ;
}

【HDOJ5520】Number Link(费用流)的更多相关文章

  1. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  2. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

  3. zkw费用流+当前弧优化

    zkw费用流+当前弧优化 var o,v:..] of boolean; f,s,d,dis:..] of longint; next,p,c,w:..] of longint; i,j,k,l,y, ...

  4. 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广

    3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 174  Solved: 9 ...

  5. CodeForces 164C Machine Programming 费用流

    Machine Programming 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co One remark ...

  6. Codeforces Gym 100002 E "Evacuation Plan" 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  7. Aizu 2304 Reverse Roads 费用流

    Reverse Roads Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  8. Cyclic Tour HDUOJ 费用流

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  9. 【 UVALive - 2197】Paint the Roads(上下界费用流)

    Description In a country there are n cities connected by m one way roads. You can paint any of these ...

随机推荐

  1. 洛谷 3567/BZOJ 3524 Couriers

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2895  Solved: 1189[Submit][S ...

  2. ElasticSearch High Level REST API【2】搜索查询

    如下为一段带有分页的简单搜索查询示例 在search搜索中大部分的搜索条件添加都可通过设置SearchSourceBuilder来实现,然后将SearchSourceBuilder RestHighL ...

  3. build path导入的jar失效导致找不到类

    今天碰到一个很奇葩的问题,搞起我以后都不敢 build path到jar了 所以我就全部放到lib目录下了,因为之前使用build path导入的jar失效了,一直找不类,具体原因我也不清楚,我之前的 ...

  4. OpenFaceswap 入门教程(3): 软件参数篇!

    OpenFaceswap 的使用可以说是非常简单,只要稍加点拨就可以学会,厉害一点的人根本不需要教程,直接自己点几下就知道了.看了前面安装篇和使用篇.我想大多数人应该会了. 当学会了使用之后,你可能对 ...

  5. Django2.1集成xadmin管理后台所遇到的错误集锦,解决填坑

    django默认是有一个admin的后台管理模块,但是丑,功能也不齐全,但是大神给我们已经集成好了xadmin后台,我们拿来用即可,但是呢,django已经升级到2.1版本了,xadmin貌似跟不上节 ...

  6. python模块之collections模块

    计数器 Counter 计数元素迭代器 elements() 计数对象拷贝 copy() 计数对象清空 clear() from collections import Counter #import ...

  7. Xadmin后台管理系统搭建基于Django1.11.11+Python3.6

    安装python及Django百度即可 主要介绍Xadmin安装 访问地址:https://github.com/sshwsfc/xadmin  下载 安装好之后,将xamdin目录复制到项目 我放在 ...

  8. Gender Equality in the Workplace【职场上的性别平等】

    Gender Equality in the Workplace A new batch of young women - members of the so-called Millennial ge ...

  9. VScode的settings.json配置

    { "editor.mouseWheelZoom": true, "astyle.additional_languages": [ "c", ...

  10. DOS中断及程序调用

    http://www.cnblogs.com/ynwlgh/archive/2011/12/12/2285017.html