HDU2686 费用流 模板
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2485 Accepted Submission(s): 1314
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.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
//费用流模板题,因为题目中的表述就是一个网络流模型,拆点建图,没点只经过一次,起点终点容量为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 费用流 模板的更多相关文章
- HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解
题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...
- 初识费用流 模板(spfa+slf优化) 餐巾计划问题
今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...
- hdu1533 费用流模板
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 费用流模板(带权二分图匹配)——hdu1533
/* 带权二分图匹配 用费用流求,增加源点s 和 汇点t */ #include<bits/stdc++.h> using namespace std; #define maxn 1000 ...
- 算法复习——费用流模板(poj2135)
题目: Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16898 Accepted: 6543 De ...
- Spfa费用流模板
; ,maxm=; ,fir[maxn],nxt[maxm],to[maxm]; int cap[maxm],val[maxm],dis[maxn],path[maxn]; void add(int ...
- zkw费用流模板
理论:http://www.cnblogs.com/acha/p/6735037.html #include<cstdio> #include<cstring> #includ ...
- 【费用流】【Next Array】费用流模板(spfa版)
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...
- 费用流 ZOJ 3933 Team Formation
题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...
随机推荐
- yun rpm
RPM:RedHat Package Manager的简称,是一种数据库记录的方式的管理机制.当需要安装的软件的依赖软件都已经安装,则继续安装,否则不予安装. 特点:1.已经编译并打包完成2.软件的信 ...
- 剑指offer-顺时针打印矩阵19
题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数 ...
- Cassandra 类型转换限制
原文地址:http://stackoverflow.com/questions/31880381/cassandra-alter-column-type-which-types-are-compati ...
- 【转载】android 常用开源框架
对于Android初学者以及对于我们菜鸟,这些大神们开发的轻量级框架非常有用(更别说开源的了). 下面转载这10个框架的介绍:(按顺序来吧没有什么排名). 一. Afinal 官方介绍: Afina ...
- Notes of the scrum meeting before publishing2(12.18)
meeting time:18:30~20:30p.m.,December 18th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- 【转】c++面试基础
1,关于动态申请内存 答:内存分配方式三种: (1)从静态存储区域分配:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在. 全局变量,static变量. (2)在栈上创建:在执行函 ...
- android BadgeView的使用(图片上的文字提醒)
BadgeView主要是继承了TextView,所以实际上就是一个TextView,底层放了一个label,可以自定义背景图,自定义背景颜色,是否显示,显示进入的动画效果以及显示的位置等等: 这是Gi ...
- java — 排序算法
1.冒泡排序 比较相邻元素,如果第一个比第二个大,就交换位置,每一次交换,当前 package BubbleSort; public class Test { public static void m ...
- 关于如何解决PHPCMS V9内容搜索显示不全问题解决方案
站长朋友们都晓得只要是开源的PHP程序都会有漏洞存在.如果想完美的建站就需要自己去研究打补丁了.最近很多站长联系小编咨询用phpcms建站当在首页搜索内容的时候有的居然搜索不到.小编感到很是奇怪于是就 ...
- AutoResetEvent的基本用法
The following example uses an AutoResetEvent to synchronize the activities of two threads.The first ...