-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathACPC10D.cs
63 lines (56 loc) · 2.52 KB
/
ACPC10D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Text;
// https://www.spoj.com/problems/ACPC10D/ #dynamic-programming-2d #path-optimization
// Finds the shortest path between top and bottom middle vertices in a 'tri graph.'
public static class ACPC10D
{
// Really similar to BYTESM2. Need to be a little careful to consider all moves,
// not just downward and diagonal, since vertices can have negative values. And we start
// at the top middle, so can't let impossible moves from the top left impact results.
// For example, recursively the answer for the bottom middle vertex is its vertex
// cost plus the minimum cost of getting to any one of the four vertices from which
// we can travel to it. For convenience, just going to consume the input array.
public static int Solve(int rowCount, int[,] vertexCosts)
{
vertexCosts[0, 0] = int.MaxValue; // Can't use this vertex as we start from middle top.
// vertexCosts[0, 1] = vertexCosts[0, 1]; Cost of arriving at start already taken into account.
vertexCosts[0, 2] += vertexCosts[0, 1]; // Only one way to reach top right.
for (int r = 1; r < rowCount; ++r)
{
int upLeftCost = vertexCosts[r - 1, 0];
int upMiddleCost = vertexCosts[r - 1, 1];
int upRightCost = vertexCosts[r - 1, 2];
int bestUpLeftMiddleCost = Math.Min(upLeftCost, upMiddleCost);
int bestUpLeftMiddleRightCost = Math.Min(bestUpLeftMiddleCost, upRightCost);
int bestUpMiddleRightCost = Math.Min(upMiddleCost, upRightCost);
vertexCosts[r, 0] += bestUpLeftMiddleCost;
vertexCosts[r, 1] += Math.Min(bestUpLeftMiddleRightCost, vertexCosts[r, 0]);
vertexCosts[r, 2] += Math.Min(bestUpMiddleRightCost, vertexCosts[r, 1]);
}
return vertexCosts[rowCount - 1, 1];
}
}
public static class Program
{
private static void Main()
{
var output = new StringBuilder();
int[,] vertexCosts = new int[100000, 3];
int testCase = 1;
int rowCount;
while ((rowCount = int.Parse(Console.ReadLine())) != 0)
{
for (int r = 0; r < rowCount; ++r)
{
string[] vertices = Console.ReadLine().Split();
for (int c = 0; c < 3; ++c)
{
vertexCosts[r, c] = int.Parse(vertices[c]);
}
}
output.AppendLine(
$"{testCase++}. {ACPC10D.Solve(rowCount, vertexCosts)}");
}
Console.Write(output);
}
}