Download benchmarks/Algorithms/matrix_chain_multiplication/solution.c from PV-SJTU/PV-Bench: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/datasets/PV-SJTU/PV-Bench/resolve/main/benchmarks/Algorithms/matrix_chain_multiplication/solution.c
- Command line
-
hf download hf://datasets/PV-SJTU/PV-Bench/benchmarks/Algorithms/matrix_chain_multiplication/solution.c
-
curl -L -o solution.c https://huggingface.co/datasets/PV-SJTU/PV-Bench/resolve/main/benchmarks/Algorithms/matrix_chain_multiplication/solution.c
1.07 kB
| int matrixChainMinCost(int *dimensions, int matrix_count) | |
| { | |
| int cost[64]; | |
| int width = matrix_count; | |
| for (int i = 0; i < matrix_count * width; ++i) { | |
| cost[i] = 0; | |
| } | |
| for (int chain_length = 2; | |
| chain_length <= matrix_count; | |
| ++chain_length) { | |
| for (int left = 0; | |
| left + chain_length <= matrix_count; | |
| ++left) { | |
| int right = left + chain_length - 1; | |
| int best = cost[left * width + left] | |
| + cost[(left + 1) * width + right] | |
| + dimensions[left] * dimensions[left + 1] | |
| * dimensions[right + 1]; | |
| for (int split = left + 1; split < right; ++split) { | |
| int candidate = cost[left * width + split] | |
| + cost[(split + 1) * width + right] | |
| + dimensions[left] * dimensions[split + 1] | |
| * dimensions[right + 1]; | |
| if (candidate < best) { | |
| best = candidate; | |
| } | |
| } | |
| cost[left * width + right] = best; | |
| } | |
| } | |
| int result = cost[matrix_count - 1]; | |
| return result; | |
| } | |