Matrix Again(最大费用最大流)
Matrix Again |
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) |
Total Submission(s): 16 Accepted Submission(s): 7 |
Problem Description
Starvae 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 starvae should to do is that choose a detour which from 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 starvae 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 starvae can not pass the same area of the Matrix except the start and end.. Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU. |
Input
The input contains multiple test cases.
Each case first line given the integer n (2<=n<=600) Then n lines, each line include n positive integers. (<100) |
Output
For each test case output the maximal values starvae can get.
|
Sample Input
2 |
Sample Output
28 |
Author
Starvae
|
Source
HDOJ Monthly Contest – 2010.04.04
|
Recommend
lcy
|
/*
题意:给你一个n*n的矩阵每个点都有相应的权值,然后让你求出从左上角走到右下角,再返回能讲过的最大权值是多少,但是两次的路
线不能经过同一个点。 #初步思路:最大费用最大流由于每个数只能取一次,所以对当前每一个数要进行拆点,将i拆为i和i'',然后从i连接一条边到i''
,容量为1,费用为第i个点的费用,然后将i和取完i点后能取的数连一条边,容量为1,费用为0。因为是从(1,1)->(n,n)->(1,
1),所以我们建立超级源点S,连接S和第一个节点,容量为2(因为要走两条路径),费用为0,建立超级汇点T,连接点(n*n)'' 到
T,容量为1,费用为0,然后求一次最大费用最大流,因为1和n*n这两个点算了两次,故需要减去一次他们的费用之和,发现网络流的题
目数组的大小很重要,之前因为数组问题出现了各种错误,望今后引起高度重视 #补充:超内存......用数组来模拟栈就过了
*/ #include<bits/stdc++.h>
using namespace std;
int n;
int mapn[][];
int tol=;
/**************************************************数组模拟************************************************************/
const int MAXN = ;
const int MAXM = ;
const int INF = <<;
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=;
memset(head,-,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=; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst;
edg[eid].cap=; edg[eid].flow=; head[v]=eid++;
} bool inq[MAXN];
int q[MAXN];
bool spfa(int sNode,int eNode , int n){
int l=,r=;
for(int i=; i<n; i++){
inq[i]=false; cost[i]= -;
}
cost[sNode]=; inq[sNode]=; pre[sNode]=-;
q[r++]=sNode;
while(l!=r){
int u=q[l++]; //数组模拟
if(l==MAXN)l=;
inq[u]=;
for(int i=head[u]; i!=-; i=edg[i].next){
int v=edg[i].to;
if(edg[i].cap-edg[i].flow> && cost[v]<cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费
cost[v] = cost[u]+edg[i].cost;
pre[v]=i; //记录路径上的边
if(!inq[v]){
if(r==MAXN)r=;
q[r++]=v; inq[v]=;
}
}
}
}
return cost[eNode]!=-; //判断有没有增广路
}
//反回的是最大流,最小花费为minCost
int minCost_maxFlow(int sNode,int eNode ,int& minCost , int n){
int ans=;
while(spfa(sNode,eNode , n)){ for(int i=pre[eNode]; i!=-; i=pre[edg[i^].to]){
edg[i].flow+=; edg[i^].flow-=;
minCost+=edg[i].cost;
}
ans++;
if(ans==)break;
}
return ans;
}
/**************************************************数组模拟************************************************************/
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
//cout<<n<<endl;
tol=n*n;//总的点数
init();
// 首先是初始化,总共可能n*n个点 0到n*n-1
// 但是这个题还需要回来,所以可能走的点就是
// 2*n*n
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&mapn[i][j]);
}
}
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(i||j){ //如果这个点不是起点 addEdg(i*n+j, i*n+j+n*n , , mapn[i][j]);
//如果这个点还没有到达右边的边界
if(j+<n)//向右边走一步
addEdg(i*n+j+n*n, i*n+j+ , , ); //如果这个点还没有到达下面的边界
if(i+<n)//想下边走一步
addEdg(i*n+j+n*n, (i+)*n+j , , );
}
else{//如果这个点是原点的话
addEdg( , , ,) , addEdg( , n , , );
}
}
}
int ans=;
minCost_maxFlow(,tol-,ans,tol*);
ans+=mapn[][];
if(n>) ans+=(mapn[n-][n-]);
printf("%d\n",ans);
}
return ;
}
Matrix Again(最大费用最大流)的更多相关文章
- hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- uva12534 Binary Matrix 2(最小费用最大流)
http://blog.csdn.net/qq564690377/article/details/17082055 做的时候觉得明显是费用流,但是真的不知道怎么建图,看了上面的博客会稍微清晰一点.后面 ...
- [poj] 3422 Kaka's Matrix Travels || 最小费用最大流
原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...
- UVa11082 Matrix Decompressing(最小费用最大流)
题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
- POJ 3422 Kaka's Matrix Travels (最小费用最大流)
POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...
- POJ3422 Kaka's Matrix Travels 【费用流】*
POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...
- POJ 3422 Kaka's Matrix Travels(费用流)
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6792 Accepted: ...
- POJ 3422 Kaka's Matrix Travels 【最小费用最大流】
题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...
随机推荐
- [UIKit学习]06.懒加载,模型,自定义代码段,重写构造方法
懒加载 在get中加载,且只加载一次 - (NSArray *)shops { if (_shops == nil) { NSString *file = [[NSBundle mainBundle] ...
- 翻译:MariaDB wait/nowait
本文为mariadb官方手册:wait/nowait的译文.原文:https://mariadb.com/kb/en/library/wait-and-nowait/ 从MariaDB 10.3.0开 ...
- JSP入门 生命周期
我们之前使用的都是javax.servlet.http.HttpServlet,这个类实现了javax.servlet.Servlet接口,而这个接口中定义的三个方法是所有servlet都必须实现的. ...
- D. How many trees? DP
D. How many trees? time limit per test 1 second memory limit per test 64 megabytes input standard in ...
- 重学C语言---01概述
1.什么是C语言 C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点.计算机语言是从第二次世界大战以后,经历了戏剧性的发展过程.从机器语言到汇编语言和高级语言.C语言是与硬件 ...
- MySQL Base
/* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 ---> input pwd /* 数据库存贮引擎 */ InnoDB : 1) ...
- 【DDD】领域驱动设计实践 —— Domain层实现
本文是DDD框架实现讲解的第三篇,主要介绍了DDD的Domain层的实现,详细讲解了entity.value object.domain event.domain service的职责,以及如何识别出 ...
- git-分支使用方式
需求场景:假如你看着教程完成了一个项目,但是感觉第一次代码掌握不牢,想要进行第二次代码练习--如果某某心里想我还有初始备份文件,我此时的心里独白是你的硬盘还够用吗o(╯□╰)o 1 创建一个新分支 - ...
- 组件 layui 表单抓取数据四步走
注意事项: layui 中提交按钮是基于"监听"机制实现的. form.on() 的调用需置于 layui.use 的回调函数中. 末尾的 'return false' 不可或缺, ...
- 张高兴的 Windows 10 IoT 开发笔记:使用 MAX7219 驱动 8×8 点阵
GitHub:https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/MAX7219