hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686
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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = ; int n,from,to;
struct node
{
int v,flow,cost;
int next;
}edge[M*];
int head[maxn],edgenum;
int dis[maxn],pre[maxn],pid[maxn],vis[maxn];
int an[][]; void add(int u,int v,int flow,int cost)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow;
edge[edgenum].cost=cost ;edge[edgenum].next=head[u];
head[u]=edgenum++; edge[edgenum].v=u ;edge[edgenum].flow=;
edge[edgenum].cost=-cost ;edge[edgenum].next=head[v];
head[v]=edgenum++;
} int spfa()
{
memset(vis,,sizeof(vis));
memset(dis,-,sizeof(dis));
queue<int> Q;
Q.push(from);
dis[from]=;
vis[from]=;
while (!Q.empty())
{
int u=Q.front() ;Q.pop();
vis[u]=;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (edge[i].flow> && dis[v]<dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=u;
pid[v]=i;
if (!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
return dis[to];
} int mincost()
{
int aug=,maxflow=;
int ans=;
int ncase=;
while ()
{
aug=inf;
int tmp=spfa();
if (tmp==) break;
for (int i=to ;i!=from ;i=pre[i])
{
if (edge[pid[i] ].flow<aug)
aug=edge[pid[i] ].flow;
}
for (int i=to ;i!=from ;i=pre[i])
{
edge[pid[i] ].flow -= aug;
edge[pid[i]^ ].flow += aug;
}
ans += tmp;
ncase++;
if (ncase==) break;
}
return ans-an[][]-an[n][n];
} int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=n ;j++)
scanf("%d",&an[i][j]);
}
from=;
to=*n*n;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=n ;j++)
{
int u=(i-)*n+j;
int v=(i-)*n+j+n*n;
add(u,v,,an[i][j]);
if (j+<=n) add(v,u+,,);
if (i+<=n) add(v,i*n+j,,);
}
}
add(from,from+n*n,,an[][]);
add(n*n,to,,an[n][n]);
int sum=mincost();
printf("%d\n",sum);
}
return ;
}
hdu 2686 Matrix 最小费用最大流的更多相关文章
- HDU 2686 Matrix(最大费用最大流+拆点)
题目链接:pid=2686">http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(由于有 ...
- hdu 4494 Teamwork 最小费用最大流
Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...
- HDU 4862 JUMP 最小费用最大流
2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定 ...
- HDU 2686 Matrix(最大费用流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 2686 Matrix 3376 Matrix Again(费用流)
HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...
- hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- UVa11082 Matrix Decompressing(最小费用最大流)
题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...
随机推荐
- Knockout.Js官网学习(event绑定、submit绑定)
event绑定 event绑定在DOM元素上添加指定的事件句柄以便元素被触发的时候执行定义的JavaScript 函数.大部分情况下是用在keypress,mouseover和mouseout上. 简 ...
- mr的logs的查看
在map或者reduce函数中使用System.out.println打印的信息沾满查看呢? 步骤1:启动history server /usr/local/hadoop-2.6.0/sbin/mr- ...
- form表单无刷新提交文件(iframe)
先看一段代码(PHP例子) 1.表单代码(form.html): <iframe name="testIframeName" style="display:none ...
- Laravel 5 基础(十一)- 表单验证
在建立一个文章的时候,如果你什么都不输入直接提交,ok,你获得了一个空的文章,没有任何错误提示,这是不对的.在命令行下运行 php artisan 可以看到一个选项 make:request,新建一个 ...
- BufferedInputSream和BufferedOutputSream,,,
package cd.itcast.bufferinputstream; import java.io.BufferedInputStream; import java.io.File; import ...
- Mongodb Gridfs
http://www.cnblogs.com/lipan/archive/2011/03/21/1989409.html
- 一些关于python的小感想
python是一门优秀的语言,但随之而来的是大量的知识,各种模块,相信一个人的大脑是很难记住如此多的内容.这时后的我们就应该想办法避免去记忆这么多的内容. 1.查看官方文档(英语很重要,啥也不说了) ...
- View和监听器
View的基本概念 View就是Activity当中显示出来的控件,用对象来表示,如文本框的TextView类,按钮的Button类等等 每一种控件都对应一个类,都属于View的子类 在Activit ...
- 统计工具之QQ图
正态 QQ 图和普通 QQ 图 分位数-分位数 (QQ) 图是两种分布的分位数相对彼此进行绘制的图.评估数据集是否正态分布,并分别研究两个数据集是否具有相似的分布. 如何构建正态 QQ 图 首先,数据 ...
- Mybatis关于like的字符串模糊处理
其中通过"%"#{key}"%"来拼接语句 <sql id="select_where"> from cellphone c l ...