Maximum fee of K-channel,
The value of each vertex can only be retrieved once: Split the vertex. The two parts of a vertex are connected to an edge with a capacity of MP and a side with a capacity of 0.
K times: Split the Source and Sink points. K edges are connected between the two parts.
KaKa's matrix travels
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:7985 |
|
Accepted:3191 |
Description OnN×NChessboard with a non-negative number in each grid, Kaka starts his matrix travelsSum= 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 numberSumIn each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSumKaKa can obtain for his first travel. Now Kaka is wondering what is the maximumSumHe can obtain after hisKTh travel. NoteSumIs accumulative duringKTravels. Input The first line contains two integersNAndK(1 ≤N≤ 50, 0 ≤K≤ 10) described above. The followingNLines represents the Matrix. You can assume the numbers in the matrix are no more than 1000. Output The maximumSumKaKa can obtain after hisKTh 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;}
Poj 3422 Kaka's matrix travels