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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. Ubuntu nginx 配置404错误页面
  2. OkHttp和Volley对比
  3. Greenplum 数据库安装部署(生产环境)
  4. Hadoop 2.6.0 集群部署
  5. Linux 关闭防火墙命令
  6. 内存修改console
  7. docker中怎样设置开机启动--随容器的启动而启动服务?
  8. [Caffe]Win10+VS2015+CUDA8.0+cudnn5.1环境配置
  9. MapReduce-WordCount
  10. 利用iframe实现无刷新提交
  11. IntelliJ idea连接操作DB2数据库
  12. 学习windows编程 day5 之按键消息
  13. ZooKeeper系列(7):ZooKeeper一致性原理
  14. codeforces820B Mister B and Angle in Polygon 2017-06-28 09:42 123人阅读 评论(0) 收藏
  15. python input选择
  16. activiti 数据表设计
  17. Git Note - git tag
  18. 20155338 2016-2017-2 《Java程序设计》第九周学习总结
  19. 设计BBS
  20. URLRewrite地址重定向的实现

热门文章

  1. blob-照片转换与展示
  2. 积跬步,聚小流------java获取图片的尺寸
  3. php获取调用本方法的上个方法,php堆栈,函数入库
  4. chrome-vimium在markdown插件的页面失去效果
  5. [Git] How to rename your remote branch
  6. [Compose] 13. Lift into a Pointed Functor with of
  7. udp绑定信息
  8. UTC时间与当地时间的转换关系?
  9. 数据预处理(normalize、scale)
  10. [Angular] Dynamic component&#39;s instance and sorting