HDU 2686 Matrix(费用流)

题意:有一个n*n的矩阵,矩阵的格子中每个都有一个正数.现在你要从左上角走到右下角去,然后在从右下角回到左上角.过程中除了左上角和右下角外,任意网格最多走一次,且要求你所走过的所有网格的权值和最大,为最大值是多少?

思路:和POJ3422一个类型的题目都是在矩阵中特定的走法求权值最大,建图的话都是拆点来做,一个表示进,一个表示出,这里因为要从(1,1)走到(n,n)再回到(1,1)相当于找两条除了这两个点不相交的路径,那么从(n,n)走回到(1,1)其实就相当于从(1,1)走到(n,n),然和和POJ3422几乎一样的建图,不多说,注意的是源点和汇点和边连接时的容量为2,因为走两遍。


#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=2500;
int T,cas=1;
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(){}
    Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
};

struct MCMF
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool inq[maxn];     //是否在队列
    int d[maxn];        //Bellman_ford单源最短路径
    int p[maxn];        //p[i]表从s到i的最小费用路径上的最后一条弧编号
    int a[maxn];        //a[i]表示从s到i的最小残量

    //初始化
    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;++i) G[i].clear();
    }

    //添加一条有向边
    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    //求一次增广路
    bool BellmanFord(int &flow, int &cost)
    {
        for(int i=0;i<n;++i) d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0, a[s]=INF, inq[s]=true, p[s]=0;
        queue<int> Q;
        Q.push(s);
        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
            for(int i=0;i<G[u].size();++i)
            {
                Edge &e=edges[G[u][i]];
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                {
                    d[e.to]= d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]= min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }
                }
            }
        }
        if(d[t]==INF) return false;
        flow +=a[t];
        cost +=a[t]*d[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -=a[t];
            u = edges[p[u]].from;
        }
        return true;
    }

    //求出最小费用最大流
    int Min_cost()
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return cost;
    }
}mc;

int d[maxn][maxn];

int main()
{
	int n,m;
	while (scanf("%d",&n)!=EOF)
	{
        for (int i = 1;i<=n;i++)
			for (int j = 1;j<=n;j++)
				scanf("%d",&d[i][j]);
		mc.init(2*n*n+2,0,2*n*n+1);
		mc.AddEdge(0,1,2,0);
		mc.AddEdge(2*n*n,2*n*n+1,2,0);
		mc.AddEdge(1,n*n+1,1,0);        //走了两遍有一次重复了,所以费用为0
		mc.AddEdge(n*n,2*n*n,1,0);
		for (int i = 1;i<=n;i++)
			for (int j = 1;j<=n;j++)
			{
				int id = (i-1)*n+j;
				mc.AddEdge(id,id+n*n,1,-d[i][j]);
				if (i<=n-1)
				{	
					mc.AddEdge(id+n*n,id+n,1,0);
				}
				if (j<=n-1)
				{
					mc.AddEdge(id+n*n,id+1,1,0);
				}
			}
			printf("%d\n",-mc.Min_cost());
	}
} 


Description

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. 
 

Input

The input contains multiple test cases. 
Each case first line given the integer n (2<n<30) 
Than n lines,each line include n positive integers.(<100) 
 

Output

For each test case output the maximal values yifenfei can get.
 

Sample Input

       
       
2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
 

Sample Output

       
       
28 46 80
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值