Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2485    Accepted Submission(s): 1314

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方阵的左上角走到右下角(只往下或右走)再从右下角回到左上角(只往上或左走)的最大费用。
代码:
//费用流模板题,因为题目中的表述就是一个网络流模型,拆点建图,没点只经过一次,起点终点容量为2,
//其他点容量为1,要求最大费用费用为负权值,套模板。起点和终点经历了两次,最后要减去。
/****************最小费用最大流模板,白书363页*******************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=**+,inf=0x7fffffff;//本体拆点,数组多开两倍
struct Edge
{
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int cs):from(u),to(v),cap(c),flow(f),cost(cs){}
};
struct MCMF
{
int n,m,s,t;
vector<Edge>edges;
vector<int>g[maxn];
int inq[maxn],d[maxn],p[maxn],a[maxn];
void init(int n)
{
this->n=n;
for(int i=;i<n;i++) g[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back((Edge){from,to,cap,,cost});
edges.push_back((Edge){to,from,,,-cost});
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool BellmanFord(int s,int t,int &flow,int &cost)
{
for(int i=;i<n;i++) d[i]=inf;
memset(inq,,sizeof(inq));
d[s]=;inq[s]=;p[s]=;a[s]=inf;
queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
inq[u]=;
for(int i=;i<(int)g[u].size();i++){
Edge &e=edges[g[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
p[e.to]=g[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]) {q.push(e.to);inq[e.to]=;}
}
}
}
if(d[t]==inf) return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s){
edges[p[u]].flow+=a[t];
edges[p[u]^].flow-=a[t];
u=edges[p[u]].from;
}
return true;
}
int Mincost(int s,int t)
{
int flow=,cost=;
while(BellmanFord(s,t,flow,cost));
return cost;//返回最小费用,flow存最大流
}
}MC;
/**********************************************************************************/
int main()
{
int n,mp[][];
while(scanf("%d",&n)==){
int s=,t=n*n*+;
MC.init(n*n*+);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&mp[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int id=(i-)*n+j;
if(id==){
MC.AddEdge(id,id+n*n,,-mp[i][j]);
MC.AddEdge(s,id,,);
}
else if(id==n*n){
MC.AddEdge(id,id+n*n,,-mp[i][j]);
MC.AddEdge(id+n*n,t,,);
}
else MC.AddEdge(id,id+n*n,,-mp[i][j]);
if(i<n){
int nid=id+n;
MC.AddEdge(id+n*n,nid,,);
}
if(j<n){
int nid=id+;
MC.AddEdge(id+n*n,nid,,);
}
}
}
int ans=-(MC.Mincost(,n*n*+)+mp[][]+mp[n][n]);
printf("%d\n",ans);
}
return ;
}

HDU2686 费用流 模板的更多相关文章

  1. HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解

    题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...

  2. 初识费用流 模板(spfa+slf优化) 餐巾计划问题

    今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...

  3. hdu1533 费用流模板

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 费用流模板(带权二分图匹配)——hdu1533

    /* 带权二分图匹配 用费用流求,增加源点s 和 汇点t */ #include<bits/stdc++.h> using namespace std; #define maxn 1000 ...

  5. 算法复习——费用流模板(poj2135)

    题目: Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16898   Accepted: 6543 De ...

  6. Spfa费用流模板

    ; ,maxm=; ,fir[maxn],nxt[maxm],to[maxm]; int cap[maxm],val[maxm],dis[maxn],path[maxn]; void add(int ...

  7. zkw费用流模板

    理论:http://www.cnblogs.com/acha/p/6735037.html #include<cstdio> #include<cstring> #includ ...

  8. 【费用流】【Next Array】费用流模板(spfa版)

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...

  9. 费用流 ZOJ 3933 Team Formation

    题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...

随机推荐

  1. 《Effective C++》读书笔记 资源管理

    C++程序中最常用的资源包括动态分配的内存,文件描述器,互斥锁,数据库连接,网络socket等等.不论哪种资源,重要的是,当你不再使用他时,必须将他归还给系统. 一个很好的做法是以对象管理资源.把资源 ...

  2. yarn logs -applicationId命令java版本简单实现

    import java.io.DataInputStream; import java.io.EOFException; import java.io.FileNotFoundException; i ...

  3. leetcode7_C++整数反转

      给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 输出:  示例 2: 输入: - 输出: - 示例 3: 输入: 输出: 注意: 假设我们的环境只能存 ...

  4. FPGA学习-PS2接口

    选自http://m.elecfans.com/article/774143.html

  5. oracle数据库之游标的使用

    一.游标概念 为了处理 SQL 语句,ORACLE 必须分配一片叫上下文( context area )的区域来处理所必需的信息,其中包括要处理的行的数目,一个指向语句被分析以后的表示形式的指针以及查 ...

  6. python切片详解

    先从原理上分析切片运算: list的切片,内部是调用__getitem__,__setitem__,__delitem__和slice函数.而slice函数又是和range()函数相关的. 给切片传递 ...

  7. java线程安全— synchronized和volatile

    java线程安全— synchronized和volatile package threadsafe; public class TranditionalThreadSynchronized { pu ...

  8. TCP系列37—Keep Alive—1、TCP存活检测

    一.TCP存活(keepalive)检测的背景 对于TCP设计来说,如果一个客户端和服务器端建立连接后,不在进行数据传输,那么这个连接将会一直存在下去,理论上即使中间的路由器崩溃重启.或者中间的网络线 ...

  9. web前端之路 - 开篇

    一 web发展历程 了解事物的历史有助于我们渐进式的从发展的思路清楚了解事物的来龙去脉. 这里有一篇网文写得比较清晰和完整:https://www.tianmaying.com/tutorial/we ...

  10. s3c2440调试nandflash裸机程序遇到的问题

    图挂了可以去 https://github.com/tanghammer/mini2440_peripherals/blob/master/nand/debug_nand.md 按照前面sdram的代 ...