Kimi LeetCode 3594. 所有人渡河所需的最短时间 C语言实现

发布时间:2026/9/30 7:40:42

Kimi    LeetCode 3594. 所有人渡河所需的最短时间 C语言实现
LeetCode 3594. 所有人渡河所需的最短时间 — C 语言实现题目分析这道题是一个状态空间最短路问题。由于人可以来回摆渡状态存在循环无法用普通 DP必须用 Dijkstra 算法。状态定义 (people_mask, dir, stage)- people_mask二进制位为 1 表示该人还在起点营地- dir0 船在起点1 船在对岸- stage当前环境阶段索引0m-1C 语言实现要点C 没有内置优先队列需要手动实现 二叉堆Min-Heapc#include stdio.h#include stdlib.h#include math.h#include string.h#include float.htypedef struct {double cost;int people; // bitmask: 1 means still at start campint dir; // 0 boat at start, 1 boat at destinationint stage; // current stage index} State;// Min-Heap implementationtypedef struct {State *data;int size;int capacity;} MinHeap;MinHeap* createHeap(int capacity) {MinHeap *heap (MinHeap*)malloc(sizeof(MinHeap));heap-data (State*)malloc(sizeof(State) * capacity);heap-size 0;heap-capacity capacity;return heap;}void swap(State *a, State *b) {State tmp *a;*a *b;*b tmp;}void pushHeap(MinHeap *heap, State s) {if (heap-size heap-capacity) {heap-capacity * 2;heap-data (State*)realloc(heap-data, sizeof(State) * heap-capacity);}int i heap-size;heap-data[i] s;// sift upwhile (i 0) {int parent (i - 1) / 2;if (heap-data[parent].cost heap-data[i].cost) break;swap(heap-data[parent], heap-data[i]);i parent;}}State popHeap(MinHeap *heap) {State ret heap-data[0];heap-data[0] heap-data[--heap-size];// sift downint i 0;while (1) {int left 2 * i 1;int right 2 * i 2;int smallest i;if (left heap-size heap-data[left].cost heap-data[smallest].cost)smallest left;if (right heap-size heap-data[right].cost heap-data[smallest].cost)smallest right;if (smallest i) break;swap(heap-data[i], heap-data[smallest]);i smallest;}return ret;}int isEmpty(MinHeap *heap) {return heap-size 0;}void freeHeap(MinHeap *heap) {free(heap-data);free(heap);}// Precompute max_time for each subsetvoid precomputeMaxTime(int n, int *time, double *max_time) {int total 1 n;max_time[0] 0.0;for (int mask 1; mask total; mask) {int mx 0;for (int i 0; i n; i) {if (mask (1 i)) {if (time[i] mx) mx time[i];}}max_time[mask] (double)mx;}}// Precompute valid subsets (at most k people) for each settypedef struct {int **valid_subsets;int *count;int *capacity;} ValidSubsets;ValidSubsets* precomputeValidSubsets(int n, int k) {int total 1 n;ValidSubsets *vs (ValidSubsets*)malloc(sizeof(ValidSubsets));vs-valid_subsets (int**)malloc(sizeof(int*) * total);vs-count (int*)calloc(total, sizeof(int));vs-capacity (int*)malloc(sizeof(int) * total);for (int ppl 0; ppl total; ppl) {vs-capacity[ppl] 4;vs-valid_subsets[ppl] (int*)malloc(sizeof(int) * vs-capacity[ppl]);// Enumerate all subsets of pplint sub ppl;while (1) {int bits 0;int tmp sub;while (tmp) {bits tmp 1;tmp 1;}if (bits k) {if (vs-count[ppl] vs-capacity[ppl]) {vs-capacity[ppl] * 2;vs-valid_subsets[ppl] (int*)realloc(vs-valid_subsets[ppl],sizeof(int) * vs-capacity[ppl]);}vs-valid_subsets[ppl][vs-count[ppl]] sub;}if (sub 0) break;sub (sub - 1) ppl;}}return vs;}void freeValidSubsets(ValidSubsets *vs, int n) {int total 1 n;for (int i 0; i total; i) {free(vs-valid_subsets[i]);}free(vs-valid_subsets);free(vs-count);free(vs-capacity);free(vs);}double minTime(int n, int k, int m, int* time, int timeSize, double* mul, int mulSize) {int full_mask (1 n) - 1;int total_states 1 n;// Precompute max_time for each subsetdouble *max_time (double*)malloc(sizeof(double) * total_states);precomputeMaxTime(n, time, max_time);// Precompute valid subsetsValidSubsets *vs precomputeValidSubsets(n, k);// dist[people_mask][dir][stage]double ***dist (double***)malloc(sizeof(double**) * total_states);for (int i 0; i total_states; i) {dist[i] (double**)malloc(sizeof(double*) * 2);for (int j 0; j 2; j) {dist[i][j] (double*)malloc(sizeof(double) * m);for (int s 0; s m; s) {dist[i][j][s] DBL_MAX;}}}MinHeap *heap createHeap(1024);dist[full_mask][0][0] 0.0;pushHeap(heap, (State){0.0, full_mask, 0, 0});double result -1.0;while (!isEmpty(heap)) {State cur popHeap(heap);int ppl cur.people;int dir cur.dir;int spd cur.stage;if (cur.cost dist[ppl][dir][spd] 1e-9) {continue;}if (dir 1 cur.people 0) {result cur.cost;break;}if (dir 0) {// Boat at start: send a group (at most k people) to destinationfor (int idx 0; idx vs-count[ppl]; idx) {int sub vs-valid_subsets[ppl][idx];if (sub 0) continue;double d max_time[sub] * mul[spd];double new_cost cur.cost d;int new_ppl cur.people ^ sub;int new_spd ((spd (int)floor(d)) % m m) % m;if (new_cost dist[new_ppl][1][new_spd] - 1e-9) {dist[new_ppl][1][new_spd] new_cost;pushHeap(heap, (State){new_cost, new_ppl, 1, new_spd});}}} else {// Boat at destination: send exactly 1 person backint arrived full_mask ^ cur.people;for (int i 0; i n; i) {int mask 1 i;if (arrived mask) {double d max_time[mask] * mul[spd];double new_cost cur.cost d;int new_ppl cur.people | mask;int new_spd ((spd (int)floor(d)) % m m) % m;if (new_cost dist[new_ppl][0][new_spd] - 1e-9) {dist[new_ppl][0][new_spd] new_cost;pushHeap(heap, (State){new_cost, new_ppl, 0, new_spd});}}}}}// CleanupfreeHeap(heap);freeValidSubsets(vs, n);free(max_time);for (int i 0; i total_states; i) {for (int j 0; j 2; j) {free(dist[i][j]);}free(dist[i]);}free(dist);return result;}核心设计组件 说明Min-Heap 手动实现二叉堆支持 push / pop / isEmpty动态扩容max_time[mask] 预处理每个子集的最大 time[i]避免重复计算valid_subsets 预处理每个剩余集合的所有合法子集人数 ≤ k用动态数组存储三维 dist 数组 dist[mask][dir][stage]初始化为 DBL_MAX浮点比较 用 1e-9 容差避免精度问题编译运行bashgcc -o leetcode_3594 leetcode_3594.c -lm./leetcode_3594下载文件: [leetcode_3594.c](sandbox:///mnt/agents/output/leetcode_3594.c)

