poj 3422 最小费用流 Bellman-Ford 拆点
来源:互联网 发布:绝地求生枪械数据最新 编辑:程序博客网 时间:2024/06/06 03:14
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
#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>typedef long long ll;using namespace std;#define MV 15000#define INF 0x3f3f3f3fint a[55][55];struct edge{ int to,cap,cost,rev;};vector<edge>G[MV];int dis[MV];int preve[MV],prevv[MV];int min_cost_flow(int v,int s,int t,int f){ int ans=0,i,j; while(f>0) { fill(dis,dis+v,INF); dis[s]=0; bool update=true; while(update) { update=false; for(i=0;i<v;i++) { if(dis[i]==INF) continue; int size1=G[i].size(); for(j=0;j<size1;j++) { edge &es=G[i][j]; if(es.cap>0&&dis[es.to]>dis[i]+es.cost) { dis[es.to]=dis[i]+es.cost; preve[es.to]=j; prevv[es.to]=i; update=true; } } } } if(dis[t]==INF) { return -1; } int d=f; for(i=t;i!=s;i=prevv[i]) { d=min(d,G[prevv[i]][preve[i]].cap); } f-=d; ans+=d*dis[t]; for(i=t;i!=s;i=prevv[i]) { edge &es=G[prevv[i]][preve[i]]; es.cap-=d; G[es.to][es.rev].cap+=d; } } return ans;}void addedge(int s,int t,int cap,int cost){ edge es; es.to=t; es.cap=cap; es.cost=cost; es.rev=G[t].size(); G[s].push_back(es); es.to=s; es.cap=0; es.cost=-cost; es.rev=G[s].size()-1; G[t].push_back(es);}int main(){ int v, s, t, f; int i,j; int n,k; while(scanf("%d%d",&n,&k)==2) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<MV;i++) { G[i].clear(); } for(i=0;i<n;i++) { for(j=0;j<n;j++) { addedge(i*n+j, i*n+j+n*n, 1, -a[i][j]); addedge(i*n+j, i*n+j+n*n, k, 0); if(i<n-1) { addedge(i*n+j+n*n, (i+1)*n+j, k, 0); } if(j<n-1) { addedge(i*n+j+n*n, i*n+j+1, k , 0); } } } s=(n-1)*n+n-1+n*n+1; t=s+1; addedge(s, 0, k, 0); addedge(t-2, t, k, 0); int tt=-min_cost_flow(t+1,s, t, k); printf("%d\n",tt); } return 0;}
- poj 3422 最小费用流 Bellman-Ford 拆点
- 最小费用流bellman-ford算法总结
- 最小费用流模板(Bellman-ford)
- 最小费用流 Bellman-Ford与Dijkstra 模板
- 最小费用最大流(Bellman-Ford找增广路)
- 最小费用流模板(Bellman-Ford算法找最短路)
- POJ--3422--Kaka's Matrix Travels【最小费用最大流+拆点】
- poj 3422 有流量限制的最小费用流 反用求最大 + 拆点
- POJ 3422 最小费用流
- poj-3259 bellman-ford
- poj 3259 bellman-ford
- poj 1860 bellman-ford
- poj 2240 bellman-ford
- poj 1847 bellman-ford
- poj 1860 Bellman-Ford
- poj 1860 (bellman-ford)
- poj 2240 Bellman-ford
- poj 1860 Bellman-Ford
- 【nginx-rtmp】08、指令(Directives)之 直播(Live)
- CodeForces 734D:Anton and Chess(模拟?)
- html2canvas 将html代码转为图片
- 使用express创建nodejs项目
- JavaScript中的闭包
- poj 3422 最小费用流 Bellman-Ford 拆点
- Android Framework--事件分发
- Nginx----正向代理和反向代理的区别
- 解决phpStudy集成环境下 , 项目运行稍慢的问题
- POJ1260 Pearls(DP)
- 使用Debug Diagnostic Tool排除内存泄漏故障
- mysql 存储过程的见解
- progressActivity包裹swpielayout出现包裹内容无法隐藏问题
- 封装弹性dialog框架,使用只需一行代码