国庆八天乐,刷题也快乐。

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最小费用流的更多相关文章

  1. hoj 2715 (费用流 拆点)

    http://acm.hit.edu.cn/hoj/problem/view?id=2715 将每个格子 i 拆成两个点 i’, i’’并加边(i’, i’’, 1, -Vi), (i’, i’’, ...

  2. HOJ - 2543最小费用流

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2543 这个题目挺有意思. 自己扣了一会儿,发现图挺好建,就把(u,v,f,w) 拆成(u,v,f,0) ...

  3. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  4. HIT 2715 - Matrix3 - [最小费用最大流][数组模拟邻接表MCMF模板]

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2715 Time limit : 5 sec Memory limit : 64 M Zhouguyue ...

  5. hoj 2662 经典状压dp // MyFirst 状压dp

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2662 1.引言:用dp解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态. ...

  6. HOJ 1797 Red and Black

    传送门  http://acm.hit.edu.cn/hoj/problem/view?id=1797 总体的思路是遍历可以到达的' . ',将其对应的vis数组化为1,然后统计所有为1的vis项; ...

  7. POJ2195 最小费用流

    题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...

  8. HDU 4067 hdoj 4067 Random Maze 最小费用流

    给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...

  9. HOJ 1001: A+B; 1002: A+B+C

    两道水题,用来熟悉 HOJ 的提交系统. 1001:输入两个整数 A, B (0 <= A,B <= 10),输出 A+B. #include <iostream> using ...

随机推荐

  1. python--字符编码理解

    一.字符编码简史: 美国:1963年 ASCII (包含127个字符  占1个字节) 中国:1980年 GB2312 (收录7445个汉字,包括6763个汉字和682个其它符号) 1993年 GB13 ...

  2. python入门:输出1-100之内的所有奇数和偶数

    #!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-100之内的所有奇数和偶数 """ 给start赋值等于1,while ...

  3. python有三元运算符吗

    所属网站分类: python基础 > 语法,变量,运算符 作者:goodbody 链接: http://www.pythonheidong.com/blog/article/12/ 来源:pyt ...

  4. SQL前后端分页

    /class Page<T> package com.neusoft.bean; import java.util.List; public class Page<T> { p ...

  5. LeetCode(224) Basic Calculator

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string may co ...

  6. Centos7 安装 OwnCloud 私有云

    OwnCloud 一款文件主机服务软件,就是我们平时使用的云存储,不过这是在自己主机的服务器上建立属于自己的私有云,OwnCloud 使用AGPLv3协议发布.本项目是基于PHP和SQLite,MyS ...

  7. matlab画图颜色设置

    各种颜色属性选项选项意义选项意义'r' 红色 'm' 粉红'g' 绿色 'c' 青色'b' 兰色 'w' 白色'y' 黄色 'k' 黑色各种线型属性选项选项意义选项意义'-' 实线 '--' 虚线': ...

  8. 一个通用的分页存储过程实现-SqlServer(附上sql源码,一键执行即刻搭建运行环境)

    使用前提 查询表必须有ID字段,且该字段不能重复,建议为自增主键 背景 如果使用ADO.NET进行开发,在查询分页数据的时候一般都是使用分页存储过程来实现的,本文提供一种通用的分页存储过程,只需要传入 ...

  9. Pycharm 简单设置

  10. 九度oj 题目1391:顺时针打印矩阵

    题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2 ...