相关新闻

Libretto:自动修复失败的 Playwright 脚本,免费且开源!

Libretto:自动修复失败的 Playwright 脚本,免费且开源!

2026/9/25 6:00:01

自动修复失败的 Playwright 脚本保留现有的浏览器自动化脚本,当脚本运行失败时,Libretto 会对实时页面进行调查,并在 GitHub 上发起包含代码修复建议的拉取请求。可以开始使用,或者与开发人员交流。OpenLibretto 自动修复 Playwri…

Loud Links性能优化:如何避免音效延迟与资源加载问题

Loud Links性能优化:如何避免音效延迟与资源加载问题

2026/9/5 23:51:30

Loud Links性能优化:如何避免音效延迟与资源加载问题 【免费下载链接】loud-links :sound: A simple tiny Javascript library to add interaction sounds to your website. 项目地址: https://gitcode.com/gh_mirrors/lo/loud-links Loud Links是一款轻量级…

M1芯片安装Asahi Linux的性能优势与实战指南

M1芯片安装Asahi Linux的性能优势与实战指南

2026/9/8 6:27:50

1. 项目背景:M1芯片与Linux的奇妙碰撞 当苹果在2020年推出基于ARM架构的M1芯片时,整个计算机行业都为之震动。这颗5纳米工艺的芯片不仅在能效比上碾压同期x86处理器,其统一内存架构和强大的神经网络引擎更是为移动计算树立了新标杆。但随之而…

CANN/GE ACL数据集缓冲区添加函数

CANN/GE ACL数据集缓冲区添加函数

2026/9/29 22:00:59

aclmdlAddDatasetBuffer 【免费下载链接】ge GE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、Te…

用ffmpeg高效批量调整图片尺寸的实战指南

用ffmpeg高效批量调整图片尺寸的实战指南

2026/9/28 16:01:49

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱

2026/9/28 2:15:29

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱 【免费下载链接】transformers 🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and mu…

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南

2026/9/29 19:20:49

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南 【免费下载链接】rustfs 🚀2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system sup…

Java Integer缓存揭秘:128陷阱原理、避坑与面试全解

Java Integer缓存揭秘:128陷阱原理、避坑与面试全解

2026/9/28 3:58:00

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据

2026/9/28 3:47:14

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据 【免费下载链接】rustfs 🚀2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system supporting mi…

远程协作的工作台整理

远程协作的工作台整理

2026/9/28 16:01:48

远程协作的工作台整理远程协作的核心不是再加一个工具,而是让交接信息足够完整。异步任务要写明目标、输入位置、完成标准和需要决策的人。 工作台的最小配置 将日程、待办、代码和沟通入口收拢到少数固定位置;通知按紧急程度分层。工作台不需要模仿办公…

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

2026/9/28 5:05:21

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能分类:[AI/大模型]细分主题:AI 增强型 CI/CD 流水线自动化与 GitOps 实践:Agent 工作流、工具调用与任务拆解:从原型到生产的验收清单很多团队在尝试用大…

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

2026/9/28 16:01:48

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场分类:[工程技术]细分主题:Kubernetes 生产环境运维与排障实战:可复制的项目复盘模板与决策记录大部分团队的事故复盘报告,最后都变成了躺在 Confluence 或钉…