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

  1. HDU 5988 Coding Contest(浮点数费用流)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...

  2. HDU 4780 Candy Factory(拆点费用流)

    Problem Description   A new candy factory opens in pku-town. The factory import M machines to produc ...

  3. HDU 2686 (双线程) Matrix

    这也是当初卡了很久的一道题 题意:从左上角的格子出发选一条路径到右上角然后再回到左上角,而且两条路径除了起点和终点不能有重合的点.问所经过的格子中的最大和是多少 状态设计:我们可以认为是从左上角出发了 ...

  4. HDU 4744 Starloop System(ZKW费用流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4744 题意:三维空间n个点,每个点有一个wi值.每对点的距离定义为floor(欧拉距离),每对点之间建 ...

  5. HDU 5644 King's Pliot【费用流】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 题意: 每天都有p[i]个飞行员进行阅兵,飞行员只工作一天. m个休假公式,花费tt[i]元让 ...

  6. hdu 1853(拆点判环+费用流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  7. POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

    累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...

  8. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  9. hdoj 3376,2686 Matrix Again 【最小费用最大流】

    题目:hdoj 3376 Matrix Again 题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次仅仅能向右或者向下,出了末尾点其它仅仅能走一次,不能交叉,每次走到一个格子拿走这个格子中 ...

随机推荐

  1. python 标准库 —— io(StringIO)

    0. io流(io stream) 流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数据的操作 ...

  2. Android 节日短信送祝福(功能篇:2-短信历史记录Fragment的编写)

    因为用于展示短信记录的是一个ListView,但是为了方便,可以直接继承自ListFragment,就可以免去写ListView对应的布局了,只需要写其item对应的布局即可. item_sended ...

  3. [NPM] Use package.json variables in npm scripts

    In this lesson we will show that you can leverage values that you already have provided in your pack ...

  4. QQ欢乐豆斗地主心得体会(四):合谋赢豆

    刚刚又在玩QQ欢乐斗地主,只可惜,这次不够欢乐. 本金,300万豆,运气比较好,赢到将近400万.突然,形势急转直下,一直输,一直到180多万豆.本来这一局,有硬炸在手,但是没有癞子,基本被吊打的节奏 ...

  5. echarts多条折线图和柱状图实现

    参考链接:echarts官网:http://echarts.baidu.com/原型图(效果图): 图片.png 代码: <!DOCTYPE html> <html> < ...

  6. Lua转让C功能

    在上一篇文章中(C调用lua函数)中.讲述了怎样用c语言调用lua函数,通常,A语言能调用B语言,反过来也是成立的.正如Java 与c语言之间使用JNI来互调.Lua与C也能够互调. 当lua调用c函 ...

  7. 商(quotient)—— 两数之比

    1. IQ:Intelligence quotient IQ=MACA×100 MA:心智年龄(mental age) CA:实足年龄(chronological age)

  8. XSS攻击之基础篇:HTML标签与字符串的渲染

    <body> XSS攻击之基础篇:HTML标签与字符串的渲染 测试 <div class="a1"> </div> <div class= ...

  9. C# 直接创建多个类和使用反射创建类的性能

    原文:C# 直接创建多个类和使用反射创建类的性能 本文告诉大家我对比的使用直接创建多个类和使用反射创建多个类的性能 在上一篇 C# 程序内的类数量对程序启动的影响 的基础上,继续做实验 现在创建 10 ...

  10. Windows下MinGW跨平台编译和使用log4cpp

    Log4cpp 是C++开源日志库,为 C++ 应用程序开发中提供了日志的追踪和调试功能,基于 LGPL 开源协议,移植自 java 的日志项目 log4j, 并在 api 上保持了一致性. 1. 环 ...