HOJ - 2715最小费用流
国庆八天乐,刷题也快乐。
HOJ崩了,但是VJ可以把题目挂出来。
题目链接:https://vjudge.net/contest/188441#problem/A
涉及到矩阵里面的网络流,化为图来做。
某个点有流量限制,一定要想到拆点。
求最大值的话,要把w变成负数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 6e3;
const int INF = 1e9;
int vis[maxn],dist[maxn];
int tot,head[maxn];
int pv[maxn],pe[maxn];
struct edge
{
int to,pre,cap,cost;
}e[];
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
void add(int from,int to,int cap,int cost)
{
e[tot].pre = head[from];
e[tot].to = to;
e[tot].cap = cap;
e[tot].cost = cost;
head[from] = tot++;
}
void addedge(int from,int to,int cap,int cost)
{
add(from,to,cap,cost);
add(to,from,,-cost);
}
int n,k;
int min_cost_flow(int s,int t,int f,int& max_flow)
{
int ret = ;
while(f>&&(k--))
{
memset(vis,,sizeof(vis));
for(int i=;i<maxn;i++) dist[i] = INF;
dist[s] = ;
queue<int> q;
q.push(s);
while(!q.empty())
{
int v = q.front(); q.pop();
vis[v] = ;
for(int i=head[v];i>=;i=e[i].pre)
{
int to = e[i].to,cap = e[i].cap,cost = e[i].cost;
if(cap>&&dist[to]>dist[v]+cost)
{
pv[to] = v,pe[to] = i;
dist[to] = dist[v] + cost;
if(!vis[to]) q.push(to);
vis[to] = ;
}
}
// printf("%d\n",v);
}
//printf("%d\n",dist[t]);
if(dist[t]==INF) return ret;///同一目的地,每次增广路都是最小费用
///当所有边的流量都流净后,即没有残余网络,返回。
int d = f;
for(int v=t;v!=s;v=pv[v])
{
d = min(d,e[pe[v]].cap);
}
f -= d;
max_flow += d;
ret += d*dist[t]; ///走一单位就消耗dist[t]
for(int v=t;v!=s;v=pv[v])
{
e[pe[v]].cap -= d;
e[pe[v]^].cap += d;
}
}
return ret;
}
int height[][];
int value[][];
int judge(int x,int y)
{
if(x<=||x>=(n-)||y<=||y>=(n-)) return ;
else return ;
}
int judge1(int x,int y)
{
if(x>=&&x<=(n-)&&y>=&&y<=(n-)) return ;
else return ;
}
int to[][] = {,,-,,,,,-};
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&k);
init(); ///千万别忘记写
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&value[i][j]);
}
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&height[i][j]);
}
}
int s = *n*n+;
int t = *n*n+;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
addedge(s,i*n+j,INF,);
addedge(i*n+j,i*n+j+n*n,,-value[i][j]);
addedge(i*n+j,i*n+j+n*n,INF,); ///addedge(i',i'',inf,0)的边,可以再次走,只不过没有宝藏了
if(judge(i,j))
{
addedge(i*n+j+n*n,t,INF,);
}
}
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
for(int k=;k<;k++)
{
int nextx = i+to[k][];
int nexty = j+to[k][];
if(judge1(nextx,nexty)&&height[i][j]>height[nextx][nexty])
{
addedge(i*n+j+n*n,nextx*n+nexty,INF,);
}
}
}
}
int max_flow = ;
int ans = min_cost_flow(s,t,INF,max_flow);
if(ans==)
{
printf("%d\n",ans);
}
else
{
printf("%d\n",-ans);
}
}
return ;
}
/*
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 1
*/
HOJ - 2715最小费用流的更多相关文章
- hoj 2715 (费用流 拆点)
http://acm.hit.edu.cn/hoj/problem/view?id=2715 将每个格子 i 拆成两个点 i’, i’’并加边(i’, i’’, 1, -Vi), (i’, i’’, ...
- HOJ - 2543最小费用流
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2543 这个题目挺有意思. 自己扣了一会儿,发现图挺好建,就把(u,v,f,w) 拆成(u,v,f,0) ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- HIT 2715 - Matrix3 - [最小费用最大流][数组模拟邻接表MCMF模板]
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2715 Time limit : 5 sec Memory limit : 64 M Zhouguyue ...
- hoj 2662 经典状压dp // MyFirst 状压dp
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2662 1.引言:用dp解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态. ...
- HOJ 1797 Red and Black
传送门 http://acm.hit.edu.cn/hoj/problem/view?id=1797 总体的思路是遍历可以到达的' . ',将其对应的vis数组化为1,然后统计所有为1的vis项; ...
- POJ2195 最小费用流
题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
- HOJ 1001: A+B; 1002: A+B+C
两道水题,用来熟悉 HOJ 的提交系统. 1001:输入两个整数 A, B (0 <= A,B <= 10),输出 A+B. #include <iostream> using ...
随机推荐
- H5(一)H5与HTML、XHTML的不同
一.基本概念 html:超文本标记语言 (Hyper Text Markup Language) xhtml:可扩展超文本标记语言,是一种置标语言,表现方式与超文本标记语言(HTML)类似,不过语法上 ...
- Java 编辑html模板并生成pdf
1.工具类 import com.hujiang.project.zhgd.Util; import com.itextpdf.text.BaseColor; import com.itextpdf. ...
- makedown语法
文章转载至:https://blog.csdn.net/u014061630/article/details/81359144#1-%E5%BF%AB%E6%8D%B7%E9%94%AE 前言 写过博 ...
- 掌握这些Python代码技巧,编程至少快一半!
被人工智能捧红的 Python 已是一种发展完善且非常多样化的语言,其中肯定有一些你尚未发现的功能.本文或许能够让你学到一些新技巧. Python 是世界上最流行.热门的编程语言之一,原因很多,比 ...
- 【HIHOCODER 1142】 三分·三分求极值
描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d. 输入 第1行:5个整数a,b,c,x,y.前三个数构成抛物 ...
- HDU - 5884 Sort (二分答案+贪心)
有n个数字,你需要把这n个数字合成一个数字,每次只能把k个数字合并成一个,花费为这k个数字的和. 给一个最大花费,问不超过这个最大花费的情况下,k的最小值. Sample Input 1 5 25 1 ...
- 最长回文子串——manacher
最长回文子串--Manacher 算法 (原版的博主的代码都是用py写的,这里改成c++) c++ 算法 字符串处理 0. 问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一 ...
- 虚拟机上的Linux学习
title: 虚拟机上的Linux学习 date: 2018-08-08 15:48:28 updated: tags: [Linux,学习笔记] description: keywords: com ...
- Mantis安装与配置
什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...
- javascript 内置日期转换方法
var d = new Date(); console.log(d); // 输出:Mon Nov 04 2013 21:50:33 GMT+0800 (中国标准时间) console.log(d.t ...