Jump
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 428 Accepted Submission(s): 176
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
Author
FZU
Source
还是做题太少了,根本不知道怎样建图,
参考别人的:
题意:给出一个n×m的数字矩阵,给定一个k。表示你可以在这个矩阵上进行k次遍历,每一次遍历选择一个起点,遍历的方式只能向右或者向下移动。移动的代价如题目所述,如果目的地的数字与起始位置的数字相同,则获得数字的效益。求在k次遍历后是否能遍历整个矩阵,若能输出最大效益,否则输出-1.
题解:讲图变成二分图,X部和Y部的点数都为n×m+k,X部中的i点一步能到达Y部中的j点,则建一条权值为效益-代价,X部中的新添加的K个点与Y中前N*M个点建一条权值为0的点,相应的Y部中后K个点也是一样。这样表示每次遍历的起点的前驱和后继都是新建立的点,并且不会产生任何代价和效益。最后通过判断是否X部中的点都被匹配上就可以判断是否能遍历整个矩阵。最后的最优匹配就是答案。
等下再用最小费用流做做看。。
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
const int INF=1000000;
const int maxn=555;
bool xm[maxn],ym[maxn];
int w[maxn][maxn],A[maxn],B[maxn],rm[maxn];
// A左集合可行顶标, B为右集合可行顶标, rm[]为右集合点匹配左集合哪个点
// xm[] ym[] 分别表示左、右集合点是否在交错树中, 1是,0不是
int Hs,ms;
int n,m;
char val[maxn][maxn];
bool hungary (int a)
{ //用匈牙利找最大匹配
int i;
xm[a]=true;
for(i=1;i<=ms;i++)
{
if(!ym[i]&&w[a][i]==A[a]+B[i])
{
ym[i]=true;
if(rm[i]==0||hungary(rm[i]))
{
rm[i]=a;
return true;
}
}
}
return false;
}
void KM()
{
int sum=0,d;
memset(B,0,sizeof(B));
for(int i=1;i<=ms;i++)
{
A[i]=-INF;
for(int j=1;j<= ms;j++)
{
A[i]=max(A[i],w[i][j]);
}
}
memset(rm,0,sizeof(rm));
for(int i=1;i<=ms;i++)
{
while(1)
{
memset(xm,0,sizeof (xm));
memset(ym,0,sizeof (ym));
if (hungary(i))
break;
d=INF;
for(int k=1;k<=ms;k++)
{
if(xm[k])
{
for(int j=1;j<=ms;j++)
{
if (!ym[j])
d=min(d,A[k]+B[j]-w[k][j]);
}
}
}
for (int j=1;j<=ms;j++)
{
if(xm[j])
A[j]-=d;
if(ym[j])
B[j]+=d;
}
}
}
int i;
for (i=1;i<=ms;i++)
{
if(w[rm[i]][i]==-INF)
break;
sum+=w[rm[i]][i];
}
if (i<=ms)
puts("-1");
else
printf("%d\n",sum);
}
int main()
{
int t,cas=1;
int k;
cin>>t;
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++)
scanf("%s",val[i]);
for(int i=1;i<=n*m+k;i++)
{
for(int j=1;j<=n*m+k;j++)
w[i][j]=-INF;
}
for(int i=1;i<=k;i++)
w[i+n*m][i+n*m]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int ii=0;ii<k;ii++) w[n*m+ii+1][i*m+j+1]=w[i*m+j+1][n*m+ii+1]=0;
for(int jj=j+1;jj<m;jj++)
{
w[i*m+j+1][i*m+jj+1]=(val[i][j]==val[i][jj]?val[i][j]-'0':0)-(jj-j-1);
}
for(int ii=i+1;ii<n;ii++)
{
w[i*m+j+1][ii*m+j+1]=(val[i][j]==val[ii][j]?val[i][j]-'0':0)-(ii-i-1);
}
}
}
ms=n*m+k;
printf("Case %d : ", cas++);
KM();
}
return 0;
}