题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686

Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every
time yifenfei should to do is that choose a detour which frome the top left
point to the bottom right point and than back to the top left point with the
maximal values of sum integers that area of Matrix yifenfei choose. But from the
top to the bottom can only choose right and down, from the bottom to the top can
only choose left and up. And yifenfei can not pass the same area of the Matrix
except the start and end. 
 
题意描述:n*n的矩阵,每个数为正整数,要求从最左上的位置走到最右下的位置,每次只能向右或向下走一格,然后又从最右下的位置回到最左上的位置,每次只能向上或向左走一格,然后累加这两次路径上的数。注意每个格子只能走一次(除去最左上和最右下),即矩阵中的每个数只能加一次。
 
算法分析:这道题可以运用DP来做,但我的DP思维有限,还不大熟悉,刚好最近刷图论题,介绍一下用最小费用最大流算法解之。
把最左上看作源点,最右下看作汇点。从上往下走一次,再从下往上走一次,其实就等同于从上往下走两次罢了,即从源点到汇点找到两条价值最大且不相交的路径。
首先拆点建图:(i-1)*n+j->(i-1)*n+j+n*n(w为1(注意:源点和汇点为2,因为要找到两条路),cost为矩阵中这个数值);
(i-1)*n+j+n*n -> (i-1)*n+j+1(w为1,cost为0)
(i-1)*n+j+n*n -> i*n+j(w为1,cost为0)
然后用费用流求解即可。(不是题中明确说明每个数都为正吗,我dis[]函数初始化0WA了,这是为什么?)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = ; int n,from,to;
struct node
{
int v,flow,cost;
int next;
}edge[M*];
int head[maxn],edgenum;
int dis[maxn],pre[maxn],pid[maxn],vis[maxn];
int an[][]; void add(int u,int v,int flow,int cost)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow;
edge[edgenum].cost=cost ;edge[edgenum].next=head[u];
head[u]=edgenum++; edge[edgenum].v=u ;edge[edgenum].flow=;
edge[edgenum].cost=-cost ;edge[edgenum].next=head[v];
head[v]=edgenum++;
} int spfa()
{
memset(vis,,sizeof(vis));
memset(dis,-,sizeof(dis));
queue<int> Q;
Q.push(from);
dis[from]=;
vis[from]=;
while (!Q.empty())
{
int u=Q.front() ;Q.pop();
vis[u]=;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (edge[i].flow> && dis[v]<dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=u;
pid[v]=i;
if (!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
return dis[to];
} int mincost()
{
int aug=,maxflow=;
int ans=;
int ncase=;
while ()
{
aug=inf;
int tmp=spfa();
if (tmp==) break;
for (int i=to ;i!=from ;i=pre[i])
{
if (edge[pid[i] ].flow<aug)
aug=edge[pid[i] ].flow;
}
for (int i=to ;i!=from ;i=pre[i])
{
edge[pid[i] ].flow -= aug;
edge[pid[i]^ ].flow += aug;
}
ans += tmp;
ncase++;
if (ncase==) break;
}
return ans-an[][]-an[n][n];
} int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=n ;j++)
scanf("%d",&an[i][j]);
}
from=;
to=*n*n;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=n ;j++)
{
int u=(i-)*n+j;
int v=(i-)*n+j+n*n;
add(u,v,,an[i][j]);
if (j+<=n) add(v,u+,,);
if (i+<=n) add(v,i*n+j,,);
}
}
add(from,from+n*n,,an[][]);
add(n*n,to,,an[n][n]);
int sum=mincost();
printf("%d\n",sum);
}
return ;
}

hdu 2686 Matrix 最小费用最大流的更多相关文章

  1. HDU 2686 Matrix(最大费用最大流+拆点)

    题目链接:pid=2686">http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(由于有 ...

  2. hdu 4494 Teamwork 最小费用最大流

    Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...

  3. HDU 4862 JUMP 最小费用最大流

    2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定 ...

  4. HDU 2686 Matrix(最大费用流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  6. hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

  8. hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. UVa11082 Matrix Decompressing(最小费用最大流)

    题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...

随机推荐

  1. c#中判断对象为空的几种方式(字符串等)

    (1)先了解几个与空类型相关的关键字和对象  Null : 关键字表示不引用任何对象的空引用,它是所有引用类型变量的默认值,在2.0版本之前也就只有引用变量类型可以为null,如(string a=n ...

  2. 复杂的databinding接受Ilist作为数据源

    Combobox控件绑定数据源时,List<T>可以作为数据源,但是List<String,Object> 不存在,我们有时候需要用Dictionary<String,o ...

  3. [leetcode]_Maximum Depth of Binary Tree

    第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解.看来还得好好研究下递归算法. 题目:求一棵树的最大深度. 思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 ...

  4. MongoDB(3):小的细节问题

    1.文档 {“greeting”:“hello,world”,“foo”: 3} 文档中的键/值对是有序的,下面的文档与上面的文档是完全不同的两个文档. {“foo”: 3 ,“greeting”:“ ...

  5. 删除map容器中指定的元素

    for (std::map<Int64,Int64>::iterator iter = ips_forbidden_.begin(); iter != ips_forbidden_.end ...

  6. mysql 导入导出数据库、数据表

    Linux下 均在控制台下操作. 导入数据库: 前提:数据库和数据表要存在(已经被创建) (1)将数据表 test_user.sql 导入到test 数据库的test_user 表中 [root@te ...

  7. r

    http://vgoulet.act.ulaval.ca/en/emacs/mac/ mean() 均值 sd()标准差 http://www.walware.de/it/downloads/stat ...

  8. 6.24AppCan移动开发者大会价值30万的展示机会归了谁?

    最近,小编的邮箱都被挤爆了! 来自开发者的邮件一封封涌进邮箱,VIP展位申请,TOP30 APP评选,感谢大家的积极参与,展位有限,APP名额有限,开发者的热情无限. 经过谨慎筛选.综合评定后,以下5 ...

  9. golang:slice陷阱

    slice陷阱,slice底层指向某个array,在赋值后容易导致array长期被引用而无法释放

  10. 怎么解决/bin/sh: arm-linux-gcc: not found make

      1.arm-linux-gcc 环境变量没有设,所以找不到这个编译器 在/etc/profile里添加arm-linux-gcc的存放路径 sudo -s gedit /etc/profile 编 ...