hdu4862 Jump 2014多校第一场1002

链接: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): 657    Accepted Submission(s): 281


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
 
何逗之有,做了那么多网络流二分图,这题却只能想到拆点。。。。。

简单题解:就是个最小k路径覆盖,拆点后二分图转换成费用流搞,相比普通的二分图带权覆盖建图,左部分加了一个点,源点连向它,流量为k,费用0,然后向右部分每个点都一连,流量为1,费用0,表示可以从任意节点出发k次,这个联系到二分图的路径覆盖=点数-最大匹配就不能理解了。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
const int maxm=400010;
const int maxn=1005;
struct node{
    int to,next,c,cost;
}edge[maxm];
int head[maxn],tol;
int pre[maxn],dis[maxn];
int load[maxn];//前驱边
int que[maxn];
bool vis[maxn];
int N; //点数
int ans_flow;
void init(int n)
{
    N=n;
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int c,int cost)
{
    edge[tol].to=v;
    edge[tol].c=c;
    edge[tol].cost=cost;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].to=u;
    edge[tol].c=0;
    edge[tol].cost=-cost;
    edge[tol].next=head[v];
    head[v]=tol++;
}
bool spfa(int s,int t)
{
    int rear=0,front=0;
    for(int i=0;i<=N;i++)
    {
        dis[i]=INF;
        vis[i]=false;
        pre[i]=-1;
        load[i]=-1;
    }
    que[rear++]=s;
    vis[s]=true;
    dis[s]=0;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==maxn)
            front=0;
        vis[u]=false; //spfa注意点
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].c&&dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                if(!vis[v])
                {
                    vis[v]=true;
                    que[rear++]=v;
                    if(rear==maxn)
                        rear=0;
                }
                pre[v]=u;
                load[v]=i;
            }
        }
    }
    if(pre[t]==-1)
        return false;
    else
        return true;
}
int mincostflow(int s,int t)
{
    ans_flow=0;
    int ans_cost=0;
    while(spfa(s,t))
    {
        int Min=INF;
        int u=t;
        while(pre[u]!=-1)
        {
            Min=min(edge[load[u]].c,Min);
            u=pre[u];
        }
        u=t;
        while(pre[u]!=-1)
        {
            edge[load[u]].c-=Min;
            edge[load[u]^1].c+=Min;
            u=pre[u];
        }
        ans_cost+=dis[t]*Min;
        ans_flow+=Min;
    }
    return ans_cost;
}
char a[13][13];
int main()
{
    int t,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a[i]+1);
        }
        init(2*n*m+3);  //点数
        int s=0,t=2*n*m+2,ss=2*n*m+1;
        addedge(s,2*n*m+1,k,0);  //控制流量k次
        //二分图最小路径覆盖等于点数减去匹配的数
        //匹配的数即为不加k控制时的最大流
        //如果不加控制k那条边求的最大流是n*m-出发的点
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                addedge(s,(i-1)*m+j,1,0);
                addedge(ss,n*m+(i-1)*m+j,1,0);
                addedge(n*m+(i-1)*m+j,t,1,0);
                for(int k=i+1;k<=n;k++)
                {
                    int temp=k-i-1;   //求最大费用取负值最后结果再取负
                    if(a[i][j]==a[k][j])
                        temp-=(a[i][j]-'0');
                    addedge((i-1)*m+j,n*m+(k-1)*m+j,1,temp);
                }
                for(int k=j+1;k<=m;k++)
                {
                    int temp=k-j-1;
                    if(a[i][j]==a[i][k])
                        temp-=(a[i][j]-'0');
                    addedge((i-1)*m+j,n*m+(i-1)*m+k,1,temp);
                }
            }
        }
        int ans=mincostflow(s,t);
        printf("Case %d : ",++cnt);
        if(ans_flow==n*m)
            printf("%d\n",-1*ans);
        else
            printf("-1\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值