2014多校联合第一场 1002 Jump

http://acm.hdu.edu.cn/showproblem.php?pid=4862

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

解题思路:

           最小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,最这个图跑最小费用最大流,如果满流就是存在解,反之不存在,最小费用的相反数就是可以获得的最大能量

111551992014-07-23 16:57:06Accepted486215MS292K4390 BC++ 


#include <iostream>
#include <cstdio>

using namespace std;

const int oo=1e9;//无穷大
const int maxm=1111111;//边的最大数量,为原图的两倍
const int maxn=2222;//点的最大数量

int node,src,dest,edge;//de节点数,src源点,dest汇点,edge边数
int head[maxn],p[maxn],dis[maxn],q[maxn],vis[maxn];//head链表头,p记录可行流上节点对应的反向边,dis计算距离

struct edgenode {
    int to;//边的指向
    int flow;//边的容量
    int cost;//边的费用
    int next;//链表的下一条边
} edges[maxm];

void prepare(int _node,int _src,int _dest);
void addedge(int u,int v,int f,int c);
bool spfa();

inline int min(int a,int b) {
    return a<b?a:b;
}

inline void prepare(int _node,int _src,int _dest) {
    node=_node;
    src=_src;
    dest=_dest;
    for (int i=0; i<node; i++) {
        head[i]=-1;
        vis[i]=false;
    }
    edge=0;
}

void addedge(int u,int v,int f,int c) {
    edges[edge].flow=f;
    edges[edge].cost=c;
    edges[edge].to=v;
    edges[edge].next=head[u];
    head[u]=edge++;
    edges[edge].flow=0;
    edges[edge].cost=-c;
    edges[edge].to=u;
    edges[edge].next=head[v];
    head[v]=edge++;
}

bool spfa() {
    int i,u,v,l,r=0,tmp;
    for (i=0; i<node; i++) dis[i]=oo;
    dis[q[r++]=src]=0;
    p[src]=p[dest]=-1;
    for (l=0; l!=r; ((++l>=maxn)?l=0:1)) {
        //printf("**\n");
        for (i=head[u=q[l]],vis[u]=false; i!=-1; i=edges[i].next) {
            if (edges[i].flow&&dis[v=edges[i].to]>(tmp=dis[u]+edges[i].cost)) {
                dis[v]=tmp;
                p[v]=i^1;
                if (vis[v]) continue;
                vis[q[r++]=v]=true;
                if (r>=maxn) r=0;
            }
        }
    }
    return p[dest]>=0;
}
struct note {
    int x,y;
};
note spfaflow() {
    int i,ret1=0,ret2=0,delta;
    while (spfa()) {

        for (i=p[dest],delta=oo; i>=0; i=p[edges[i].to]) {
            delta=min(delta,edges[i^1].flow);
        }
        for (int i=p[dest]; i>=0; i=p[edges[i].to]) {
            edges[i].flow+=delta;
            edges[i^1].flow-=delta;
        }
        ret1+=delta*dis[dest];
        ret2+=delta;
    }
    note ret;
    ret.x=ret2;
    ret.y=ret1;
    return ret;
}
char a[15][15];
int value[15][15];
int point[15][15];
int n,m,k;
int main() {
    int T;
    scanf("%d",&T);
    int cas=0;
    while(T--) {
        scanf("%d%d%d%*c",&n,&m,&k);
        int cnt=1;
        for(int i=0; i<n; i++) {
            scanf("%s",a[i]);
            for(int j=0; j<m; j++) {
                value[i][j]=a[i][j]-'0';
                point[i+1][j+1]=cnt++;
            }
        }
        //printf("%d\n",cnt);
        prepare(2*n*m+3,0,2*n*m+2);
        addedge(src,n*m*2+1,k,0);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++) {
                addedge(src,point[i][j],1,0);
                addedge(point[i][j]+m*n,dest,1,0);
                addedge(n*m*2+1,n*m+point[i][j],1,0);
            }
        /*for (int i=0; i<2*n*m+3; i++) {
            for (int k=head[i]; k!=-1; k=edges[k].next) {
                if (k&1) continue;
                int v=edges[k].to;
                int f=edges[k].cost;
                printf("%d %d %d\n",i,v,f);
            }
        }
        cerr<<"---------"<<endl;
        */
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++) {
                for(int x=i+1; x<=n; x++) {
                    int temp=x-i-1;
                    if(a[i-1][j-1]==a[x-1][j-1])
                        temp-=(a[x-1][j-1]-'0');
                    addedge(point[i][j],point[x][j]+m*n,1,temp);
                }
                for(int x=j+1; x<=m; x++) {
                    int temp=x-j-1;
                    if(a[i-1][j-1]==a[i-1][x-1])
                        temp-=(a[i-1][x-1]-'0');
                    addedge(point[i][j],point[i][x]+m*n,1,temp);
                }
            }
        /*for (int i=0; i<2*n*m+3; i++) {
            for (int k=head[i]; k!=-1; k=edges[k].next) {
                int v=edges[k].to;
                int f=edges[k].cost;
                printf("%d %d %d\n",i,v,f);
            }
        }
        */
        note ret=spfaflow();
        if(ret.x!=n*m)
            printf("Case %d : -1\n",++cas);
        else
            printf("Case %d : %d\n",++cas,-ret.y);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值