lightoj 1071 Baker Vai(费用流)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/82229103


1071 - Baker Vai

    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

All of you must have heard the name of Baker Vai. Yes, he rides a bike and likes to help people. That's why he is popular amongst general people.

Baker Vai lives in a city which can be modeled as a 2D m x n matrix. Where the north-west corner is cell 1, 1 and the south-east corner is cell m, n. In each cell there are certain amount of people who needs help which is already known to Baker Vai.

Each day Baker Vai starts his journey from the north-west corner and he can only go to east or south. This way he reaches the south-east corner of the city. After that he returns back to the north-west, but this time he can only move to west or north. He doesn't want a cell to be visited twice other than the two corners. And if he visits a cell, he helps all the people in the cell.

Now you are given the map of the city and the number of people who need help in all cells for a particular day. You have to help Baker Vai finding the maximum number of people he can help in that day.

Input

Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case contains a blank line and two integers, m, n (2 ≤ m, n ≤ 100). Each of the next m lines will contain n integers, denoting the number of people who are in need. In a cell there will be no more than 20 people and a cell can be empty, too.

Output

For each test case, print the case number and the maximum number of people Baker Vai can help considering the above conditions.

Sample Input

Output for Sample Input

2

 

3 3

1 1 1

1 0 1

1 1 1

 

3 4

1 1 0 1

1 1 1 1

0 1 10 1

Case 1: 8

Case 2: 18

 题目大意:给一个n*m的矩阵,有一个人在左上角,要走到右下角,再从右下角走回左上角,走到右下角的过程中只能走右边和下边,走回左上角只能走左边和上边,要求路径不能重复,矩阵的每个格子有一个值 ,每次走到这个格子就能得到这个值,要求这个值最大

刚开始想着走回左上角要怎么办,其实很简单,它等同于让我们去找另外一条走到右下角的路径,想到这里就很简单了,我们将每个点拆点,起点和终点拆的点相连的边容量为2,其他的为1,因为起点终点会经过两遍,而其他点只能经过一遍,费用为权值取负,然后跑一遍费用流,但是这样得出的答案并不是正确答案,因为起点和终点都走了两遍,所以要减去一次起点和终点的权值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=20010;
const int maxm=1e6+7;
const int inf=0x3f3f3f3f;
struct Node
{
	int to;
	int capa;
	int cost;
	int next;
}edge[maxm];
int cnt;
int n,m;
int source,sink;
int head[maxn];
int map[110][110];
int dis[maxn];
bool vis[maxn];
int pre[maxn];
int rec[maxn];
void init()
{
	cnt=0;
	memset(head,-1,sizeof(head));
	return;
}
void add(int u,int v,int capa,int cost)
{
	edge[cnt].to=v;
	edge[cnt].capa=capa;
	edge[cnt].cost=cost;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	edge[cnt].to=u;
	edge[cnt].capa=0;
	edge[cnt].cost=-cost;
	edge[cnt].next=head[v];
	head[v]=cnt++;
	return;
}
int getIndex(int x,int y)
{
	return (x-1)*m+y;
}
bool spfa()
{
	queue<int> que;
	que.push(source);
	memset(dis,inf,sizeof(dis));
	memset(vis,false,sizeof(vis));
	memset(rec,-1,sizeof(rec));
	memset(pre,-1,sizeof(pre));
	dis[source]=0;
	vis[source]=true;
	while(!que.empty())
	{
		int node=que.front();
		que.pop();
		vis[node]=false;
		for(int i=head[node];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].capa>0&&dis[v]>dis[node]+edge[i].cost)
			{
				dis[v]=dis[node]+edge[i].cost;
				rec[v]=i;
				pre[v]=node;
				if(!vis[v])
				{
					vis[v]=true;
					que.push(v);
				}
			}
		}
	}
	return dis[sink]!=inf;
}
int mcmf()
{
	int mincost=0;
	while(spfa())
	{
		int node=sink;
		int flow=inf;
		while(node!=source)
		{
			flow=min(flow,edge[rec[node]].capa);
			node=pre[node];
		}
		node=sink;
		while(node!=source)
		{
			mincost+=flow*edge[rec[node]].cost;
			edge[rec[node]].capa-=flow;
			edge[rec[node]^1].capa+=flow;
			node=pre[node];
		}
	}
	return -mincost;
}
int main()
{
	//freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
	int test;
	scanf("%d",&test);
	for(int cas=1;cas<=test;cas++)
	{
		init();
		scanf("%d%d",&n,&m);
		source=1;
		sink=n*m*2;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				scanf("%d",&map[i][j]);
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				int x=getIndex(i,j);
				if((i==1&&j==1)||(i==n&&j==m))
				{
					add(x,x+n*m,2,-map[i][j]);
				}
				else
				{
					add(x,x+n*m,1,-map[i][j]);
				}
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				int x=getIndex(i,j);
				int down=getIndex(i+1,j);
				int right=getIndex(i,j+1);
				if(i<n) add(x+n*m,down,1,0);
				if(j<m) add(x+n*m,right,1,0);
			}
		}
		printf("Case %d: %d\n",cas,mcmf()-map[1][1]-map[n][m]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/82229103