Matrix Again

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4255    Accepted Submission(s): 1233

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
 代码:
//和HDU2686一样,只是数据变大了,上一个模板会超内存,这个板不会。
/***********************最小费用最大流模板2*************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=**+;
const int maxm=*maxn;//!边数要够
const int inf=0x7fffffff;
struct Edge
{
int to,next,cap,flow,cost;
}edges[maxm];
int head[maxn],tol,pre[maxn],dis[maxn];
bool vis[maxn];
int N;
void init(int n)
{
N=n;
tol=;
memset(head,-,sizeof(head));
}
void AddEdge(int u,int v,int cap,int cost)
{
edges[tol].to=v;
edges[tol].cap=cap;
edges[tol].cost=cost;
edges[tol].flow=;
edges[tol].next=head[u];
head[u]=tol++;
edges[tol].to=u;
edges[tol].cap=;
edges[tol].cost=-cost;
edges[tol].flow=;
edges[tol].next=head[v];
head[v]=tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i=;i<=N;i++){
dis[i]=inf;
vis[i]=;
pre[i]=-;
}
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edges[i].next){
int v=edges[i].to;
if(edges[i].cap>edges[i].flow&&dis[v]>dis[u]+edges[i].cost){
dis[v]=dis[u]+edges[i].cost;
pre[v]=i;
if(!vis[v]) {vis[v]=;q.push(v);}
}
}
}
if(pre[t]==-) return ;
return ;
}
int MinCostFlow(int s,int t)
{
int flow=,cost=;
while(spfa(s,t)){
int Min=inf;
for(int i=pre[t];i!=-;i=pre[edges[i^].to])
Min=min(Min,edges[i].cap-edges[i].flow);
for(int i=pre[t];i!=-;i=pre[edges[i^].to]){
edges[i].flow+=Min;
edges[i^].flow-=Min;
cost+=edges[i].cost*Min;
}
flow+=Min;
}
return cost;//返回最小费用,flow存最大流
}
/*********************************************************************/
int main()
{
int n,mp[][];
while(scanf("%d",&n)==){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) scanf("%d",&mp[i][j]);
int s=,t=n*n*+;
init(n*n*+);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int id=(i-)*n+j;
if(id==){
AddEdge(id,id+n*n,,-mp[i][j]);
AddEdge(s,id,,);
}
else if(id==n*n){
AddEdge(id,id+n*n,,-mp[i][j]);
AddEdge(id+n*n,t,,);
}
else AddEdge(id,id+n*n,,-mp[i][j]);
if(i<n) AddEdge(id+n*n,id+n,,);
if(j<n) AddEdge(id+n*n,id+,,);
}
}
int ans=-(MinCostFlow(s,t)+mp[][]+mp[n][n]);
printf("%d\n",ans);
}
return ;
}

HDU3376 最小费用最大流 模板2的更多相关文章

  1. 图论算法-最小费用最大流模板【EK;Dinic】

    图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...

  2. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

  3. 最大流 && 最小费用最大流模板

    模板从  这里   搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...

  4. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

  5. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  6. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

  7. POJ2135 最小费用最大流模板题

    练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...

  8. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  9. 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]

    题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. python作业:三级菜单(第一周)

    一.作业需求: 1. 运行程序输出第一级菜单 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 3. 菜单数据保存在文件中 4. 让用户选择是否要退出 5. 有返回上一级菜单的功能 二.三级菜单 ...

  2. java poi技术读取到数据库

    https://www.cnblogs.com/hongten/p/java_poi_excel.html java的poi技术读取Excel数据到MySQL 这篇blog是介绍java中的poi技术 ...

  3. PHP 将一个字符串部分字符用$re替代隐藏

    <?php/** * 将一个字符串部分字符用$re替代隐藏 * @param string $string 待处理的字符串 * @param int $start 规定在字符串的何处开始, * ...

  4. 3dContactPointAnnotationTool开发日志(十三)

      为了使生成的项目能够显示报错信息我又勾选了下面这几个选项:   然后生成的项目运行时可以显示错误信息了,貌似是shader是空的.   之前的代码是这么写的,调用了Shader.Find(),貌似 ...

  5. 使用WCF上传数据

    通过传递Stream对象来传递大数据文件,但是有一些限制: 1.只有 BasicHttpBinding.NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据. 2. ...

  6. Python2中编码错误---重组人表皮生长因子凝胶(易孚格式转化为UTF-8

    在python2的使用中,总会遇到各种各样的编码问题,这也是使用Python2最头疼的一件事情,幸好python3解决了编码的问题. 下面我在爬虫时遇到的类似重组人表皮生长 ...

  7. [socket编程] 一个服务器与多个客户端之间通信

    转自:http://blog.csdn.net/neicole/article/details/7539444 并加以改进 Server程序: // OneServerMain.cpp #includ ...

  8. MyBatis配置和日志

    MyBatis最关键的组成部分是SqlSessionFactory,我们可以从中获取SqlSession,并执行映射的SQL语句.SqlSessionFactory对象可以通过基于XML的配置信息或者 ...

  9. 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  10. Python 日志输出中添加上下文信息

    Python日志输出中添加上下文信息 除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息.比如,在一个网络应用中,可能希望在日志中记录客户端的特定信息,如: ...