HDU 2686 Matrix 3376 Matrix Again(费用流)
HDU 2686 Matrix
3376 Matrix Again
题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值
思路:拆点。建图,然后跑费用流就可以,只是HDU3376这题,极限情况是300W条边,然后卡时间过了2333
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std; const int MAXNODE = 600 * 600 * 2 + 5;
const int MAXEDGE = 4 * MAXNODE;
typedef int Type;
const Type INF = 0x3f3f3f3f; struct Edge {
int u, v;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
this->cost = cost;
}
}; struct MCFC {
int n, m, s, t;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
int inq[MAXNODE];
Type d[MAXNODE];
int p[MAXNODE];
Type a[MAXNODE]; void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
} void add_Edge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
} bool bellmanford(int s, int t, Type &flow, Type &cost) { for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = s; a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if (!inq[e.v]) {Q.push(e.v); inq[e.v] = true;}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].u;
}
return true;
} Type Mincost(int s, int t) {
Type flow = 0, cost = 0;
while (bellmanford(s, t, flow, cost));
return cost;
}
} gao; const int N = 600 * 600 + 5;
const int d[2][2] = {1, 0, 0, 1}; int n, num[N]; int get(int now, int k) {
int x = now / n;
int y = now % n;
x += d[k][0];
y += d[k][1];
if (x < 0 || x >= n || y < 0 || y >= n) return -1;
return x * n + y;
} int main() {
while (~scanf("%d", &n)) {
gao.init(n * n * 2);
for (int i = 0; i < n * n; i++) {
scanf("%d", &num[i]);
if (i == 0) gao.add_Edge(i, i + n * n, 2, -num[i]);
else if (i == n * n - 1) gao.add_Edge(i, i + n * n, 2, -num[i]);
else gao.add_Edge(i, i + n * n, 1, -num[i]);
}
for (int i = 0; i < n * n; i++) {
for (int j = 0; j < 2; j++) {
int next = get(i, j);
if (next < 0 || next >= n * n) continue;
gao.add_Edge(i + n * n, next, 2, 0);
}
}
printf("%d\n", -gao.Mincost(0, n * n * 2 - 1) - num[0] - num[n * n - 1]);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU 2686 Matrix 3376 Matrix Again(费用流)的更多相关文章
- HDU 5988 Coding Contest(浮点数费用流)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...
- HDU 4780 Candy Factory(拆点费用流)
Problem Description A new candy factory opens in pku-town. The factory import M machines to produc ...
- HDU 2686 (双线程) Matrix
这也是当初卡了很久的一道题 题意:从左上角的格子出发选一条路径到右上角然后再回到左上角,而且两条路径除了起点和终点不能有重合的点.问所经过的格子中的最大和是多少 状态设计:我们可以认为是从左上角出发了 ...
- HDU 4744 Starloop System(ZKW费用流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4744 题意:三维空间n个点,每个点有一个wi值.每对点的距离定义为floor(欧拉距离),每对点之间建 ...
- HDU 5644 King's Pliot【费用流】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 题意: 每天都有p[i]个飞行员进行阅兵,飞行员只工作一天. m个休假公式,花费tt[i]元让 ...
- hdu 1853(拆点判环+费用流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
- hdoj 3376,2686 Matrix Again 【最小费用最大流】
题目:hdoj 3376 Matrix Again 题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次仅仅能向右或者向下,出了末尾点其它仅仅能走一次,不能交叉,每次走到一个格子拿走这个格子中 ...
随机推荐
- [React] Create & Deploy a Universal React App using Zeit Next
In this lesson, we'll use next to create a universal React application with no configuration. We'll ...
- mysql去除字段内容的空格和换行回车
MySQL 去除字段中的换行和回车符 解决方法: UPDATE tablename SET field = REPLACE(REPLACE(field, CHAR(10), ''), ...
- Javascript 获取页面高度(多种浏览器)
//2015年8月13日11:00:50 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: d ...
- WeakRefence
http://183615215-qq-com.iteye.com/blog/1867568
- php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort)
php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort) 一.总结 核心是排序 ...
- ArcSDE 设置
---------------------转载----------------------- a)创建加载路径——st_shapelib.dll 执行创建库脚本:create or r ...
- 简单sql部分强化练习题
简单查询部分sql练习题 -- 选择部门30中的全部职工 select * from emp where deptno = 30; -- 列出全部业务员(CLERK)的姓名,编号,和部门编号 sele ...
- CF337D Book of Evil - 树型dp
传送门 题目大意: 一棵树上有一个特殊点,特殊点可以影响距离小于等于d的点,现在告诉被影响的点,问特殊点可以在几个点上. 题目分析: 对题意进行转化:求到被影响点的最大距离小于等于d的点数目. 然后就 ...
- Qt写入unicode编码格式的文本(用QChar写入BOM标记,并且列出所有Qt支持的字符集)
1.文本流设置unicode小端模式 2.写入文本前两个字节FF FE 3.字符串转成unicode编码 QList<QByteArray> list = QTextCodec::avai ...
- Read-Copy Update Implementation For Non-Cache-Coherent Systems
A technique for implementing read-copy update in a shared-memory computing system having two or more ...