sdut2414 An interesting game 费用流

MTK Tunning ISP 模块面试总结 ISP面试问答 阅读详情

An interesting game

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Xiao Ming recently designs a little game, in front of player there are N small hillsides put in order, now Xiao Ming wants to increase some hillsides to block the player, so he prepared another M hillsides, but he does not hope it will be too difficult,so only K in M hillsides are selected to add at most. Paying attention to the original N hillsides, between each two can add only one hillside. Xiao Ming expects players from the starting place to reach the destination in turn and passes all the hillsides to make his distance travelled longest. Please help Xiao Ming how to add the hillsides that he prepared. Note: The distance between two hillsides is the absolute value of their height difference.

输入

The first line of input is T, (1 <= T <= 100) the number of test cases. Each test case starts with three integers N,M,K (2 <= N <= 1000, 1 <= M <= 1000, 1 <= K <= M and 1 <= K < N), which means that the number of original hillsides, the number of hillsides Xiao Ming prepared and The number of most Xiao Ming can choose from he prepared. Then follow two lines, the first line contains N integers Xi (0 <= Xi <= 30), denoting the height of each original hillside, Note: The first integer is player's starting place and the last integer is player's destination. The second line contains M integers Yi (0 <= Yi <= 30), denoting the height of prepared each hillsides.

输出

For every test case, you should output "Case k: " first in a single line, where k indicates the case number and starts from 1. Then print the distance player can travel longest.

示例输入

32 1 16 982 1 16 9153 2 15 9 1521 22

示例输出

Case 1: 3Case 2: 15Case 3: 36

  第三届省赛的一道题,现在已经有N座山,还可以从另外M座里选最多K个插到这些山中间,每两个山中间最多插一个,山之间可以得到的权值是高度差,问从第1座山到最后一座得到的最大权值是多少。

  求最大权值相当于把权值取负数建图求最小费用,难就难在在建图上。这里因为原来N座山是固定的,所以至少获得的权值是这些山高度差的和,我们先把这个记录下来,于是建图只用求新增加的权值。首先起点s连向一个中点mid,容量为K花费为0,保证了最多添加K座山,接下来注意到山的高度最大才30,把高度建成点(只用建30个,而并不是把M个山都建,这样节省了时间),mid和高度点连边,容量为M座山中这个高度山的个数,费用为0。再用N个点表示初始的N座山,把高度的点和2-N的山连起来,容量为1,费用为-(abs(i-a[j])+abs(i-a[j-1])-abs(a[j]-a[j-1]),a[j]为初始第j座山的高度,因为我们只算新增的权值,所以要减去一开始就有的abs(a[j]-a[j-1]),走了这条边就说明往j-1和j中间插了一座高度为i的山,最后把2-N的山和终点连边,容量1,费用0,保证了初始每两座山中间只能插一座新的。

  最后答案是求的最小费用的负数加上初始权值。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;

 const int MAXN=1100;

int T,N,M,K;
int a[MAXN],cnt[MAXN];

struct Edge{
    int from,to,cap,flow,cost;
};
struct MCMF{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[MAXN];
    int inq[MAXN];  //是否在队列中
    int d[MAXN];    //bellman
    int p[MAXN];    //上一条弧
    int a[MAXN];    //增广量

    void init(int n){
        this->n=n;
        for(int i=0;i<n;i++) G[i].clear();
        edges.clear();
    }
    void add_edge(int from,int to,int cap,int cost){
        edges.push_back((Edge){from,to,cap,0,cost});
        edges.push_back((Edge){to,from,0,0,-cost});
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    bool bellman(int s,int t,int& flow,int& cost){
        for(int i=0;i<n;i++) d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0;
        inq[s]=1;
        p[s]=0;
        a[s]=INF;
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            inq[u]=0;
            int len=G[u].size();
            for(int i=0;i<len;i++){
                Edge& e=edges[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){
                        q.push(e.to);
                        inq[e.to]=1;
                    }
                }
            }
        }
        if(d[t]==INF) return false;
        if(d[t]>0) return false;
        flow+=a[t];
        cost+=d[t]*a[t];
        int u=t;
        while(u!=s){
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
            u=edges[p[u]].from;
        }
        return true;
    }
    int mincost(int s,int t){
        int flow=0;
        int cost=0;
        while(bellman(s,t,flow,cost));
        return cost;
    }
}g;

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&N,&M,&K);
        int ans=0;
        int s=N+31,mid=N+32,t=N+33;
        g.init(N+34);
        for(int i=1;i<=N;i++){
            scanf("%d",&a[i]);
            if(i>1) ans+=abs(a[i]-a[i-1]);
        }
        memset(cnt,0,sizeof(cnt));
        int tmp;
        for(int i=1;i<=M;i++){
            scanf("%d",&tmp);
            cnt[tmp]++;
        }
        g.add_edge(s,mid,K,0);
        for(int i=0;i<=30;i++) if(cnt[i]){
            g.add_edge(mid,i,cnt[i],0);
            for(int j=2;j<=N;j++) g.add_edge(i,30+j,1,-(abs(i-a[j])+abs(i-a[j-1])-abs(a[j]-a[j-1])));
        }
        for(int i=2;i<=N;i++) g.add_edge(30+i,t,1,0);
        printf("Case %d: %d\n",++cas,-g.mincost(s,t)+ans);
    }
    return 0;
}



