fork download
  1. using static IO;
  2. public class IO {
  3. public static IO Cin = new();
  4. public static StreamReader reader = new(Console.OpenStandardInput());
  5. public static StreamWriter writer = new(Console.OpenStandardOutput());
  6. public static implicit operator string(IO _) => reader.ReadLine();
  7. public static implicit operator char[](IO _) => reader.ReadLine().ToArray();
  8. public static implicit operator int(IO _) => int.Parse(reader.ReadLine());
  9. public static implicit operator double(IO _) => double.Parse(reader.ReadLine());
  10. public static implicit operator string[](IO _) => reader.ReadLine().Split();
  11. public static implicit operator int[](IO _) => Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
  12. public void Deconstruct(out int a, out int b) { int[] r = Cin; (a, b) = (r[0], r[1]); }
  13. public void Deconstruct(out int a, out int b, out int c) { int[] r = Cin; (a, b, c) = (r[0], r[1], r[2]); }
  14. public static IEnumerable<int> MakeRange(int start, int count) => Enumerable.Range(start, count);
  15. public static T[] MakeArray<T>(int count) where T : new() => MakeRange(0, count).Select(_ => new T()).ToArray();
  16. public static object? Cout { set { writer.Write(value); } }
  17. public static object? Coutln { set { writer.WriteLine(value); } }
  18. public static void Main() { Program.Coding(); writer.Flush(); }
  19. }
  20. class Program {
  21. record struct Point(ushort X, ushort Y) {
  22. public readonly int GetIndex(int n) => Y * n + X;
  23. }
  24. public static void Coding() {
  25. checked {
  26. (int n, int blind) = Cin;
  27.  
  28. int source = n * n;
  29. int limiter = source + 1;
  30. int sink = limiter + 1;
  31.  
  32. int[][] eggs = MakeRange(0, n).Select(_ => (int[])Cin).ToArray();
  33. PriorityQueue<(Point a, Point b), int> candidate = new();
  34.  
  35. for (ushort y = 0; y < n; y++) {
  36. for (ushort x = 1; x < n; x++) {
  37. candidate.Enqueue((new((ushort)(x - 1), y), new(x, y)), eggs[y][x - 1] + eggs[y][x]);
  38. }
  39. }
  40. for (ushort x = 0; x < n; x++) {
  41. for (ushort y = 1; y < n; y++) {
  42. candidate.Enqueue((new(x, (ushort)(y - 1)), new(x, y)), eggs[y - 1][x] + eggs[y][x]);
  43. }
  44. candidate.Dequeue();
  45. }
  46.  
  47. FlowGraph graph = new(sink + 1);
  48. for (int y = 0; y < n; y++) {
  49. for (int x = 0; x < n; x++) {
  50. int me = y * n + x;
  51. if ((x + y) % 2 is 0) {
  52. graph.AddLine(limiter, me, 1, -eggs[y][x]);
  53. }
  54. else {
  55. graph.AddLine(me, sink, 1, -eggs[y][x]);
  56. }
  57. }
  58. }
  59. while (candidate.Count > 0) {
  60. var (a, b) = candidate.Dequeue();
  61. if ((a.X + a.Y) % 2 is not 0) (a, b) = (b, a);
  62. graph.AddLine(a.GetIndex(n), b.GetIndex(n), 1, 0);
  63. }
  64.  
  65. graph.AddLine(source, limiter, blind, 0);
  66. _ = graph.GetMaxFlow(source, sink, out int cost);
  67. Cout = eggs.Sum(arr => arr.Sum()) + cost;
  68. }
  69. }
  70. }
  71. class FlowLine {
  72. public int Start { get; }
  73. public int End { get; }
  74. public int Capacity { get; }
  75. public int Flow { get; set; } = 0;
  76. public FlowLine Reverse { get; }
  77. public int Remaining => Capacity - Flow;
  78. public int Cost { get; }
  79. public FlowLine(int start, int end, int capacity, int cost, FlowLine? reverse = null) {
  80. this.Start = start;
  81. this.End = end;
  82. this.Capacity = capacity;
  83. this.Cost = cost;
  84. this.Reverse = reverse ?? new(end, start, 0, -cost, this);
  85. }
  86. }
  87. class FlowGraph {
  88. public FlowGraph(int size) {
  89. graph = MakeArray<List<FlowLine>>(Count = size);
  90. }
  91. public void AddLine(int start, int end, int capacity, int cost) {
  92. FlowLine line = new(start, end, capacity, cost);
  93. graph[start].Add(line);
  94. graph[end].Add(line.Reverse);
  95. }
  96. public int GetMaxFlow(int source, int sink, out int cost) {
  97. int totalFlow = 0;
  98. for (cost = 0; true;) {
  99. FlowLine?[] path = new FlowLine?[Count];
  100. int[] dist = Enumerable.Repeat(int.MaxValue, Count).ToArray();
  101. bool[] inQueue = new bool[Count];
  102. Queue<int> queue = new();
  103. dist[source] = 0;
  104. inQueue[source] = true;
  105. queue.Enqueue(source);
  106. while (queue.Count > 0) {
  107. int node = queue.Dequeue();
  108. inQueue[node] = false;
  109. foreach (var line in graph[node]) {
  110. int nextdist = dist[node] + line.Cost;
  111. if (line.Remaining > 0 && dist[line.End] > nextdist) {
  112. dist[line.End] = nextdist;
  113. path[line.End] = line;
  114. if (inQueue[line.End] is false) {
  115. inQueue[line.End] = true;
  116. queue.Enqueue(line.End);
  117. }
  118. }
  119. }
  120. }
  121. if (path[sink] is not FlowLine sinkLine) break;
  122. int flow = int.MaxValue;
  123. for (FlowLine? line = sinkLine; line is not null; line = path[line.Start]) {
  124. flow = Math.Min(flow, line.Remaining);
  125. }
  126. for (FlowLine? line = sinkLine; line is not null; line = path[line.Start]) {
  127. line.Flow += flow;
  128. line.Reverse.Flow -= flow;
  129. cost += flow * line.Cost;
  130. }
  131. totalFlow += flow;
  132. }
  133. return totalFlow;
  134. }
  135. public int Count { get; }
  136. private readonly List<FlowLine>[] graph;
  137. }
Success #stdin #stdout 0.06s 33740KB
stdin
3 1
2 7 6
9 5 1
4 3 8
stdout
31