hdu 4862

Jump

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


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
 

        做这一题时尝试了许多建图的方法,但发现都有问题,最后看了下题解发现建图的方式很巧妙。在这之前,我们需要知道这样几个概念:

最小路径覆盖:

        比较正式的解释:在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联;(如果把这些路径中的每条路径从它的起始点走到它的             终点,那么恰好可以经过图中的每个顶点一次且仅一次);如果不考虑图中存在回路,那么每条路径就是一个弱连通子集. 

       非正式解释:在一个有向图中至少放几个人才能走遍所有点。

 

       路径覆盖与二分图匹配的关系(必须是有向无环图):

             最小路径覆盖=|P|-最大匹配数

             http://baike.baidu.com/view/2444809.htm?fr=aladdin

 

      而这一题的题意恰好满足上面的性质,从每个点只能向右或者向下走,保证了有向无环。每个点有且仅有一次经过。这题就变成了求最优匹配,但还要考虑到能走k次。下面使用网络流解决的。

 

        建图方法如下,将n*m点拆成2*n*m个点,将源点向左边n*m个点连一条容量为1,费用为0的边,将右边n*m个点向汇点连一条容量为1,费用为0的边,在左部添加一个点,由源点向该点连一条容量为k,费用为0的边,并将该点向右部的点连一条容量为1,费用为0的边,最后若x能够到达y,则连一条容量为1,费用为消耗的能量-得到的能量的边。

 

 

        将所建的模型跑最小费用流,若是满流费用则为负的得到的费用,否则不能找到解。

 

        为什么可以这样建图,理由如下:如果我们不考虑那个特殊节点的话,跑最小费用流等效于求最优匹配,而最小路径覆盖=|P|-最大匹配数,若k>=最小路径,则存在最优解。不知道说清楚没有,下面附代码:

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3fffffff
#define eps 1e-8
#define pb push_back
#define pf printf
#define sf scanf
#define ms memset
#define bk break
#define rt return
#define REP0(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,s,n) for(int i=(s);i<=(n);i++)
#define maxn 210
#define MOD
int n,m,k,sink,source,edgeNum;
char mapt[15][15];
int head[maxn],pre[maxn],dis[maxn],vis[maxn];
struct node{
    int to,cap,cost,next;
    bool FZ(int nto,int nnext,int ncap,int ncost){
        to = nto; next = nnext; cap = ncap; cost = ncost;
        return true;
    }
}edge[30000];
int addEdge(int from,int to,int cap,int cost){
//    edge[edgeNum].to = to;
//    edge[edgeNum].cap = cap; edge[edgeNum].cost = cost;
//    edge[edgeNum].next = head[from];
//    head[from] = edgeNum++;
//    edge[edgeNum].to = from;
//    edge[edgeNum].cap =0; edge[edgeNum].cost = -cost;
//    edge[edgeNum].next = head[to];
//    head[to] = edgeNum++;
    edge[edgeNum].FZ(to,head[from],cap,cost);
    head[from] =edgeNum++;
    edge[edgeNum].FZ(from,head[to],0,-cost);
    head[to] = edgeNum++;
    return 0;
}
int init(){
    edgeNum =0;
    ms(head,-1,sizeof head);
    return 0;
}
bool spfa(){
    queue<int> que;
    memset(pre,-1,sizeof pre);
    memset(vis,0,sizeof vis);
    REP0(i,maxn) dis[i] = INF;
    //for(int i=0;i<10;i++) pf("%d ",dis[i]);
    vis[source] = 1; dis[source] = 0;
    que.push(source);
    while(!que.empty()){
        int now = que.front(); que.pop();
        //pf("%d <<\n",now);
        for(int j=head[now];j!=-1;j=edge[j].next){
            int to = edge[j].to,cap=edge[j].cap,cost=edge[j].cost;
            //pf("%d %d==\n",dis[to],dis[now]+cost);
            if(cap>0&&dis[to]>dis[now]+cost){
                dis[to] = dis[now] + cost;
                pre[to] = j;
                if(!vis[to]){
                    que.push(to);
                    vis[to] = 1;
                }
            }
        }
        vis[now] =0;
    }
    //pf("---");
    return pre[sink] != -1;
}
int MCMF(int& flow){
    flow = 0;
    int minFlow,minCost=0;
    while(spfa()){
        minFlow = INF;
        for(int i=pre[sink];i!=-1;){
            int to = edge[i^1].to;
            //pf("%d\n",i);
            minFlow = min(minFlow,edge[i].cap);
            i= pre[to];
        }
        flow += minFlow;
        //pf("%d\n",minFlow);
        for(int i=pre[sink];i!=-1;){
            int to = edge[i^1].to;
            edge[i].cap -= minFlow;
            edge[i^1].cap += minFlow;
            i = pre[to];
        }
        minCost += dis[sink];
        //pf("%d-\n",minCost);
    }
    return minCost;
}
int main(){
    #ifdef ACBang
        freopen("data.in","r",stdin);
    #endif // ACBang
    int t;
    sf("%d",&t);
    for(int p=1;p<=t;p++){
        init();
        sf("%d%d%d",&n,&m,&k);
        for(int i=0;i<n;i++){
            scanf("%s",mapt[i]);
        }
        source = 0; sink = 2*(n*m)+2;
        REP1(i,n*m){
            addEdge(0,i,1,0);
        }
        addEdge(0,n*m+1,k,0);
        REP(i,n*m+2,sink-1){
            addEdge(n*m+1,i,1,0);
        }
        REP(i,n*m+2,sink-1){
            addEdge(i,sink,1,0);
        }
        REP0(i,n){
            REP0(j,m){
                for(int x=i+1;x<n;x++){
                    int dist = x-i-1;
                    int cost = mapt[i][j]==mapt[x][j]?dist - (mapt[i][j]-48):dist;
                    int tmp = x*m+j+1+(n*m+1);
                    addEdge(i*m+j+1,tmp,1,cost);
                }
                for(int y=j+1;y<m;y++){
                    int dist = y-j-1;
                    int cost = mapt[i][j]==mapt[i][y]?dist - (mapt[i][j]-48):dist;
                    int tmp = i*m+y+1+(n*m+1);
                    addEdge(i*m+j+1,tmp,1,cost);
                    //pf("%d %d>>\n",i*m+j+1,tmp,cost);
                }
            }
        }
//        REP(i,0,sink){
//            pf("%d :",i);
//            for(int j=head[i];j!=-1;j=edge[j].next){
//                pf("(%d %d)",edge[j].to,edge[j].cap);
//            }
//            pf("\n");
//        }
        int flow =0,minCost;
        minCost = MCMF(flow);
        pf("Case %d : ",p);
        if(flow == n*m){
            pf("%d\n",-minCost);
        }else pf("-1\n");
        //pf("%d %d\n",flow,minCost);
    }
    return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值