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费用流在稠密图跑得快,普 ...
随机推荐
- Java Web开发框架Spring+Hibernate整合效果介绍(附源码)(已过期,有更好的)
最近花了一些时间整合了一个SpringMVC+springAOP+spring security+Hibernate的一套框架,之前只专注于.NET的软件架构设计,并没有接触过Java EE,好在有经 ...
- LeetCode 96——不同的二叉搜索树
1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...
- 怎么给kibana加上权限?
更新:2016-05-20 09:36 通过向Elastic了解,他们目前开发的5.0版本支持更好的权限定制,粒度达到字段级别.他们预计今年就可以上线,如果你不是有一个旧版本的kibanba非要维护不 ...
- Check the string
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend ...
- Where to go from here
Did you get through all of that content? Congratulations! You've learnt the fundamentals of algorith ...
- 修改IP的批处理
昨天遇到一个客户,说是抢火车票来着,用了3个公网IP,要求在抢票前15分钟换次IP(看我这毛病,废话多了,正题) 系统是2003 32位的 因为自己不懂脚本,网上找了个修改了下,就有了下面的脚本: 首 ...
- 异常--throw
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Python35 升级 pip
使用pip安装插件的时候报错: You are using pip version 8.1.1, however version 9.0.1 is available.You should consi ...
- 进程&线程间通信方式总结
一.进程间的通信方式 # 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系通常是指父子进程关系. # 有名管道 (namedpip ...
- [Leetcode] 1.Two Sum(unordered_map)
1.首先想到的方法就是两个for循环全部遍历,代码如下,可通过,但效率太低 class Solution { public: vector<int> twoSum(vector<in ...