hdu 4862 Jump(最小费用流)

时间:2023-02-03 08:18:41

Jump

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1040    Accepted Submission(s): 432


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.
 

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)
 

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
 
 
51 5 1919291 5 2919291 5 3919293 3 33333333333 3 2333333333
 

Sample Output
 
 
Case 1 : 0Case 2 : 15Case 3 : 16Case 4 : 18Case 5 : -1
 

2014多校欠下的债

给定一个n*m的网格 每个里面有一个0-9的数字

从一个格子 只可以到达他正下方或者正右方的格子  

每次可以从任意一个格子开始 最多走k次 要走到所有的格子

从一个格子到另一个的花费为|xi-xj|+|yi-yj|-1的值  

如果两个格子上的数字值相同  则可以获得这个数字值的能量

求 最终能够获得的最大的能量值


最小K路径覆盖的模型,用费用流或者KM算法解决,构造二部图,X部有N*M个节点,源点向X部每个节点连一条边,流量1,费用0Y部有N*M个节点,每个节点向汇点连一条边,流量1,费用0,如果X部的节点x可以在一步之内到达Y部的节点y,那么就连边x->y,费用为从x格子到y格子的花费能量减去得到的能量,流量1,再在X部增加一个新的节点,表示可以从任意节点出发K次,源点向其连边,费用0,流量K,这个点向Y部每个点连边,费用0,流量1,最这个图跑最小费用最大流,如果满流就是存在解,反之不存在,最小费用的相反数就是可以获得的最大能量


因为是求最大值 所以在构图时  cost的值要取相反数  跑完最小费用流后得到的cost值再取相反数

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
char ch;
int a = 0;
while((ch = getchar()) == ' ' | ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}

void Print(int a) //输出外挂
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}

struct Edge
{
int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tot;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n)
{
N=n;
tot=0;
MEM(head,-1);
}
void addedge(int u,int v,int cap,int cost)
{
edge[tot].to=v; edge[tot].cap=cap; edge[tot].cost=cost;
edge[tot].flow=0; edge[tot].next=head[u]; head[u]=tot++;
edge[tot].to=u; edge[tot].cap=0; edge[tot].cost=-cost;
edge[tot].flow=0; edge[tot].next=head[v]; head[v]=tot++;
}
bool spfa(int s,int t)
{
queue<int> q;
for(int i=0;i<N;i++)
{
dis[i]=INF; vis[i]=0; pre[i]=-1;
}
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(pre[t]==-1) return 0;
else return 1;
}

int minCostMinflow(int s,int t,int &cost)
{
int flow=0;
cost=0;
while(spfa(s,t))
{
int Min=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
Min=min(Min,edge[i].cap-edge[i].flow);
}
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
}
return flow;
}

char ch[20][20];

int main()
{
// fread;
int tc;
int cs=1;
scanf("%d",&tc);
while(tc--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++)
scanf("%s",ch[i]);
int s=0,t=2*n*m+1,p=2*n*m+2;
init(p+1);
addedge(s,p,k,0);
for(int i=1;i<=n*m;i++)
{
addedge(s,i,1,0);
addedge(i+n*m,t,1,0);
addedge(p,i+n*m,1,0);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int k=j+1;k<m;k++)
{
if(ch[i][j]==ch[i][k])
addedge(i*m+j+1,n*m+i*m+k+1,1,-(ch[i][j]-'0'-(k-j-1)));
else addedge(i*m+j+1,n*m+i*m+k+1,1,k-j-1);
}
for(int k=i+1;k<n;k++)
{
if(ch[i][j]==ch[k][j])
addedge(i*m+j+1,n*m+k*m+j+1,1,-(ch[i][j]-'0'-(k-i-1)));
else addedge(i*m+j+1,n*m+k*m+j+1,1,k-i-1);
}
}
}
int ans;
int f=minCostMinflow(s,t,ans);
printf("Case %d : ",cs++);
if(f==n*m)
printf("%d\n",-ans);
else puts("-1");
}
return 0;
}