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. MVC查询数据接收及校验

    本来想写一篇aspx的TreeView控件绑值的文章的,在写案例的时候,写了一半,发现有些地方还得考虑以下,就留待下次了. 这一篇的话,是最近在开发一个项目的时候,有大量的页面和数据表,需要花式查询, ...

  2. 中国移动飞信WAP登陆分析及脚本

    中国移动飞信WAP网页版 http://f.10086.cn/im5/ 用WAP飞信登录并向好友发送信息,同时用wireshark抓包. 1.过滤POST表单提交数据包(wireshark规则: ht ...

  3. 极化码之tal-vardy算法(2)

    上一节我们了解了tal-vardy算法的大致原理,对所要研究的二元输入无记忆对称信道进行了介绍,并着重介绍了能够避免输出爆炸灾难的合并操作,这一节我们来关注信道弱化与强化操作. [1]<Chan ...

  4. css左右布局的几种实现方式和优缺点

    记录一下左右布局的实现方式,实现的具体效果是,左侧固定宽度,高度适中等于父元素的高度,父元素的高度由右侧内容决定: html代码如下: <div class="parent" ...

  5. MySQL高级查询(二)

    EXISTS 和NOT EXISTS子查询 EXISTS子查询 语法:   SELECT ……… FROM 表名 WHERE EXISTS (子查询); 例: SELECT `studentNo` A ...

  6. 【转】String字符串相加的问题

    String字符串相加的问题 前几天同事跟我说我之前写的代码中在操作字符串时候,使用字符串相加的方式而不是使用StringBuffer或者StringBuilder导致内存开销很大.这个问题一直在困扰 ...

  7. 移植u-boot-2012.04.01到JZ2440

    开发环境:Ubuntu 12.04 开发板:JZ2440  256M NandFlash  64M SDRAM 交叉编译器:arm-linux-gcc-4.3.2 u-boot:u-boot-2012 ...

  8. httpd2.2配置文件详解

    httpd2.2官方配置手册:http://httpd.apache.org/docs/2.2/ 注意:关闭防火墙,iptables规则 vim /etc/sysconfig/selinux SELI ...

  9. UI自动化测试(四)AutoIT工具使用和robot对象模拟键盘按键操作

    AutoIT简介 AutoIt 目前最新是v3版本,这是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作.它利用模拟键盘按键,鼠标移动和窗口/ ...

  10. windows phone 模拟器

    window phone 模拟器启动报错 修改Bios设置,我的是yoga pro 2,只修改 即可.启动成功