转自:http://blog.youkuaiyun.com/qq574857122/article/details/38047565
Total Submission(s): 446 Accepted Submission(s): 185
Jump
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 446 Accepted Submission(s): 185
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个起点
每个起点可以向右或向下跳任意步
花费是2点间的曼哈顿距离
若2个格子的数字一样
则赚取格子上的数字的价值
问:遍历整个图的最小花费
若不能遍历则输出-1
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 220
#define M 12345
#define inf (1<<29)
//注意 点标必须是 [0 - 汇点]
//双向边,注意RE
struct Edge{
int from, to, flow, cap, nex, cost;
}edge[M*2];
int head[N], edgenum;
void add(int u,int v,int cap,int cost){//网络流要加反向弧
Edge E={u, v, 0, cap, head[u], cost};
edge[edgenum]=E;
head[u]=edgenum++;
Edge E2={v, u, 0, 0, head[v], -cost}; //这里的cap若是单向边要为0
edge[edgenum]=E2;
head[v]=edgenum++;
}
int D[N], P[N], A[N];
bool inq[N];
bool BeintmanFord(int s, int t, int &flow, int &cost){
for(int i=0;i<=t;i++) D[i]= inf;
memset(inq, 0, sizeof(inq));
D[s]=0; inq[s]=1; P[s]=0; A[s]=inf;
queue<int> Q;
Q.push( s );
while( !Q.empty()){
int u = Q.front(); Q.pop();
inq[u]=0;
for(int i=head[u]; i!=-1; i=edge[i].nex){
Edge &E = edge[i];
if(E.cap > E.flow && D[E.to] > D[u] +E.cost){
D[E.to] = D[u] + E.cost ;
P[E.to] = i;
A[E.to] = min(A[u], E.cap - E.flow);
if(!inq[E.to]) Q.push(E.to) , inq[E.to] = 1;
}
}
}
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;
}
int flow;
int Mincost(int s,int t){//返回最小费用
flow = 0 ; int cost = 0;
while(BeintmanFord(s, t, flow, cost));
return cost;
}
void init(){memset(head,-1,sizeof head); edgenum = 0;}
int n, m, k;
int Hash1(int x, int y){ return (x-1)*m+y;}
int Hash2(int x, int y){ return n*m + (x-1)*m+y; }
char s[20];
int mp[20][20];
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T, i, j, g, Cas = 1; scanf("%d",&T);
while(T--){
cin>>n>>m>>k;
for(i = 1; i <= n; i++)
{
scanf("%s",s+1);
for(j = 1; j<=m; j++)
mp[i][j] = s[j] - '0';
}
printf("Case %d : ", Cas++);
init();
int i, j, g;
int from = 0, to = Hash2(n,m)+10, jiji = to-2;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
add(from, Hash1(i,j), 1, 0);
add(Hash2(i,j), to, 1, 0);
add(jiji, Hash2(i,j), 1, 0);
for(g = j+1; g <= m; g++)
{
int cos = g - j - 1;
if(mp[i][j] == mp[i][g]) cos -= mp[i][j];
add(Hash1(i,j), Hash2(i, g), 1, cos);
}
for(g = i+1; g <= n; g++)
{
int cos = g - i - 1;
if(mp[i][j] == mp[g][j]) cos -= mp[i][j];
add(Hash1(i,j), Hash2(g, j), 1, cos);
}
}
}
add(from, jiji, k, 0);
int ans = - Mincost(from, to);
if(flow != n*m)
puts("-1");
else
cout<< ans << endl;
}
return 0;
}