LightOj--1071--Baker Vai(拆点最大费用流)
来源:互联网 发布:淘宝卖家怎么评差评 编辑:程序博客网 时间:2024/05/21 17:21
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
#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;}
- LightOj--1071--Baker Vai(拆点最大费用流)
- LightOJ 1071 - Baker Vai 最大费用流
- lightoj 1071 - Baker Vai 【最大费用最大流】
- Lightoj1071:Baker Vai(最小费用最大流)
- lightoj 1071 - Baker Vai(双线DP)
- lightoj 1071 - Baker Vai DP
- LightOJ - 1071 Baker Vai(记忆化搜索)
- LightOJ 1071 - Baker Vai(水DP)
- lightoj 1071 Baker Vai (思维dp)
- Baker Vai Light OJ 1071
- Light oj 1071 - Baker Vai(记忆化)
- Light OJ 1071 Baker Vai (DP)
- light Oj 1071 - Baker Vai(记忆化搜索)
- BZOJ1877(最小费用最大流+拆点)
- hdu3395(拆点+最大费用流)
- hdu4411Arrest【最小费用最大流 拆点】
- HDU 2686 Matrix(最大费用最大流+拆点)
- POJ 3422 Kaka's Matrix Travels(最大费用最大流 + 拆点)
- 公众平台调整SSL安全策略,请开发者注意升级
- STL乱搞 Codeforces675D Tree Construction
- PPT怎么x的几次方?
- Digilent Basys2开发板驱动安装失败
- 初学JAVA环境变量配置以及编译程序和运行
- LightOj--1071--Baker Vai(拆点最大费用流)
- HDOJ 5600-N bulbs【模拟】
- Java基础 - 1.Java 语言特性及其基本语法
- 算法设计☞快速排序
- CROC 2016 - Elimination Round(A)bfs
- PPT如何存出文稿中的图片
- POJ 2411 Mondriaan'sDream(状压DP)
- 线段_线段树
- JS学习14(Canvas)