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. java GUI编程二

    java基础学习总结--GUI编程(二) 一.事件监听 测试代码一: 1 package cn.javastudy.summary; 2 3 import java.awt.*; 4 import j ...

  2. BP算法

    1986年Rumelhart和McCelland在<并行分布式处理>中提出了BP算法,即非线性连续变换函数的多层感知器网络误差反向传播算法. 该算法的思想是:学习过程分为信号的正向传播与误 ...

  3. Conscription poj3723(最大生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6870   Accepted: 2361 Desc ...

  4. Distribute Candies

    Given an integer array with even length, where different numbers in this array represent different k ...

  5. webservice部署到服务器报错

    System.Net.WebException: 基础连接已经关闭: 发送时发生错误. ---> System.IO.IOException: 从传输流收到意外的 EOF 或 0 个字节. 在 ...

  6. Elixir游戏服设计三

    玩家进程用gen_server来建模,我不直接使用 use GenServer, 而是使用exactor,该库可以去掉反锁的接口定义. 我们新建一个 player_server_manager app ...

  7. win7+ ubuntu 双系统

    windows +linux双系统组合有多种方式,只要划好分区两者即可共处,本文是为了解决两者在启动时遇到的问题. 第三方启动器(例如grub,grub2,grub4dos等)

  8. ubuntu访问文件服务器

    随便打开一个文件夹,按ctrl+L 输入 smb://172.29.17.1 然后输入用户名和密码即可

  9. MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等

    drop procedure if exists pro_rep_shadow_rs; delimiter | ---------------------------------- -- rep_sh ...

  10. YYHS-string(线段树)

    题解 这道题给你两个操作,一个升序,一个降序 我们可以观察到这个字符串都是由小写字母组成的,只有26个字符,所以我们开一个26个字符的线段树 每次查询的时候找到这个区间内'a'到'z'的数量,再判断一 ...