hdu4862 Jump 费用流

Jump

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


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
  
  
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次,对于每一次跳跃,你可以跳多步,必须选择之前未访问的点作为起点且过程中不能经过以访问点,从(x1,y1)跳到(x2,y2)消耗|x1-y1|+|x2-y2|-1单位的能量,如果这两个点相同,会额外获得这个点的数值的能量,求遍历所有点可以获得的最大能量。

题目模型:k次路径覆盖
难点在于如何处理遍历所有的点,对于每一个点x拆点,建立(x,x',1,100000)的边,这里把费用置成100000可以使每次增广的时候优先选择该点,然后通过回流来调整费用,因为最多只会跳(n*m+1)/2次,而每次费用都可以增大,那么走k遍spfa就可以得出最后结果了,最后如果ans<n*m*100000,则没有遍历到所有点,输出-1,否则输出ans%100000

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define inf 0x3f3f3f3f
#define rep(_,a,b) for(int _=a;_<=b;_++)
#define rep_e(i,u) for(int i=head[u];i!=-1;i=e[i].next)
using namespace std;
int cas,n,m,k;
char c[12][12];
void read(){
    scanf("%d%d%d",&n,&m,&k);
    rep(i,0,n-1)rep(j,0,m-1) cin>>c[i][j];
}
int s,t;
int d[400],pre[400];
bool vis[400];
struct edge{
    int u,v,next,f,c;
}e[400010];
int head[400],tol;
void init(){
    tol=0;
    memset(head,-1,sizeof head);
}
void addedge(int u,int v,int f,int c){
    //cout<<u<<" "<<v<<" "<<f<<" "<<c<<endl;
    e[tol].u=u,e[tol].v=v,e[tol].f=f,e[tol].c=c,e[tol].next=head[u],head[u]=tol++;
    e[tol].u=v,e[tol].v=u,e[tol].f=0,e[tol].c=-c,e[tol].next=head[v],head[v]=tol++;
}
bool spfa(){
    fill(d,d+400,-inf);
    memset(pre,-1,sizeof pre);
    memset(vis,0,sizeof vis);
    queue<int> q;
    q.push(s),d[s]=0,vis[s]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        rep_e(i,u){
            int v=e[i].v;
            if(e[i].f&&d[v]<d[u]+e[i].c){
                d[v]=d[u]+e[i].c;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return d[t]!=-inf;
}
int mcmf(){
    int ret=0;
    int cnt=0;
    while(cnt<min(k,(m*n+1)/2)){
        if(!spfa()) break;
        int minf=inf;
        for(int i=t;i!=s;i=e[pre[i]].u){
            minf=min(minf,e[pre[i]].f);
        }
        for(int i=t;i!=s;i=e[pre[i]].u){
            e[pre[i]].f-=minf;
            e[pre[i]^1].f+=minf;
        }
        ret+=minf*d[t];
        cnt++;
    }
    return ret;
}
void solve(int ca){
    s=2*n*m,t=2*n*m+1;
    rep(i,0,n-1)rep(j,0,m-1){
        addedge(s,i*m+j,1,0);
    }
    rep(i,0,n-1)rep(j,0,m-1){
        addedge(i*m+j+n*m,t,1,0);
    }
    rep(i,0,n-1)rep(j,0,m-1){
        addedge(i*m+j,i*m+j+n*m,1,100000);
    }
    rep(i,0,n-1)rep(j,0,m-1){
        rep(ii,i+1,n-1){
            if(c[i][j]==c[ii][j]) addedge(i*m+j+n*m,ii*m+j,1,c[i][j]-'0'-ii+i+1);
            else addedge(i*m+j+n*m,ii*m+j,1,i+1-ii);
        }
        rep(jj,j+1,m-1){
            if(c[i][j]==c[i][jj]) addedge(i*m+j+n*m,i*m+jj,1,c[i][j]-'0'-jj+j+1);
            else addedge(i*m+j+n*m,i*m+jj,1,j+1-jj);
        }
    }
    int ret=mcmf();
    printf("Case %d : ",ca);
    if(ret<n*m*100000) printf("-1\n");
    else printf("%d\n",ret%100000);
}
int main(){
    scanf("%d",&cas);
    rep(ca,1,cas){
        init();
        read();
        solve(ca);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值