翁恺-C语言程序设计 PTA习题集 代码练习题 阅读详情

相关推荐

信息安全与技术——(十一)恶意代码检测与防范技术

恶意代码(Malicious Code )或恶意软件(Malware )主要是指以危害信息的安全等不良意图为目的的程序,它们一般潜伏在受害计算机系统中实施破坏或窃取信息。

m0_63853448的博客 5991

HDUT-2414-An interesting game(最小费用最大流)

An interesting game Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Xiao Ming recently designs a little game, in front of player there are N small hillsides put in order, now Xia

不会吃鱼的喵 719

R语言笔记十:独立性检验函数

1.独立性检验函数        独立性检验是根据频数信息判断两类因子彼此相关或相互独立的假设检验。所谓独立性就是指变量之间是独立的,没有关系。 独立性检验算法:卡方检验、Fisher检验、Cochran-Mantel-Haenszel检验 2.假设检验 假设检验(Hypothesis Testing)是数理统计学中根据一定假设条件由样本推断总体的一种方法。 原假设——没有发生 备择假设——发生了 具体做法:根据问题的需要对所研究的总体作

weixin_44384631的博客 4111

【SDUTOJ 2414】An interesting game(最小费用最大流)

【SDUTOJ 2414】An interesting game(最小费用最大流) An interesting game Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Xiao Ming recently designs a little game, in front of player

ACMer' 2097

EVE-NG 新增 qcow2 镜像教程

本文介绍了如何在 EVE-NG 网络仿真平台中添加 QCOW2 镜像并配置 YML 文件。首先,用户需要了解 EVE-NG 的目录结构,上传并重命名 QCOW2 镜像,然后设置文件权限并刷新界面以识别设备。接着,讲解了 YML 文件的作用,它用于定义设备的CPU、内存、网卡等参数,并给出了配置示例。若有多个设备版本,可以通过复制并重命名 YML 文件使其独立显示。最后,通过检查 QEMU 进程和日志验证 YML 配置是否生效。总结部分强调了 EVE-NG 的自动识别机制以及独立管理设备版本的方式。

qq_41862216的博客 3075

YOLOv5 改进点总览:轻量化、高精度的背后有哪些魔改?

YOLOv5是由Ultralytics团队开发的高效目标检测模型,其核心改进包括:主干网络采用CSPDarknet53提升梯度传播效率;特征融合使用PANet增强小目标识别;输出结构采用解耦头设计优化定位和分类任务。此外,YOLOv5通过CIoU损失函数、Mosaic数据增强和SimOTA标签分配机制进一步提升性能。模型支持多种推理格式(ONNX/TensorRT等),并具备自动锚框匹配和多尺度训练功能,使其在轻量化(yolov5s仅7MB)与高性能(最高51.2% mAP)间取得平衡,但缺乏正式论文支撑是

guoguozgw的博客 1805

SDUT 2414 An interesting game(最大费用流)

#include<map> #include<queue> #include<cmath> #include<cstdio> #include<stack> #include<iostream> #include<cstring> #include<algorithm> #define LL long long #define inf 0x3f3f3f3f #define eps 1e-8 #def

Grit_ICPC的博客 547

MMRotate 详细使用教程

MMRotate 详细使用教程 做旋转目标检测,怎么可以不知道如何使用MMRotate呢! github链接: https://github.com/open-mmlab/mmrotate 1 安装 创建虚拟环境并激活 conda create -n mmrotate python=3.7 -y conda activate mmrotate 安装 PyTorch 和 torchvision conda install pytorch torchvision -c pytorch 注意:确保

Sylvia_Lan的博客 1万+

【SDUT2414】An interesting game——最小费用

题目描述 Xiao Ming recently designs a little game, in front of player there are N small hillsides put in order, now Xiao Ming wants to increase some hillsides to block the player, so he prepared another M

绝尘 790

YOLO-V8训练自己的数据集之标注样本

四、标注样本 4.1、标注样本概述 4.1.1、样本标注主要方法 4.1.2、标注样本概念 4.1.3、标注样本实现方式 4.1.4、常见标注样本软件 4.2、标注工具LabelImg 4.2.1、LabelImg介绍 4.2.2、LabelImg特点 4.2.3、LabelImg安装方法 4.2.4、LabelImg使用方法 4.3、标注样本实操 4.3.1、安装标注软件Labelimg 4.3.2、Labelimg界面介绍 4.3.3、LabelImg实操过程中常

qq_29633789的博客 662

分布式协作方案:OpenClaw多节点部署与设备互联

主从架构或对等架构服务发现与注册机制负载均衡策略选择分布式任务队列优先级调度失败重试机制WebSocket实时通信数据同步引擎冲突解决策略健康检查机制故障自动转移数据复制与恢复通过分布式部署,OpenClaw可以支撑更大规模的业务场景,提供更稳定可靠的AI助手服务。在下一篇文章中,我们将深入探讨安全与权限管理的精细化策略。

