POJ 3422 Kaka's Matrix Travels(最大费用最大流 + 拆点)
来源:互联网 发布:ubuntu mysql自启动 编辑:程序博客网 时间:2024/04/30 03:42
题目链接:http://poj.org/problem?id=3422
Description
On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.
Input
The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.
Output
The maximum SUM Kaka can obtain after his Kth travel.
Sample Input
3 21 2 30 2 11 4 2
Sample Output
15
Source
//最大费用最小流只要在添加边的时候换一下位置就好了
//求最大费用最大流只需要把费用换成相反数,用最小费用最大流求解即可
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;const int MAXN = 10000;const int MAXM = 100000;const int INF = 0x3f3f3f3f;struct Edge{ int to, next, cap, flow, cost; int x, y;} edge[MAXM];int head[MAXN],tol;int pre[MAXN],dis[MAXN];bool vis[MAXN];int N, M;int map[MAXN][MAXN];void init(){ N = MAXN; tol = 0; memset(head, -1, sizeof(head));}void addedge(int u, int v, int cap, int cost)//左端点,右端点,容量,花费{ edge[tol]. to = v; edge[tol]. cap = cap; edge[tol]. cost = cost; edge[tol]. flow = 0; edge[tol]. next = head[u]; head[u] = tol++; edge[tol]. to = u; edge[tol]. cap = 0; edge[tol]. cost = -cost; edge[tol]. flow = 0; edge[tol]. next = head[v]; head[v] = tol++;}bool spfa(int s, int t){ queue<int>q; for(int i = 0; i < N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = edge[i]. next) { int v = edge[i]. to; if(edge[i]. cap > edge[i]. flow && dis[v] > dis[u] + edge[i]. cost ) { dis[v] = dis[u] + edge[i]. cost; pre[v] = i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } if(pre[t] == -1) return false; else return true;}//返回的是最大流, cost存的是最小费用int minCostMaxflow(int s, int t, int &cost){ int flow = 0; cost = 0; while(spfa(s,t)) { int Min = INF; for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to]) { if(Min > edge[i]. cap - edge[i]. flow) Min = edge[i]. cap - edge[i]. flow; } for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to]) { edge[i]. flow += Min; edge[i^1]. flow -= Min; cost += edge[i]. cost * Min; } flow += Min; } return flow;}int main(){ int n, k; while(~scanf("%d%d",&n,&k)) { init();//注意 for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d",&map[i][j]); } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { int tt = (i-1)*n+j; addedge(tt*2,tt*2+1,1,-map[i][j]);//拆点,之间建一条负的价值的边,流量为1 addedge(tt*2,tt*2+1,k-1,0);//再建一条价值为0,流量为k-1的边 //printf("tt:%d tt*2:%d tt*2+1:%d ",tt,tt*2,tt*2+1); } //printf("\n"); } for(int i = 1; i <= n; i++)//向右 { for(int j = 1; j < n; j++) { int tt = (i-1)*n+j;//编号从2开始 addedge(tt*2+1,(tt+1)*2,k,0); //printf("tt:%d tt*2:%d (tt+1)*2:%d\n",tt,tt*2,(tt+1)*2); } } for(int i = 1; i < n; i++)//向下 { for(int j = 1; j <= n; j++) { int tt = (i-1)*n+j; addedge(tt*2+1,(tt+n)*2,k,0); //printf("tt:%d tt*2:%d (tt+n)*2:%d\n",tt,tt*2,(tt+n)*2); } } int beg = 1;//超级起点 int end = 2*n*n+2;//超级汇点 addedge(beg,2,k,0);//超级起点,容量为1,花费为0 addedge(2*n*n+1,end,k,0);//超级汇点容量为1,花费为0 int ans = 0; minCostMaxflow(beg,end,ans); //minCostMaxflow(2,2*n*n+1,ans); printf("%d\n",-ans);//相反数即为最大的价值 } return 0;}
- 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 Kaka's Matrix Travels (最大费用最大流)
- POJ--3422--Kaka's Matrix Travels【最小费用最大流+拆点】
- poj3422--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 Kaka's Matrix Travels 3422 (最小费用最大流)
- sphinx 增量索引 实现近实时更新
- 几种输入输出方式的区别
- JQuery一个checkbox实现全选/全不选,取消子checkbox时,全选checkbox取消对号
- 灵感1
- 载波聚合、LTE-Hi浮出水面 抢占LTE-A演进制高点
- POJ 3422 Kaka's Matrix Travels(最大费用最大流 + 拆点)
- csdn如何转载别人的文章
- Java界面布局一般步骤
- 线程池
- 广东外帮移民公司简介
- Maven Archetype
- 数据结构 第七章 查找技术
- linux使用脚本杀死指定名称的进程
- 8421 码 4个状态