Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1890    Accepted Submission(s): 1005

Problem 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
 
Author
yifenfei
 
Source
 题意:给一个n*n的距阵。每一个点都有一个值。问从(0,0)到(n-1, n-1)点(仅仅能从左到右 或 从上到下)再回到(0,0)点(仅仅能从右到左 或 下到上)经过的点的值总和最大是多少?每一个点仅仅能走一次。
解题:事实上就是找两条从(0,0)到(n-1,n-1)总和最大的路.拆点法。每一个边容量为1。费用:对于点的本身,边权为点权,非点的边权值为0。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN = 10010;
const int MAXM = 1001000;
const int INF = 1<<30;
struct EDG{
int to,next,cap,flow;
int cost; //单位价格
}edg[MAXM];
int head[MAXN],eid;
int pre[MAXN], cost[MAXN] ; //点0~(n-1) void init(){
eid=0;
memset(head,-1,sizeof(head));
}
void addEdg(int u,int v,int cap,int cst){
edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst;
edg[eid].cap=cap; edg[eid].flow=0; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst;
edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;
} bool inq[MAXN];
bool spfa(int sNode,int eNode , int n){
queue<int>q;
for(int i=0; i<n; i++){
inq[i]=false; cost[i]= -1;
}
cost[sNode]=0; inq[sNode]=1; pre[sNode]=-1;
q.push(sNode);
while(!q.empty()){
int u=q.front(); q.pop();
inq[u]=0;
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(edg[i].cap-edg[i].flow>0 && cost[v]<cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费
cost[v] = cost[u]+edg[i].cost;
pre[v]=i; //记录路径上的边
if(!inq[v])
q.push(v),inq[v]=1;
}
}
}
return cost[eNode]!=-1; //推断有没有增广路
}
//反回的是最大流,最小花费为minCost
int minCost_maxFlow(int sNode,int eNode ,int& minCost , int n){
int ans=0;
while(spfa(sNode,eNode , n)){ for(int i=pre[eNode]; i!=-1; i=pre[edg[i^1].to]){
edg[i].flow+=1; edg[i^1].flow-=1;
minCost+=edg[i].cost;
}
ans++;
if(ans==2)break;
}
return ans;
}
int main(){
int n,mapt[35][35];
while(scanf("%d",&n)>0){
init();
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d",&mapt[i][j]);
int s = 0 , t = n*n-1; for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
if(i||j){
addEdg(i*n+j , i*n+j+n*n , 1 , mapt[i][j]);
if(j+1<n) addEdg(i*n+j+n*n, i*n+j+1 , 1 , 0 );
if(i+1<n) addEdg(i*n+j+n*n, (i+1)*n+j , 1 , 0);
}
else{
addEdg(s , 1 , 1,0) , addEdg(s , n , 1 , 0);
}
int maxCost=mapt[0][0];
if(n>1) maxCost+=mapt[n-1][n-1];
minCost_maxFlow(s , t , maxCost , n*n*2);
printf("%d\n",maxCost);
}
}

HDU 2686 Matrix(最大费用流)的更多相关文章

  1. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  2. POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

    累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...

  3. HDU 2686 Matrix(最大费用最大流+拆点)

    题目链接:pid=2686">http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(由于有 ...

  4. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

  5. HDU 5988 Coding Contest(费用流+浮点数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ...

  6. [hdu 2686]Matrix

    网上说这道题的题解是费用流 我粗粗看了一下数据范围,觉得出题者似乎是让我们用 “大(d)屁(p)” 的样子,为了尊重出题人,我还是写一写吧喵~ 首先,一条回路可以看做是两条路齐头并进,这是 大屁 和 ...

  7. poj 3422 Kaka's Matrix Travels 费用流

    题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...

  8. My Brute HDU - 3315(KM || 费用流)

    题意: 有S1到Sn这n个勇士要和X1到Xn这n个勇士决斗,初始时,Si的决斗对象是Xi. 如果Si赢了Xi,那么你将获得Vi分,否则你将获得-Vi分. Si和Xi对决时,Si有初始生命Hi,初始攻击 ...

  9. hdu 1533 KM或费用流

    以前用KM写过,现在再用费用流写. #include <iostream> #include <cstdio> #include <cstring> #includ ...

随机推荐

  1. 排序与相关性(Sorting and Relevance)

    本文翻译自Elasticsearch官方指南的Sorting and Relevance一章的第一节. 原文地址:http://www.elastic.co/guide/en/elasticsearc ...

  2. No.20 selenium学习之路之文件读写

    1.open 使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.tx ...

  3. Javascript之对象的创建

    面向对象语言有一个非常显著的标志,那就是它们都有类的概念,通过类之间的继承就可以达到任意创建具有相同属性方法的对象.而在ECMAScript中并没有类的概念,它把对象定义为:无序属性的集合,其属性包含 ...

  4. LeetCode517. Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  5. GreenPlum学习笔记:date_part与extract提取日期时间、时间差

    GP可以使用date_part / extract从日期时间类型中抽取部分内容. 方法一:extract 格式:extract(field from source)  extract函数从日期.时间数 ...

  6. KnockoutJs学习笔记(四)

    由于Writable computed observables和How dependency tracking works的要求相对较高,我先跳过这两篇,学习Pure computed observa ...

  7. 在jenkins里使用SCM管理jenkinsfile

    注意,这样作的前提是,插件里一定要安装pipeline和git. 设置就比较简单了. 在gitlat里生成一个演示的jenkinsfile pipeline { agent { node { labe ...

  8. HBase(五)HBase的API操作

    一.项目环境搭建 新建 Maven Project,新建项目后在 pom.xml 中添加依赖: <dependency> <groupId>org.apache.hbase&l ...

  9. Web前端开发最佳实践系列文章汇总

    Web前端开发最佳实践(1):前端开发概述 Web前端开发最佳实践(2):前端代码重构 Web前端开发最佳实践(3):前端代码和资源的压缩与合并 Web前端开发最佳实践(4):在页面中添加必要的met ...

  10. react-native第一次开发记录

    1.安装指定版本 react-native init demo --verbose --version 0.41.0 2.更新依赖包 npm install -g npm-check-updates ...