POJ 3422 Kaka's Matrix Travels,poj3422

來源:互聯網
上載者:User

POJ 3422 Kaka's Matrix Travels,poj3422


K路最大費用最大流,

每個點的值只能取一次: 拆點,一個點的兩個部分之間連 1 條費用mp容量一的邊,連一條費用0容量很大的邊

K次: 源點和匯點拆點,兩個部分之間連K條邊


Kaka's Matrix Travels
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7985   Accepted: 3191

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 SUMKaka 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

POJ Monthly--2007.10.06, Huang, Jinsong



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int maxn=100100;const int INF=1<<30;struct Edge{int to,next,cap,flow,cost;}edge[maxn*20];int Adj[maxn],Size,n;void init(){memset(Adj,-1,sizeof(Adj)); Size=0;}void addedge(int u,int v,int cap,int cost){edge[Size].to=v;edge[Size].next=Adj[u];edge[Size].cost=cost;edge[Size].cap=cap;edge[Size].flow=0;Adj[u]=Size++;}void Add_Edge(int u,int v,int cap,int cost){//cout<<"add :"<<u<<" , "<<v<<" , "<<cap<<" , "<<cost<<endl;addedge(u,v,cap,cost);addedge(v,u,0,-cost);}int dist[maxn],vis[maxn],pre[maxn];bool spfa(int s,int t){queue<int> q;for(int i=0;i<n;i++){dist[i]=-INF; vis[i]=false; pre[i]=-1;}dist[s]=0; vis[s]=true; q.push(s);while(!q.empty()){int u=q.front(); q.pop();vis[u]=false;for(int i=Adj[u];~i;i=edge[i].next){int v=edge[i].to;if(edge[i].cap>edge[i].flow&&dist[v]<dist[u]+edge[i].cost){dist[v]=dist[u]+edge[i].cost;pre[v]=i;if(!vis[v]){vis[v]=true;q.push(v);}}}}if(pre[t]==-1) return false;return true;}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;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;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 N,K;int mp[70][70];int main(){while(scanf("%d%d",&N,&K)!=EOF){init();for(int i=0;i<N;i++){for(int j=0;j<N;j++){scanf("%d",&mp[i][j]);int a=i*N+j;int b=a+N*N;Add_Edge(a,b,1,mp[i][j]);Add_Edge(a,b,K-1,0);if(j+1<N) Add_Edge(b,a+1,K,0);if(i+1<N) Add_Edge(b,a+N,K,0);}}int s=0,t=2*N*N-1;n=2*N*N;int FLOW,COST;FLOW=MinCostMaxFlow(s,t,COST);//printf("FLOW=%d COST=%d\n",FLOW,COST);printf("%d\n",COST);}return 0;}




對於最小費用最大流

const
maxn=100;
maxq=100000;

var
p,c,f:array[1..maxn,1..maxn] of longint;
dist,path:array[1..maxn] of longint;
q:array[1..maxq] of longint;
check:array[1..maxn] of 0..1;
n,m:longint;
mark:boolean;

procedure init;
var
s,t,r,w,i,j:longint;
begin
readln (n);
readln (m);
for i:=1 to m do begin
readln (s,t,r,w);
c[s,t]:=r;p[s,t]:=w;
end;
end;

procedure main;
var
i,j,a,r,l,k:longint;
begin
fillchar(f,sizeof(f),0);
mark:=true;
while mark do begin
q[1]:=1;r:=0;l:=1;
mark:=false;
for i:=2 to n do dist[i]:=maxlongint;
fillchar(check,sizeof(check),0);dist[1]:=0;
fillchar(path,sizeof(path),0);
while r<l do begin
inc(r);
for i:=2 to n do if (c[i,q[r]]<>0) or (c[q[r],i]<>0) then begin
if (c[q[r],i]<>0) and (f[q[r],i]<c[q[r],i]) and (dist[q[r]]+p[q[r],i]<dist[i]) then begin
dist[i]:=dist[q[r]]+p[q[r],i];
path[i]:=q[r];
if check[i]=0 then begin inc(l);q[l]:=i;check[i]:=1;end;
end;
if (c[i,q[r]]<>0) and (f[i,q[r]]<>0) and (dist[q[r]]-p[i,q[r]]<dist[i]) then begin
dist[i]:=dist[q[r]]-p[i,q[r]];
path[i]......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.