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. image-base64互转

    package base64StringToImage; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStrea ...

  2. Ubuntu12.04LTS SDK无法更新

    1.打开终端输入sudo gedit /etc/hosts 加入下面 2.加入下列文字到文件里:       203.208.46.146 dl.google.com       203.208.46 ...

  3. Android 基于Bmob平台数据管理常用方法整理

    最近想搞一下基于Bmob平台的应用开发,发现确实挺方便的,很好的解决了服务器后台部署的难题, 但是也有一些弊端,数据架构的可扩展性不强,做一些数据结构简单的应用还是可以的. package com.b ...

  4. mount新磁盘

    fdisk -l  mkfs.xfs /dev/xvdb  mkdir /data mount /dev/xvdb /data df -h vi /etc/fstab /dev/xvdb /data ...

  5. [Angular] NgRx/effect, why to use it?

    See the current implementaion of code, we have a smart component, and inside the smart component we ...

  6. 【codeforces 754A】Lesha and array splitting

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. js进阶 9 js操作表单知识点总结

    js进阶 9 js操作表单知识点总结 一.总结 一句话总结:熟记较常用的知识点,对于一些不太常用的知识点可以在使用的时候查阅相关资料,在使用和练习中去记忆. 1.表单中学到的元素的两个对象集合石什么? ...

  8. HTML5在客户端存储数据的新方法——localStorage

    HTML5在客户端存储数据的新方法--localStorage localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中,而客户端一般是指上 ...

  9. python request 代理/超时/证书

    import requests headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) A ...

  10. ANT下载与安装--windows

    原文:ANT下载与安装--windows 1.下载地址 http://ant.apache.org/bindownload.cgi: 2.版本信息 1.10.2 .zip archive  对应jdk ...