Jump
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 693 Accepted Submission(s): 294
Problem Description
There are n*m grids, each grid contains a number, ranging from 0-9. Your initial energy is zero. You can play up to K times the game, every time you can choose any one of the grid as a starting point (but not traveled before) then you can choose a grid on the right or below the current grid to jump, but it has not traveled before. Every time you can jump as many times as you want, as long as you do not violate rules. If you are from (x1, y1) to (x2, y2), then you consume |x1-x2|+|y1-y2|-1 energies. Energy can be negative.
However, in a jump, if you start position and end position has same numbers S, then you can increase the energy value by S.
Give me the maximum energy you can get. Notice that you have to go each grid exactly once and you don’t have to play exactly K times.
However, in a jump, if you start position and end position has same numbers S, then you can increase the energy value by S.
Give me the maximum energy you can get. Notice that you have to go each grid exactly once and you don’t have to play exactly K times.
Input
The first line is an integer T, stands for the number of the text cases.
Then T cases followed and each case begin with three numbers N, M and K. Means there are N rows and M columns, you have K times to play.
Then N lines follow, each line is a string which is made up by M numbers.
The grids only contain numbers from 0 to 9.
(T<=100, N<=10,M<=10,K<=100)
Then T cases followed and each case begin with three numbers N, M and K. Means there are N rows and M columns, you have K times to play.
Then N lines follow, each line is a string which is made up by M numbers.
The grids only contain numbers from 0 to 9.
(T<=100, N<=10,M<=10,K<=100)
Output
Each case, The first you should output “Case x : ”,(x starting at 1),then output The maximum number of energy value you can get. If you can’t reach every grid in no more than K times, just output -1.
Sample Input
5 1 5 1 91929 1 5 2 91929 1 5 3 91929 3 3 3 333 333 333 3 3 2 333 333 333
Sample Output
Case 1 : 0 Case 2 : 15 Case 3 : 16 Case 4 : 18 Case 5 : -1
这个题如果网络流做题做的很多了应该是很容易看出来是用最小费用流来解决~
思路:先构建二分图,X部这边有n*m个节点,Y部同样也有n*m个节点,从源点向X部的节点连边,流量为1,费用为0,同样从Y部向汇点连边,流量为1,费用为0,然后对于X和Y,如果X节点可以通过一步到达Y节点,则流量为1,费用为花费减收益,再在X部加一个节点,该节点向Y部节点连边,流量为1,费用为0,然后从源点向该节点连边,流量为K,费用为0,然后跑一边最小费用最大流即可,如果慢流就有解,最小费用取相反数就是答案了。
#include
#include
#include
#include
#include
using namespace std; #define INF ((1<<31)-1) char mm[12][12]; int k; int n,m; int head[220]; int d; typedef struct node{ int u; int v; int ca; int cflow; int flow; }node; node edge[40000]; int next[40000]; int vis[220]; int fa[220]; queue
q; int dp[220]; int ans; void add(int u,int v,int c,int f) { edge[d].u=u; edge[d].v=v; edge[d].ca=c; edge[d].cflow=f; edge[d].flow=0; next[d]=head[u]; head[u]=d++; } int bfs() { memset(vis,0,sizeof(vis)); int rear=0,front=0,i; memset(vis,0,sizeof(vis)); for(i=0;i<=n*m*2+2;i++)dp[i]=INF; dp[n*m*2]=0; q.push(n*m*2); vis[n*m*2]=1; while(!q.empty()) { int f=q.front(); q.pop(); vis[f]=0; int temp; for(i=head[f];i!=-1;i=next[i])if(edge[i].cflow-edge[i].flow>0&&dp[edge[i].v]>(temp=dp[f]+edge[i].ca)){ dp[edge[i].v]=temp; fa[edge[i].v]=i; if(!vis[edge[i].v]) { vis[edge[i].v]=1; q.push(edge[i].v); } } } return dp[n*m*2+2]; } int solve() { int i,j; ans=0; int fll=0; int temp; while(1) { if((temp=bfs())!=INF){ // printf("%d\n",temp); ans+=temp; fll++; for(i=n*m*2+2;i!=n*m*2;i=edge[fa[i]].u){ edge[fa[i]].flow++; if(fa[i]%2)edge[fa[i]-1].flow=-edge[fa[i]].flow; else edge[fa[i]+1].flow=-edge[fa[i]].flow; } } else break; } if(fll==n*m)return 1; return 0; } int main() { int t; int tt=0; scanf("%d",&t); while(t--) { int i,j,ii,jj; int c; d=0; memset(head,-1,sizeof(head)); scanf("%d%d%d",&n,&m,&k); for(i=0;i