hdu 2686 Matrix 最小费用最大流

题目链接: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 ;
}
上一篇:Zabbix WebUI 配置监控Zabbix Agent


下一篇:BFC(Box,Formatting,Context) —— 块级格式化上下文