hdoj 3376,2686 Matrix Again 【最小费用最大流】
题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次仅仅能向右或者向下,出了末尾点其它仅仅能走一次,不能交叉,每次走到一个格子拿走这个格子中的数字,求价值最大?
分析:非常明显费用流。開始想的到一种建图方案,可是那样的话流量全为负值的话会成一个环,所以果断换了。
建图方案是:
首先拆点,每一个点拆成两个i 和 ii ,建边,费用为当前格子的值,流量为1,初始点和末尾点为2
然后每一个点向它的右边和下边分别建边,容量为1,费用为0
s 连接 左上角 流量 2 ,费用 0
右下角连接 t 。流量为 2 。费用为 0
PS:这个题目居然卡vector,的模拟自己实现邻接表,否则的话会超内存。
ac代码:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std; #define PB push_back
#define MP make_pair
#define Del(a,b) memset(a,b,sizeof(a)) typedef vector<int> VI;
typedef long long LL;
const LL inf = 0x3f3f3f3f;
const int N = 750000;
int cost,flow;
struct Node
{
int from,to,cap,flow,cost;
int next;
}e[N<<2];
int head[N],top;
void add_Node(int from,int to,int cap,int cost)
{
e[top] = ((Node){from,to,cap,0,cost,head[from]});
head[from] = top++;
e[top] = ((Node){to,from,0,0,-cost,head[to]});
head[to] = top++;
}
int vis[N],dis[N];
int father[N],pos[N];
bool BellManford(int s,int t,int& flow,int& cost)
{
Del(dis,inf);
Del(vis,0);
queue<int> q;
q.push(s);
vis[s]=1;
father[s]=-1;
dis[s] = 0;
pos[s] = inf;
while(!q.empty())
{
int f = q.front();
q.pop();
vis[f] = 0;
for(int i = head[f];i!=-1 ; i = e[i].next)
{
Node& tmp = e[i];
if(tmp.cap>tmp.flow && dis[tmp.to] > dis[f] + tmp.cost)
{
dis[tmp.to] = dis[f] + tmp.cost;
father[tmp.to] = i;
pos[tmp.to] = min(pos[f],tmp.cap - tmp.flow);
if(vis[tmp.to] == 0)
{
vis[tmp.to]=1;
q.push(tmp.to);
}
}
} }
if(dis[t] == inf)
return false;
flow += pos[t];
cost += dis[t]*pos[t];
for(int u = t; u!=s ; u = e[father[u]].from)
{
e[father[u]].flow += pos[t];
e[father[u]^1].flow -= pos[t];
}
return true;
}
int Mincost(int s,int t)
{
flow = 0, cost = 0;
while(BellManford(s,t,flow,cost));
return cost;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
Del(head,-1);top = 0;
int num = n*n;
int one,x;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
scanf("%d",&x);
if(i==0 && j==0)
one = x;
int tmp = 1;
if(i==0 && j==0 || i==n-1 && j == n-1)
tmp = 2;
add_Node(i*n+j,i*n+j+num,tmp,-x);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if((j+1)<n)
add_Node(i*n+j+num,i*n+j+1,1,0);
if((i+1)<n)
add_Node(i*n+j+num,(i+1)*n+j,1,0);
}
}
int s = 2*num+1 , t = s + 1;
add_Node(s,0,2,0);
add_Node(num+num-1,t,2,0);
int ans = Mincost(s,t);
printf("%d\n",-ans-x-one);
}
return 0;
}
hdoj 3376,2686 Matrix Again 【最小费用最大流】的更多相关文章
- hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdoj 1533 Going Home 【最小费用最大流】【KM入门题】
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- [poj] 3422 Kaka's Matrix Travels || 最小费用最大流
原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...
- uva12534 Binary Matrix 2(最小费用最大流)
http://blog.csdn.net/qq564690377/article/details/17082055 做的时候觉得明显是费用流,但是真的不知道怎么建图,看了上面的博客会稍微清晰一点.后面 ...
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
- hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- poj3422 Kaka's Matrix Travels(最小费用最大流问题)
/* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...
- UVa11082 Matrix Decompressing(最小费用最大流)
题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...
- POJ 3422 Kaka's Matrix Travels (最小费用最大流)
POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...
随机推荐
- 用pycharm运行django项目
[点击]run -> Edit Configrations 弹出如下页面 点击“+” 点击Django server 在弹出页面的host填0.0.0.0 点击这个“文件夹” 点击‘+’后填下面 ...
- idea 编辑器 光标问题!(insert键)
今天写代码不小心按了键盘的insert键,光标莫名闪退了 ,重新打开的时候发现 光标变成了 按了insert 的效果 ,简直无语的要命啊! 这敲代码太恶心了!怒搜资料 结果找到了解决办法! 1.打 ...
- md5如何实现encodePassword加密方法
后台的加密代码:用户名:zhangsan 密码:123 /** * 编译密码,即加密 * @param user 用户信息 * @param password 密码 * @return 返回值为加 ...
- Jquery学习总结(4)——高效Web开发的10个jQuery代码片段
在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...
- jvm 虚拟机参数_新生代内存分配
1.参数 -Xmn 设置新生代的大小,设置一个比较大的新生代会减少老年代的大小,这个参数对系统性能以及 GC 行为影响很大,新生代大小一般设置为真个堆内存的1/3到1/4 -XX:SurvivorRa ...
- HDU——T 2119 Matrix
http://acm.hdu.edu.cn/showproblem.php?pid=2119 Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- leetcode第一刷_Add Binary
二进制相加,本质上就是大整数加法,有关大整数加法我的舍友教过我一个非常好的方法,先用一个int数组保存结果,将两个数相应位置相加,所有加完后.再统一处理进位的问题.这种方法相同适用于大整数的乘法. 这 ...
- iOS7实现带文本输入框的UIAlertView及获取TextField文本内容
if (customAlertView==nil) { customAlertView = [[UIAlertView alloc] initWithTitle:@"自定义服务器地址&quo ...
- Linux就该这么学 20181003(第四章Vim/shell/测试条件)
参考链接https://www.linuxprobe.com/ vim文本编辑器 命令模式:控制光标移动,可对文本进行复制,黏贴,删除和查找工作 输入模式:正常的文本录入 末行模式:保存或退出文档,以 ...
- [jzoj 6084] [GDOI2019模拟2019.3.25] 礼物 [luogu 4916] 魔力环 解题报告(莫比乌斯反演+生成函数)
题目链接: https://jzoj.net/senior/#main/show/6084 https://www.luogu.org/problemnew/show/P4916 题目: 题解: 注: ...