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
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
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(最大费用最大流)的更多相关文章

  1. hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. uva12534 Binary Matrix 2(最小费用最大流)

    http://blog.csdn.net/qq564690377/article/details/17082055 做的时候觉得明显是费用流,但是真的不知道怎么建图,看了上面的博客会稍微清晰一点.后面 ...

  3. [poj] 3422 Kaka's Matrix Travels || 最小费用最大流

    原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...

  4. UVa11082 Matrix Decompressing(最小费用最大流)

    题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...

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

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

  6. POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

    POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...

  7. POJ3422 Kaka's Matrix Travels 【费用流】*

    POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...

  8. POJ 3422 Kaka's Matrix Travels(费用流)

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6792   Accepted:  ...

  9. POJ 3422 Kaka's Matrix Travels 【最小费用最大流】

    题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...

随机推荐

  1. Matlab入门学习(程序设计)

    一.循环(for,while) for循环: for i=begin:step:end ...... end while循环: while condition ...... end 二.分枝(if,i ...

  2. 《JavaScript闯关记》视频版硬广

    <JavaScript闯关记>视频版硬广 stone 在菜航工作时,兼任内部培训讲师,主要负责 JavaScript 基础培训,2016年整理的<JavaScript闯关记>课 ...

  3. day18<集合框架+>

    集合框架(Map集合概述和特点) 集合框架(Map集合的功能概述) 集合框架(Map集合的遍历之键找值) 集合框架(Map集合的遍历之键值对对象找键和值) 集合框架(Map集合的遍历之键值对对象找键和 ...

  4. xml跟sql查找

    xml小白笔记 ....... <sql id="wDishesColumns"> a.id AS "id", a.pid AS "pid ...

  5. 【充分利用你的Azure】将Azure用作云计算平台(1)

    本文将围绕几个步骤来讲. 因为本人是MSP,微软送了150刀的额度给我随便使用.这篇文章是要讲将Azure用作云计算平台,对于我来说,我是做机器学习的,那么Azure就要有机器学习的平台. 本文的目的 ...

  6. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  7. Longge's problem poj2480 欧拉函数,gcd

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6918   Accepted: 2234 ...

  8. Power Strings poj2406(神代码)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 29402   Accepted: 12296 D ...

  9. FastDFS简介和架构图(内容来自于阅读fastdfs官方文档的总结)

    一.FastDFS简介 1. FastDFS是一个轻量级的开源分布式文件系统 2. FastDFS主要解决了大容量的文件存储和高并发访问的问题,文件存取时实现了负载均衡 3. FastDFS实现了软件 ...

  10. http://codeforces.com/contest/535/problem/C

    C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...