LightOj--1071--Baker Vai(拆点最大费用流)

来源:互联网 发布:淘宝卖家怎么评差评 编辑:程序博客网 时间:2024/05/21 17:21

Baker Vai
Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status

Description

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

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

Sample Output

Case 1: 8

Case 2: 18

Source

Problem Setter: Jane Alam Jan

题意:n*m的地图,每个格子有一个权值,一个人从(1,1)走到(n,m)然后再走回来,走的时候只能向南或者向东,到达每一个格子可以获取格子的权值,但是除了起点还有终点之外其他点只能走一次,问最终获取的权值最大是多少
一般的最大费用拆点,因为每个点只能走一次,所以点要分为左点还有右点,权值为1,起点还有终点除外,最后跑一边最大费用流
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define MAXN 30000#define MAXM 100000#define INF 0x3f3f3f3fstruct node{int u,v,cap,flow,cost,next;}edge[MAXM];int dis[MAXN],head[MAXN],vis[MAXN],pre[MAXN],map[200][200];int n,m,cnt,sink,source;void init(){cnt=0;memset(head,-1,sizeof(head));}int num(int x,int y){return (x-1)*m+y;}void add(int a,int b,int c,int w){node E={a,b,c,0,w,head[a]};edge[cnt]=E;head[a]=cnt++;node E1={b,a,0,0,-w,head[b]};edge[cnt]=E1;head[b]=cnt++;}void getmap(){int d=n*m;source=1,sink=num(n,m)+d;memset(map,0,sizeof(map));for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){scanf("%d",&map[i][j]);int cap=1;if((i==1&&j==1)||(i==n&&j==m))cap=2;add(num(i,j),num(i,j)+d,cap,map[i][j]);}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(i+1<=n) add(num(i,j)+d,num(i+1,j),1,0);if(j+1<=m)add(num(i,j)+d,num(i,j+1),1,0);}}}bool BFS(int s,int t){queue<int>q;memset(vis,0,sizeof(vis));memset(dis,-INF,sizeof(dis));memset(pre,-1,sizeof(pre));vis[s]=1;dis[s]=0;q.push(s);while(!q.empty()){int u=q.front();q.pop();vis[u]=0;for(int i=head[u];i!=-1;i=edge[i].next){node E=edge[i];if(dis[E.v]<dis[E.u]+E.cost&&E.cap>E.flow){dis[E.v]=dis[E.u]+E.cost;pre[E.v]=i;if(!vis[E.v]){vis[E.v]=1;q.push(E.v);}}}}return pre[t]!=-1;}void mcmf(int s,int t,int &cost,int &flow){flow=cost=0;while(BFS(s,t)){int Min=INF;for(int i=pre[t];i!=-1;i=pre[edge[i^1].v]){node E=edge[i];Min=min(Min,E.cap-E.flow);}for(int i=pre[t];i!=-1;i=pre[edge[i^1].v]){edge[i].flow+=Min;edge[i^1].flow-=Min;cost+=edge[i].cost*Min;}flow+=Min;}}int main(){int t;int k=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);init();getmap();int flow,cost;mcmf(source,sink,cost,flow);printf("Case %d: %d\n",k++,cost-map[1][1]-map[n][m]);}return 0;}

0 0