hdu 4862 Jump 【最小费用最大流】好题

Jump

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

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<=10M<=10K<=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

 

题目大意:

有一个n*m的矩阵,一共可以进行游戏K次,每次游戏的起点终点都没有限定,从起点向终点走的时候只能向右或者向下走,步数也没有限定,其没走一步的花费为: |x1-x2|+|y1-y2|-1。如果这一步走到的地方和这一步脚下的数字相同,那么获得的价值为:脚下的数字的大小。

问是否K次能够走完所有点,如果可以,输出最大价值,否则输出-1、


思路(最小K路径覆盖模型):


1、建图方式如下:

①设定一个源点,将源点连入各个节点(1-n*m),流量为1,花费为0.

②设定一个汇点,将各个点的拆点(n*m+1-n*m*2)连入汇点 ,流量为1,花费为0.

③将每个点能够走到的点之间连一边(从每个点,连入可以走到的点的拆点),流量为1,花费为(a【i】【j】==a【ii】【jj】?a【i】【j】-(|x1-x2|+|y1-y2|-1));

④再设立一个点q,将源点连入这个q节点,流量为 1,花费为0.再将这个q点连入各个点的拆点,流量为1,花费为0.表示一共可以有k个起点。


2、建好图之后直接跑最长路即可(连续增广)。那么得到的最大流如果==n*m,那么输出最大花费,否则输出-1即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int from;
    int to;
    int w;
    int f;
    int next;
    int num;
}e[1515150];
char a[150][150];
int num[150][150];
int n,m,kk,cont,ss,tt,qq;
int dis[150*150*10];
int pre[150*150*10];
int vis[150*150*10];
int path[150*150*10];
int head[150*150*10];
void add(int from,int to,int f,int w)
{
    /*
    if(f)
    {
        printf("%d %d %d\n",from,to,w);
    }*/
    e[cont].to=to;
    e[cont].f=f;
    e[cont].w=w;
    e[cont].num=cont;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Getmap()
{
    ss=n*m*2+1;
    tt=ss+2;
    qq=tt-1;
    cont=0;
    memset(head,-1,sizeof(head));
    add(ss,qq,kk,0);
    add(qq,ss,0,0);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            add(ss,i*m+j+1,1,0);
            add(i*m+j+1,ss,0,0);
            add(i*m+j+1+n*m,tt,1,0);
            add(tt,i*m+j+1+n*m,0,0);
            add(qq,i*m+j+1+n*m,1,0);
            add(i*m+j+1+n*m,qq,0,0);
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            int y=j+1;
            while(y<m)
            {
                int tmpp=0;
                if(a[i][j]==a[i][y])tmpp=a[i][j]-'0';
                else tmpp=0;
                add(i*m+j+1,i*m+y+1+n*m,1,tmpp-(y-j-1));
                add(i*m+y+1+n*m,i*m+j+1,0,-(tmpp-(y-j-1)));
                y++;
            }
            int x=i+1;
            while(x<n)
            {
                int tmpp=0;
                if(a[i][j]==a[x][j])tmpp=a[i][j]-'0';
                else tmpp=0;
                add(i*m+j+1,x*m+j+1+n*m,1,tmpp-(x-i-1));
                add(x*m+j+1+n*m,i*m+j+1,0,-(tmpp-(x-i-1)));
                x++;
            }
        }
    }
}
int SPFA()
{
    for(int i=0;i<=tt;i++)dis[i]=-0x3f3f3f3f;
    dis[ss]=0;
    memset(vis,0,sizeof(vis));
    vis[ss]=1;
    queue<int >s;s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            int f=e[i].f;
            if(f&&dis[v]<dis[u]+w)
            {
                pre[v]=u;
                path[v]=e[i].num;
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    if(dis[tt]==-0x3f3f3f3f)return 0;
    else return 1;
}
void MCMF()
{
    int ans=0;
    int maxflow=0;
    while(SPFA()==1)
    {
        int minn=0x3f3f3f3f;
        for(int i=tt;i!=ss;i=pre[i])
        {
            minn=min(minn,e[path[i]].f);
        }
        for(int i=tt;i!=ss;i=pre[i])
        {
            e[path[i]].f-=minn;
            e[path[i]^1].f+=minn;
        }
        maxflow+=minn;
        ans+=minn*dis[tt];
    }
    if(maxflow==n*m)
    {
        printf("%d\n",ans);
    }
    else printf("-1\n");
}
int main()
{
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&kk);
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
        }
        Getmap();
        printf("Case %d : ",++kase);
        MCMF();
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值