gaoxiang_120的专栏 370

[SDUT 2414] | 山东省第三届省赛 An interesting game | 最小费用最大流

评测地址 题目描述 Xiao Ming recently designs a little game, in front of player there are N small hillsides put in order, now Xiao Ming wants to increase some hillsides to block the player, so he prepared another M hillsides, but he does not hope it will be too dif

PushyTao的博客 242

Arcgis栅格裁剪批处理工具

Arcgis栅格裁剪批处理工具

yilvyangguang520的博客 2173

Dinic+当前弧优化

Dinic算法是不停用BFS构造层次图,然后

7141

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Dijkstra 求最短路 最大流

原来Dijkstra算法不光只能求最短路问题,还能求最大流问题~~~求最短路 最大流" title="Dijkstra 求最短路 最大流"> 最短路问题时,Dijkstra算法的基本思想:   邻接矩阵w[x][y]保存x和y间的距离(别的设为INF,表示不连通,设为-1也行,后来更新加一个if(w[x][y]>=0)),数组d保存通过已经确定的点从起点到每个点的最短路。初始化d[0]=0,

1848

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

网络流 最大流 Edmonds-Karp算法

Edmonds-Karp算法,复杂度O(VE^2)。思想就是找增广路,不断增加流量。在残量(每条边上流量和容量的差)图上找一条每个边权值都为正的路(可以通过BFS,比DFS效率高),这些边权值里的最小值就是这条路可以增加的流量,然后在这条路径上更新流量。再重复找这样的路更新流量,直到找不到这样的路了就说明流量不能再增加了,当前的流就已经是最大流。   同时最大流也是最小割(权值和最小的割集)。设

1842

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Bellman_Ford和Spfa两种方法判断负环

Bellman_Ford是通过N-1次循环求出每个点到原点的最短路的,每次循环遍历所有的边,如果能更新就更新。前面说过为什么N-1次就行(若一个点到起点的最短路需要经过N个点,包括他自己,那么第N次循环就能确定他的最短路)。   最短路一定是不含环的,如果存在负环就根本不存在最短路了。所以这个方法可以用来判断是否有负环,如果循环了N-1次后还能进行松弛操作,说明有负环。Bellman_Ford的

1713

hdu5294 Tricks Device 最短路+最小割 多校联合第一场

Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 66    Accepted Submission(s): 14 Problem Description Innocent Wu follows

1563

uva1416 - Warfare And Logistics 最短路树优化

The army of United Nations launched a new wave of air strikes on terroristforces. The objective of the mission is to reduce enemy's logistical mobility. Each airstrike will destroy a path and therefor

1002

uva1324 - Bring Them There 网络流

By the year 3141, the human civilization has spread all over the galaxy. The special hypertunnels are usedto travel from one star system to another. To use the hypertunnel, you fly to a special locati

816

uva10806 - Dijkstra, Dijkstra. 最小费用流 求两条路权值之和最小

最小费用流是每次在残量图中求出起点到终点的最短路,然后再找出最短路上权值最小的一个边作为增广流量进行增广,更新流量,加上费用(最短路*增广流量)。找最短路用的不带链接表的spfa算法。因为每次增广后都是新流量下的最小费用流,所以最后就是最大流的最小费用。   如果是无向图、有平行边和反向边就比较麻烦了。。在有向图,没有平行边和反向边的情况下,设cap[v][u]=0,cost[v][u]=-co

781

uva10269 - Adventure of Super Mario floyd+dp+spfa

Problem A Adventure of Super Mario Input: Standard Input Output: Standard Output After rescuing the beautiful princess, Super Mario needs to find a way home -- with the princess of course :-

771

uva1306 - The K-League 网络流

Supporters for the professional soccer clubs participating in the K-League, formerly the Korea Professional Soccer League, hold orderly and organized cheering, as did the Red Devils, the official supp

769

uva658 - It's not a Bug, it's a Feature! 状态压缩+隐式图搜索+优先队列的dijkstra

It's not a Bug, it's a Feature!  It is a curious fact that consumers buying a new software product generally do not expect the software to be bug-free. Can you imagine buying a car whose

738

匈牙利算法 二分图

这几个图和定义转自https://www.byvoid.com/blog/hungary/  未盖点:设Vi是图G的一个顶点,如果Vi 不与任意一条属于匹配M的边相关联,就称Vi 是一个未盖点。 交错路:设P是图G的一条路,如果P的任意两条相邻的边一定是一条属于M而另一条不属于M,就称P是一条交错路。 可增广路:两个端点都是未盖点的交错路叫做可增广路。     匈牙

737

uva11354 - Bond 最小瓶颈路+LCA

Once again, James Bond is on his way tosaving the world. Bond's latest mission requires him to travel between severalpairs of cities in a certain country.   The country has N cities(numbered by 1, 2

710
上一篇: 两个向量的夹角(顺时针)
下一篇: uva1347 - Tour DP
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值