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. MySQL学习笔记:exists和in的区别

    一.exists函数 表示存在,常常与子查询配合使用. 用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False. 当子查询返回为真时,则外层查询语句将进行 ...

  2. C++之客户消费积分管理系统

    之前数据结构课程设计要求做这么一个小程序,现在贴上源码,来和大家进行交流学习,希望大家给出意见和建议 程序以链表为主要数据结构对客户信息进行存储,对身份证号码判断了位数及构成(前十七位为数字,最后一位 ...

  3. 一步一步学习IdentityServer3 (7)

    在介绍自定义用户服务之前先对IdentityServerServiceFactory说明下 Idr3的服务工厂 下面有很多idr3提供的接口服务, 如:ViewService.UserService. ...

  4. 【LOJ】#2061. 「HAOI2016」放棋子

    题解 水题,可惜要写高精度有点烦 一看障碍物的摆放方式和最后的答案没有关系,于是干脆不读了,直接二项式反演可以得到 设\(g_k\)为一种摆放方式恰好占了k个障碍物 \(f_k = \sum_{i = ...

  5. 高仿360界面的实现(用纯XML和脚本实现)

    源码下载:360UI 本项目XML的桌面渲染依赖GQT开源项目(请感兴趣的朋友加入QQ讨论群:101189702,在群共享文件里下载GQT源码),以下是360界面实现的全部XML代码,所有的代码都在3 ...

  6. python 关键知识点

    学习资源:笨方法学习 python3 将变量传递给脚本--argv 脚本:你编写的 .py 文件. argv 参数变量(argument variable)保存着你运行 python 脚本的参数. i ...

  7. 【基础知识】ASP.NET[基础二(aspx)]

    1.cs可以调用aspx中的runat=server控件,aspx中也可以访问测试中定义的字段.函数,还可以编写复杂的C#代码,for等所有C#代码都可以写在aspx中(不推荐这样写): 2.把代码写 ...

  8. iOS 9应用开发教程之使用代码添加按钮美化按钮

    iOS 9应用开发教程之使用代码添加按钮美化按钮 丰富的用户界面 在iOS9中提供了很多的控件以及视图来丰富用户界面,对于这些视图以及控件我们在上一章中做了简单的介绍.本章我们将详细讲解这些视图. i ...

  9. Python实现QQ自动点赞

    用Python做一个QQ自动点赞神器,上代码: 1 def QQZan(qq): 2 browser = webdriver.Chrome() 3 browser.maximize_window() ...

  10. 机器学习之路:python 多项式特征生成PolynomialFeatures 欠拟合与过拟合

    分享一下 线性回归中 欠拟合 和 过拟合 是怎么回事~为了解决欠拟合的情 经常要提高线性的次数建立模型拟合曲线, 次数过高会导致过拟合,次数不够会欠拟合.再建立高次函数时候,要利用多项式特征生成器 生 ...