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

发布时间:2026/7/17 9:51:23

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/7/17 9:51:23

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

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

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

2026/7/17 9:41:22

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/7/17 9:41:22

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

OpenClaw技能系统:模块化智能代理开发指南

OpenClaw技能系统:模块化智能代理开发指南

2026/7/17 11:11:26

1. OpenClaw技能系统概述OpenClaw作为新一代智能代理平台,其核心能力很大程度上依赖于技能(Skills)系统的灵活配置。这套机制本质上是一个模块化的指令框架,允许开发者通过Markdown文件定义代理的特定行为模式。每个技能都封装在独立的目录中&#xff0c…

自学成才程序员:问题驱动学习与系统化教育的差异分析

自学成才程序员:问题驱动学习与系统化教育的差异分析

2026/7/17 11:11:26

在当今技术快速迭代的时代,你是否曾经遇到过这样的情况:面对一个复杂的技术问题,科班出身的工程师们还在翻阅文档、讨论标准解决方案时,那个靠自学成长起来的同事已经用你从未想过的方式解决了问题? 这种现象在技术圈…

CentOS7上K8s+Rancher高可用集群部署实战拆解

CentOS7上K8s+Rancher高可用集群部署实战拆解

2026/7/17 11:11:26

1. 这不是“一键”,而是把四步压缩成一个脚本:CentOS7上K8sRancher部署的真实逻辑你搜到的标题里写着“一键安装”,点进去却发现要手动敲七八条命令、改三处配置、等五次服务启动——这根本不是一键,是把“一整套操作流程”包装成…

Claude Tag团队AI助手:共享上下文与协作工作流实践指南

Claude Tag团队AI助手:共享上下文与协作工作流实践指南

2026/7/17 11:11:26

在实际企业协作场景中,AI助手如果只能服务个人,价值会大打折扣。真正的挑战在于如何让AI理解团队上下文、参与工作流程、保持记忆连续性,并且能被多人共享使用。Anthropic最新推出的Claude Tag正是瞄准这个痛点,试图让Claude从个人…

魔兽争霸3兼容性修复工具:让经典游戏在现代电脑上完美运行

魔兽争霸3兼容性修复工具:让经典游戏在现代电脑上完美运行

2026/7/17 11:11:26

魔兽争霸3兼容性修复工具:让经典游戏在现代电脑上完美运行 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 还在为魔兽争霸3这款经典游戏在…

实战指南:掌握地理空间AI分析的5个突破性技巧

实战指南:掌握地理空间AI分析的5个突破性技巧

2026/7/17 11:01:26

实战指南:掌握地理空间AI分析的5个突破性技巧 【免费下载链接】geoai GeoAI: Artificial Intelligence for Geospatial Data 项目地址: https://gitcode.com/gh_mirrors/ge/geoai 地理空间人工智能(GeoAI)正在彻底改变我们理解和分析地…

Unity游戏文本翻译架构深度解析:XUnity.AutoTranslator的技术实现与工程实践

Unity游戏文本翻译架构深度解析:XUnity.AutoTranslator的技术实现与工程实践

2026/7/17 10:50:47

Unity游戏文本翻译架构深度解析:XUnity.AutoTranslator的技术实现与工程实践 【免费下载链接】XUnity.AutoTranslator 项目地址: https://gitcode.com/gh_mirrors/xu/XUnity.AutoTranslator XUnity.AutoTranslator作为Unity游戏社区中最成熟的文本翻译解决方…

openEuler Raspberry Pi Kernel设备驱动开发指南:为树莓派硬件添加支持

openEuler Raspberry Pi Kernel设备驱动开发指南:为树莓派硬件添加支持

2026/7/16 14:29:31

openEuler Raspberry Pi Kernel设备驱动开发指南:为树莓派硬件添加支持 【免费下载链接】raspberrypi-kernel It provides openEuler kernel source for Raspberry Pi 项目地址: https://gitcode.com/openeuler/raspberrypi-kernel 前往项目官网免费下载&…

openEuler系统集成测试实战:基于smoke-test套件的环境验证技巧

openEuler系统集成测试实战:基于smoke-test套件的环境验证技巧

2026/7/16 14:18:17

openEuler系统集成测试实战:基于smoke-test套件的环境验证技巧 【免费下载链接】integration-test The repo contains test suits for system integration test 项目地址: https://gitcode.com/openeuler/integration-test 前往项目官网免费下载:…

Cursor终端插件生态避坑指南:23个实测低效插件黑名单,附3个自研轻量替代方案

Cursor终端插件生态避坑指南:23个实测低效插件黑名单,附3个自研轻量替代方案

2026/7/17 0:00:57

更多请点击: https://intelliparadigm.com 第一章:Cursor终端插件生态避坑指南概览 Cursor 作为基于 VS Code 内核构建的 AI 原生编辑器,其终端插件生态虽活跃,但存在兼容性断层、权限策略突变与调试链路断裂等典型风险。开发者常…

ChatGPT写作提示词效率革命:单条提示词响应质量提升3.8倍的关键变量(附可复用提示词矩阵表)

ChatGPT写作提示词效率革命:单条提示词响应质量提升3.8倍的关键变量(附可复用提示词矩阵表)

2026/7/17 0:00:57

更多请点击: https://codechina.net 第一章:ChatGPT写作提示词效率革命:单条提示词响应质量提升3.8倍的关键变量(附可复用提示词矩阵表) 提示词工程已从经验试错迈入变量驱动的科学阶段。实证研究表明,影响…

DeepSeek V4替换Codex底座模型的实践与优化

DeepSeek V4替换Codex底座模型的实践与优化

2026/7/17 0:00:57

1. 为什么选择用DeepSeek V4替换Codex的底座模型去年我在开发一个智能代码补全工具时,发现Codex的默认底座模型在复杂业务逻辑场景下表现不尽如人意。经过多次测试对比,DeepSeek V4在以下几个关键指标上展现出明显优势:代码补全准确率&#x…