hdu 4862 Jump(最小k路径覆盖)

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

Jump

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


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
2014 Multi-University Training Contest 1 
最小K路径覆盖的模型,用费用流或者KM算法解决,构造二部图,X部有N*M个节点,源点向X部每个节点连一条边,流量1,费用0,Y部有N*M个节点,每个节点向汇点连一条边,流量1,费用0,如果X部的节点x可以在一步之内到达Y部的节点y,那么就连边x->y,费用为从x格子到y格子的花费能量减去得到的能量,流量1,再在X部增加一个新的节点,表示可以从任意节点出发K次,源点向其连边,费用0,流量K,这个点向Y部每个点连边,费用0,流量1,最这个图跑最小费用最大流,如果满流就是存在解,反之不存在,最小费用的相反数就是可以获得的最大能量。
简要解释一下对于最多只有K条路径的这个条件的实现证明。对于最小路径覆盖的模型中,我们将一个点拆成两个点,一个表示以这个点为起点,一个表示以这个点为终点,如果一个点到另一个点有边,那么我们从表示这个点为起点的点连向表示另个个为终点的点,这样就建成了一个二分图,我们求得该图的最大匹配后,在所有表示一个点为终点的点中,如果这个点没有被匹配,那么说明一定需要一条以这个点为起点的路径,所以最小路径覆盖等于n-最大匹配。理解了这个,那么我们要解决最多K条路径能否实现覆盖这个问题,就很显然了。我们再加一个点这个点到原点的容量为K,将这个点向表示终点的所有点连一条容量为1的表边,那么如果满流,说明所有点都被覆盖,且路径数小于等于K。因为作为路径的起点的点数一定小于等于K,才能满流。
而对于题目还要求最多能获得的能量,那么只需要在边中加上费用这个限制,用最小费用最大流即可解决。
最小费用流的代码如下:(第一次写最小费用最大流,可能写得有点挫。。。)
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#define nn 20
#define mod 210
#define inff 0x3fffffff
using namespace std;
typedef long long LL;
int n,m,k;
char tu[nn][nn];
int fuck[nn][nn];
struct node
{
    int st,en,len,cap,next;
}E[210*110];
int p[220],num;
int st,en;
void init()
{
    memset(p,-1,sizeof(p));
    num=0;
}
void add(int st,int en,int len,int cap)
{
    E[num].st=st;
    E[num].en=en;
    E[num].len=len;
    E[num].cap=cap;
    E[num].next=p[st];
    p[st]=num++;

    E[num].st=en;
    E[num].en=st;
    E[num].len=-len;
    E[num].cap=0;
    E[num].next=p[en];
    p[en]=num++;
}
int cost(int x1,int y1,int x2,int y2)
{
    int re=0;
    if(tu[x1][y1]==tu[x2][y2])
        re-=tu[x1][y1]-'0';
    return re+abs(x1-x2)+abs(y1-y2)-1;
}
int dis[220];
bool inque[220];
int que[220];
int pre[220];
bool spfa()
{
    int i;
    int top,pop;
    top=pop=0;
    for(i=0;i<=en;i++)
    {
        inque[i]=false;
        dis[i]=inff;
    }
    dis[0]=0;
    inque[0]=true;
    que[pop++]=0;
    int sta,w;
    while(top!=pop)
    {
        sta=que[top];
        inque[sta]=false;
        top=(top+1)%mod;
        for(i=p[sta];i+1;i=E[i].next)
        {
            if(E[i].cap>0)
            {
//                cout<<E[i].cap<<endl;
//                cout<<E[i].st<<" "<<E[i].en<<endl;
                w=E[i].en;
                if(dis[w]>dis[sta]+E[i].len)
                {
                    dis[w]=dis[sta]+E[i].len;
                    pre[w]=i;
                    if(!inque[w])
                    {
                        inque[w]=true;
                        que[pop]=w;
                        pop=(pop+1)%mod;
                    }
                }
            }
        }
    }
    if(dis[en]==inff)
        return false;
    return true;
}
int fei;
int argument()
{
    int x=en;
    int ix=inff;
    while(x!=st)
    {
        ix=min(ix,E[pre[x]].cap);
        x=E[pre[x]].st;
    }
    x=en;
    while(x!=st)
    {
        E[pre[x]].cap-=ix;
        E[pre[x]^1].cap+=ix;
        fei+=ix*E[pre[x]].len;
        x=E[pre[x]].st;
    }
    return ix;
}
void solve()
{
    int flow=0;
    fei=0;
    while(spfa())
    {
        flow+=argument();
    }
    if(flow==n*m)
        printf("%d\n",-fei);
    else
        puts("-1");
}
int main()
{
    int t,i,j,g;
    st=0,en=204;
    int cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(fuck,0,sizeof(fuck));
        for(i=0;i<n;i++)
        {
            scanf("%s",tu[i]);
        }
        init();
        int ix=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                fuck[i][j]=++ix;
        }
        add(0,201,0,k);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                add(0,fuck[i][j],0,1);
                add(201,n*m+fuck[i][j],0,1);
                add(n*m+fuck[i][j],en,0,1);
//                cout<<cost(i,j,i,j+1)<<endl;
                for(g=j+1;g<m;g++)
                    add(fuck[i][j],fuck[i][g]+n*m,cost(i,j,i,g),1);
                for(g=i+1;g<n;g++)
                    add(fuck[i][j],fuck[g][j]+n*m,cost(i,j,g,j),1);
            }
        }
        printf("Case %d : ",cas++);
        solve();
    }
    return 0;
}


你可能感兴趣的:(ACM,图论,网络流)