POJ 3422 Kaka's Matrix Travels

本文探讨了在矩阵中寻找经过特定次数路径后的最大累计和的优化算法,通过使用最大流和最大费用最大流的概念来解决该问题。具体地,算法首先通过构建有向图来表示矩阵中的路径和其成本,接着利用最大流或最大费用最大流的方法求解最优路径组合,最终得到在指定路径次数后的最大累计和。
Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7191 Accepted: 2874

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.

Input

The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.

Output

The maximum SUM Kaka can obtain after his Kth travel.

Sample Input

3 2
1 2 3
0 2 1
1 4 2

Sample Output

15

Source

POJ Monthly--2007.10.06, Huang, Jinsong

   第一想法是dp+贪心,结果不对,错误举例如下:
4 2
0 0 0 0
0 0 0 0
0 2 0 1
1 0 2 0
如果用贪心,结果是5,显然是不对的,答案应该是6
然后想到了用最大流,结果不对,看了解题报告,原来是最大费用最大流啊
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#define N 1000000
#define M 10000
using namespace std;
int n,k,sta,end;
struct num
{
    int sta,end,fan,next,cap,w;
}a[N];
int b[M],Top,pt[M],dis[M];
bool in[M];
int main()
{
    //freopen("data.in","r",stdin);
    void addeage(int x,int y,int cap,int w,int fan);
    int EK();
    while(scanf("%d %d",&n,&k)!=EOF)
    {
        sta = 0;
        end = n*n*2+1;
        Top = 0;
        memset(b,-1,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int w;
                scanf("%d",&w);
                int x = (i-1)*n+j;
                int y = n*n+((i-1)*n+j);
                addeage(x,y,1,w,Top+1);
                addeage(y,x,0,-w,Top-1);
                addeage(x,y,k-1,0,Top+1);
                addeage(y,x,0,0,Top-1);
            }
        }
        for(int i=1;i<=n-1;i++)
        {
            for(int j=1;j<=n-1;j++)
            {
                int x = (i-1)*n+j+n*n;
                int y = (i-1)*n+j+1;
                addeage(x,y,k,0,Top+1);
                addeage(y,x,0,0,Top-1);
                y = i*n+j;
                addeage(x,y,k,0,Top+1);
                addeage(y,x,0,0,Top-1);
            }
        }
        for(int i=1;i<=n-1;i++)
        {
            int x = (i-1)*n+n+n*n;
            int y = i*n+n;
            addeage(x,y,k,0,Top+1);
            addeage(y,x,0,0,Top-1);
        }
        for(int j=1;j<=n-1;j++)
        {
            int x = (n-1)*n+j+n*n;
            int y = (n-1)*n+j+1;
            addeage(x,y,k,0,Top+1);
            addeage(y,x,0,0,Top-1);
        }
        addeage(0,1,k,0,Top+1);
        addeage(1,0,0,0,Top-1);
        addeage(2*n*n,end,k,0,Top+1);
        addeage(end,2*n*n,0,0,Top-1);
        int ans = EK();
        printf("%d\n",ans);
    }
    return 0;
}
void addeage(int x,int y,int cap,int w,int fan)
{
    a[Top].sta = x;
    a[Top].end = y;
    a[Top].cap = cap;
    a[Top].w = w;
    a[Top].fan = fan;
    a[Top].next = b[x];
    b[x] = Top++;
}
int spfa()
{
    pt[sta] = 0;
    queue<int>que;
    memset(dis,-1,sizeof(dis));
    memset(in,false,sizeof(in));
    que.push(sta);
    in[sta] = true;
    dis[sta] = 0;
    while(!que.empty())
    {
        int x= que.front();
        in[x] = false;
        que.pop();
        for(int i = b[x];i!=-1;i=a[i].next)
        {
            int y = a[i].end;
            int val = a[i].w;
            if(a[i].cap>0&&dis[y]<(dis[x]+val))
            {
                dis[y] = dis[x] + val;
                pt[y] = i;
                if(!in[y])
                {
                    que.push(y);
                    in[y] = true;
                }
            }
        }
    }
    if(dis[end]==-1)
    {
        return 0;
    }else
    {
        return dis[end];
    }
}
int EK()
{
    int res = 0;
    while(true)
    {
        int ans = spfa();
        if(ans==0)
        {
            break;
        }
        res +=ans;
        int x = end;
        while(x!=sta)
        {
            int pos = pt[x];
            a[pos].cap-=1;
            int fan = a[pos].fan;
            a[fan].cap+=1;
            x = a[pos].sta;
        }
    }
    return res;
}



【SCI级别】多策略改进鲸鱼优化算法(HHWOA)和鲸鱼优化算法(WOA)在CEC2017测试集函数F1-F30寻优对比内容概要:本文档主要介绍了一项关于多策略改进鲸鱼优化算法(HHWOA)与标准鲸鱼优化算法(WOA)在CEC2017测试集函数F1-F30上进行寻优性能对比的研究,属于智能优化算法领域的高水平科研工作。文中通过Matlab代码实现算法仿真,重点展示了HHWOA在收敛速度、寻优精度和稳定性方面的优势,体现了多策略改进的有效性。该研究适用于复杂优化问题求解,尤其在工程优化、参数辨识、机器学习超参数调优等领域具有应用潜力。; 适合人群:具备一定算法基础和Matlab编程能力的研究生、科研人员及从事智能优化算法开发与应用的工程技术人员,尤其适合致力于SCI论文写作与算法创新的研究者。; 使用场景及目标:①用于理解鲸鱼优化算法的基本原理及多策略改进思路(如种群初始化、非线性收敛因子、精英反向学习等);②为智能优化算法的性能测试与对比实验提供CEC2017标准测试平台的实现参考;③支撑学术研究中的算法创新与论文复现工作。; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点关注HHWOA的改进策略模块与WOA的差异,通过重复实验验证算法性能,并可将其思想迁移至其他优化算法的改进中,提升科研创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值