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的弱化版本吧..建图的关键在于: 把行.列看 ...
随机推荐
- C# Datetime类常用技巧
C#类常用技巧 //今天DateTime.Now.Date.ToShortDateString();//昨天,也就是今天的日期减一DateTime.Now.AddDays(-1).ToShortDat ...
- web.xml中的url-pattern映射规则
Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单.servlet和filter中的url-pattern有一些文章在里面的,总结了一些东西,以免遇到问题又要浪费时间. 一,s ...
- hbase与Hadoop
Hbase是一种低延迟的在线系统,Hadoop是优化吞吐量的离线系统.这种互补关系造就了一种强大的.灵活的数据平台,可以用来搭建水平扩展的数据应用.
- 服务器下自动备份MySQL
Linux下 service crond restart */ * * * * /home/mysqlbackup.sh >/home/runssh.log backup.sh #bin/bas ...
- C++多态性的浅析
多态性是C++的一个重要特性,[不扯淡直接进入正题] 灵活运用多态,首先得知道类之间的继承. 当B继承了A类后,一般都是公有继承. B的实例化对象的内存空间结构若是了解 就可以合理利用多态了. A ...
- PopupWindow的简单使用
测试代码: package com.zzw.testpopuwindows; import android.app.Activity; import android.graphics.Color; i ...
- div+css的优势在哪?
1.符合W3C标准.微软等公司都是他的支持者. 2.所搜引擎更加友好. 3.样式调整更加方便. 4.css简洁的代码,减少了带宽. 5.表现和结构分离.在团队开发中更容易分工 并不是取代table,t ...
- PropertyGrid 控件使用方法
编写一个对象,后面传递给 PropertyGrid 来显示: using System; using System.Collections.Generic; using System.Linq; us ...
- 第十八章 数据访问(In .net4.5) 之 I/O操作
1. 概述 本章内容包括 文件操作.流操作.读写网络数据 以及 异步I/O操作. 2. 主要内容 2.1 文件操作 ① 使用 Drive 和 DriveInfo 访问磁盘信息. DriveInfo[] ...
- ios第三方分享到qq、微信、人人网、微博总结
我们开发出来的APP通常要通过第三方分享到其他社交平台,如qq.微博微信 等.通过分享可以提高APP的传播效率,增加APP的曝光率,因此也算是APP功能 里的标配了吧.目前常用的第三方分享途径有qq. ...