Jump
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 696 Accepted Submission(s): 296
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
构造最小费用最大流模型
分两部, x部 n*m个点, y部n*m个点
能走的边 x--->y 连一条流量为 1 , 权为花费的边
令建一个 s' 点 , 连向y部所有点, 流量1,权为0
超级源点与s‘相连, 流量为k,权为0
y部所有点与超级汇点相连, 流量为1,权为0
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
struct Edge
{
int from, to , cap, flow, cost ,next;
};
int n,m,K;
int st,en,sp,cnt;
Edge edge[31000];
int head[400],d[400],p[400],a[400];
bool vis[400];
char s[20][20];
void add(int from, int to ,int cap, int cost)
{
edge[cnt].from = from;
edge[cnt].to = to;
edge[cnt].cap = cap;
edge[cnt].flow = 0;
edge[cnt].cost = cost;
edge[cnt].next = head[ from ];
head[from]=cnt++;
edge[cnt].from = to;
edge[cnt].to = from;
edge[cnt].cap = 0;
edge[cnt].flow = 0;
edge[cnt].cost = -cost;
edge[cnt].next = head[ to ];
head[to] =cnt++;
}
bool spfa( int s, int t, int &flow, int &cost)
{
// cout<<s<<' '<<t<<endl;
memset(d,0x3f3f3f,sizeof(d));
int inf = d[0];
memset(vis,0,sizeof(vis));
queue<int> q;
d[s]= 0; a[s]=inf;
vis[s]= 1; p[s]=0;
q.push(s);
while (!q.empty())
{
int u=q.front();
q.pop();
int i=head[u];
while (i!=-1)
{
int v=edge[i].to;
if (edge[i].cap> edge[i].flow && d[v]> d[u] + edge[i].cost)
{
d[v] =d[u] +edge[i].cost;
p[v] = i;
a[v] =min (a[u], edge[i].cap - edge[i].flow );
if (!vis[v])
{
q.push(v);
vis[v]=1;
}
}
i=edge[i].next;
}
vis[u]=0;
}
if (d[t]==inf) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while ( u!=s)
{
edge[ p[u] ].flow +=a[t];
edge[ p[u]^1 ].flow -= a[t];
u= edge[ p[u] ].from;
}
return true;
}
void Minncost(int s, int t)
{
int flow=0, cost= 0;
while( spfa (s, t, flow, cost) );
if (flow==n*m) printf("%d\n",-cost);
else
printf("-1\n");
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while (T--)
{
memset(head,-1,sizeof(head));
cnt=0;
scanf("%d%d%d",&n,&m,&K);
for (int i=0; i<n; i++)
scanf("%s",s[i]);
st= 2*n*m, en=2*n*m+1, sp=2*n*m+2;
int u,v,cost;
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
int u= i*m+j;
for (int k=j+1; k<m; k++)
{
v= i*m + k+n*m;
cost = k-j-1;
if (s[i][j]==s[i][k]) cost-=s[i][j]-'0';
add( u, v, 1 , cost);
}
for (int k=i+1; k<n; k++)
{
v=k*m + j +n*m;
cost = k-i-1 ;
if (s[i][j]==s[k][j]) cost-=s[i][j]-'0';
add( u, v, 1, cost);
}
}
}
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
{
add( st, i*m+j, 1, 0 );
add( i*m+j+n*m ,en, 1, 0 );
add( sp, i*m+j+n*m, 1, 0 );
}
add( st, sp ,K, 0) ;
printf("Case %d : ",++cas);
Minncost(st ,en);
}
return 0;
}