poj_3422_Kaka's Matrix Travels
来源:互联网 发布:广州淘宝服装加工厂 编辑:程序博客网 时间:2024/06/05 02:17
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.
题解
建图:拆点,把一个点拆成两个点,每两个点连一条边。 设置一个源点和汇点,上模版。
代码:
#include <iostream>#include <queue>#include <string.h>using namespace std;#define inf 1 << 30#define M 5005int n, m, nm, ans;int map[51][51],head[M],qu[M],dis[M];bool v[M];struct node { int x,y,v,w,next;} e[100005];void add(int x, int y, int w, int f){ e[nm].x=x; e[nm].y=y; e[nm].v=w; e[nm].w=f; e[nm].next=head[x]; head[x]=nm; nm++; e[nm].x=y; e[nm].y=x; e[nm].v=-w; e[nm].w=0; e[nm].next=head[y]; head[y]=nm; nm++;}bool spfa() { for (int i=0;i<=n*n*2+1;i++){ qu[i] = -1; dis[i] = inf; v[i] = false; } queue<int>q; dis[n*n*2]=0; v[n*n*2]=true; q.push(n*n*2); int i; while (!q.empty()){ int t=q.front(); q.pop(); i=head[t]; v[t]=false; while (i!=-1){ if (e[i].w>0&&dis[e[i].y]>dis[t]+e[i].v){ dis[e[i].y]=dis[t]+e[i].v; qu[e[i].y]=i; if (!v[e[i].y]){ v[e[i].y]=true; q.push(e[i].y); } } i=e[i].next; } } if (qu[n*n*2+1]==-1) return false; return true;}void getflow() { while (spfa()) { int max1=inf; int p=qu[n*n*2+1]; while (p!=-1) { max1=min(max1,e[p].w); p=qu[e[p].x]; } p=qu[n*n*2+1]; while (p!=-1){ e[p].w-=max1; e[p^1].w+=max1; ans+=max1*e[p].v; p=qu[e[p].x]; } }}int main() { while (cin>>n>>m) { int i,j; nm=0; for (i=1;i<=n;i++) for (j=1;j<=n;j++) cin>>map[i][j]; memset(head,-1,sizeof(head)); for (i=1;i<=n;i++) for (j=1;j<=n;j++){ int x=(i-1)*n+j-1; add(x*2,x*2+1,-map[i][j],1); add(x*2,x*2+1,0,m-1); } for (i=1;i<=n;i++) for (j=1;j<n;j++){ int x=(i-1)*n+j-1; add(x*2+1,(x+1)*2,0,m); } for (i=1;i<n;i++) for (j=1;j<=n;j++){ int x=(i-1)*n+j-1; add(x*2+1,(x+n)*2,0,m); } add(n*n*2,0,0,m); add(n*n*2-1,n*n*2+1,0,m); ans=0; getflow(); cout<<-ans<<endl; } return 0;}
- poj_3422_Kaka's Matrix Travels
- Kaka's Matrix Travels
- poj3422 Kaka's Matrix Travels
- Poj3422 Kaka's Matrix Travels
- poj Kaka's Matrix Travels
- POJ3422 Kaka's Matrix Travels
- POJ3422 Kaka's Matrix Travels
- POJ3422 Kaka's Matrix Travels
- Kaka's Matrix Travels (最大费用流)
- 【费用流】Kaka's Matrix Travels
- 【最大费用流】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
- 【POJ】【P3422】【Kaka's Matrix Travels】【题解】
- 基于swoole扩展实现真正的PHP数据库连接池
- springmvc,freemarker模式下脚本注入问题
- 蓝桥杯【历届试题】九宫重排
- Lua游戏逻辑开发中的优化思路
- 串口发送一帧数据时,两个字节的间隔时间是多少?
- poj_3422_Kaka's Matrix Travels
- 文章标题
- tcp协议ack以及乱序报文暂存的实现--立即ack/延迟ack/捎带ack
- Android5.0之CardView的使用
- 单例模式引起的内存泄漏
- protobuf C#编译
- MFC多线程
- onmouseenter和onmuseover的区别
- Java中正则表达式语法(记住特别的)