POJ 3422 Kaka's Matrix Travels 费用流
来源:互联网 发布:visual basic编程手机 编辑:程序博客网 时间:2024/06/05 07:55
题目:
http://poj.org/problem?id=3422
题意:
有一个n*n的矩阵,矩阵中每个格子都有一个数字,代表当前格子的财富值,现在如下操作:从左上角开始出发,然后只能往下或者往右走,走到一个格子可以取走其中的财富值,直到走到右下角。现在可以如此操作k次,其中一个格子的财富值只能取一次,问操作k次后得到的最大财富值。
思路:
首先把格子中的值取反。把每个格子拆成两个点,设为i和i+N,那么连两条边,第一条边容量为1费用为格子的财富值,意味格子的财富值只能取一次,第二条边容量为k费用为0,意味着取走当前格子的财富值后,再经过当前格子时不会得到财富值,容量为k意味着能走k次,满足题意要求,然后源点和第一个格子连边,最后一个格子和汇点连边,跑出结果取反即可
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#include <cmath>#include <queue>using namespace std;typedef long long ll;const int N = 5000 + 10, M = 50 + 10;const int INF = 0x3f3f3f3f;struct edge{ int st, to, cap, cost, next;} g[N*1000];int cas;int cnt, head[N];int dis[N], pre[N];bool vis[N];void add_edge(int v, int u, int cap, int cost){ g[cnt].st = v, g[cnt].to = u, g[cnt].cap = cap, g[cnt].cost = cost, g[cnt].next = head[v], head[v] = cnt++; g[cnt].st = u, g[cnt].to = v, g[cnt].cap = 0, g[cnt].cost = -cost, g[cnt].next = head[u], head[u] = cnt++;}void spfa(int s, int t){ memset(dis, 0x3f, sizeof dis); memset(vis, 0, sizeof vis); memset(pre, -1, sizeof pre); queue<int> que; que.push(s); dis[s] = 0, vis[s] = true; while(! que.empty()) { int v = que.front(); que.pop(); vis[v] = false; for(int i = head[v]; i != -1; i = g[i].next) { int u = g[i].to; if(g[i].cap > 0 && dis[u] > dis[v] + g[i].cost) { dis[u] = dis[v] + g[i].cost; pre[u] = i; if(! vis[u]) que.push(u), vis[u] = true; } } }}int cost_flow(int s, int t, int flow){ int res = 0; while(flow > 0) { spfa(s, t); if(dis[t] == INF) return -1; int d = flow; for(int i = pre[t]; i != -1; i = pre[g[i].st]) d = min(d, g[i].cap); for(int i = pre[t]; i != -1; i = pre[g[i].st]) g[i].cap -= d, g[i^1].cap += d; flow -= d; res += d * dis[t]; } return res;}int main(){ int n, k; int mpa[M][M]; while(~ scanf("%d%d", &n, &k)) { for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &mpa[i][j]); cnt = 0; memset(head, -1, sizeof head); int ss = 0, tt = 2*n*n + 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { add_edge((i-1)*n + j, (i-1)*n + j + n*n, 1, -mpa[i][j]); add_edge((i-1)*n + j, (i-1)*n + j + n*n, k, 0); if(j != n) add_edge((i-1)*n + j + n*n, (i-1)*n + j+1, k, 0); if(i != n) add_edge((i-1)*n + j + n*n, i*n + j, k, 0); } add_edge(ss, 1, k, 0), add_edge(2*n*n, tt, k, 0); printf("%d\n", -cost_flow(ss, tt, k)); } return 0;}
阅读全文
0 0
- poj Kaka's Matrix Travels 费用流
- poj 3422 Kaka's Matrix Travels 最小费用最大流
- Kaka's Matrix Travels POJ 3422 最大费用流
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels 费用流
- POJ 3422 Kaka's Matrix Travels 最小费用流
- poj-3422-Kaka's Matrix Travels-最小费用最大流
- POJ 3422 Kaka's Matrix Travels 最小费用最大流
- poj 3422 Kaka's Matrix Travels 费用流
- 【POJ】 3422 Kaka’s Matrix Travels 费用流
- POJ 3422 Kaka's Matrix Travels | 费用流
- poj 3422 Kaka's Matrix Travels (费用流)
- POJ 3422 Kaka's Matrix Travels(费用流)
- 【最大费用最大流】POJ-3422 Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels(费用流)
- POJ 3422 Kaka's Matrix Travels(费用流)
- poj 3422 Kaka's Matrix Travels 费用流
- Vector源码
- 忘记bug,去跑步吧
- Linux下的文件描述符
- leetcode(461) Hamming Distance 简单题
- Java基础面试题
- POJ 3422 Kaka's Matrix Travels 费用流
- 维基利亚加解密(二)
- LAMP+Wordpress+阿里云搭建个人博客
- Ubuntu16.04使用pip3安装第三方库
- FPGA内部结构
- TCP 传输链接的释放
- 项目实战:微交互之按钮选择器
- Ext Js入门第1篇-Ext环境搭建
- lg1071 潜伏者