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. if (candidate.Count > 64) candidate.Dequeue();
  39. }
  40. }
  41. for (ushort x = 0; x < n; x++) {
  42. for (ushort y = 1; y < n; y++) {
  43. candidate.Enqueue((new(x, (ushort)(y - 1)), new(x, y)), eggs[y - 1][x] + eggs[y][x]);
  44. if (candidate.Count > 64) candidate.Dequeue();
  45. }
  46. }
  47.  
  48. FlowGraph graph = new(sink + 1);
  49. HashSet<int> used = new();
  50. while (candidate.Count > 0) {
  51. var (a, b) = candidate.Dequeue();
  52. if ((a.X + a.Y) % 2 is not 0) (a, b) = (b, a);
  53.  
  54. int ai = a.GetIndex(n);
  55. int bi = b.GetIndex(n);
  56. graph.AddLine(ai, bi, 1, 0);
  57. used.Add(ai);
  58. used.Add(bi);
  59. }
  60.  
  61. for (int y = 0; y < n; y++) {
  62. for (int x = 0; x < n; x++) {
  63. int me = y * n + x;
  64. if (used.Contains(me) is false) continue;
  65. if ((x + y) % 2 is 0) {
  66. graph.AddLine(limiter, me, 1, -eggs[y][x]);
  67. }
  68. else {
  69. graph.AddLine(me, sink, 1, -eggs[y][x]);
  70. }
  71. }
  72. }
  73.  
  74. graph.AddLine(source, limiter, blind, 0);
  75. _ = graph.GetMaxFlow(source, sink, out int cost);
  76. Cout = eggs.Sum(arr => arr.Sum()) + cost;
  77. }
  78. }
  79. }
  80. class FlowLine {
  81. public int Start { get; }
  82. public int End { get; }
  83. public int Capacity { get; }
  84. public int Flow { get; set; } = 0;
  85. public FlowLine Reverse { get; }
  86. public int Remaining => Capacity - Flow;
  87. public int Cost { get; }
  88. public FlowLine(int start, int end, int capacity, int cost, FlowLine? reverse = null) {
  89. this.Start = start;
  90. this.End = end;
  91. this.Capacity = capacity;
  92. this.Cost = cost;
  93. this.Reverse = reverse ?? new(end, start, 0, -cost, this);
  94. }
  95. }
  96. class FlowGraph {
  97. public FlowGraph(int size) {
  98. graph = MakeArray<List<FlowLine>>(Count = size);
  99. }
  100. public void AddLine(int start, int end, int capacity, int cost) {
  101. FlowLine line = new(start, end, capacity, cost);
  102. graph[start].Add(line);
  103. graph[end].Add(line.Reverse);
  104. }
  105. public int GetMaxFlow(int source, int sink, out int cost) {
  106. int totalFlow = 0;
  107. for (cost = 0; true;) {
  108. FlowLine?[] path = new FlowLine?[Count];
  109. int[] dist = Enumerable.Repeat(int.MaxValue, Count).ToArray();
  110. bool[] inQueue = new bool[Count];
  111. Queue<int> queue = new();
  112. dist[source] = 0;
  113. inQueue[source] = true;
  114. queue.Enqueue(source);
  115. while (queue.Count > 0) {
  116. int node = queue.Dequeue();
  117. inQueue[node] = false;
  118. foreach (var line in graph[node]) {
  119. int nextdist = dist[node] + line.Cost;
  120. if (line.Remaining > 0 && dist[line.End] > nextdist) {
  121. dist[line.End] = nextdist;
  122. path[line.End] = line;
  123. if (inQueue[line.End] is false) {
  124. inQueue[line.End] = true;
  125. queue.Enqueue(line.End);
  126. }
  127. }
  128. }
  129. }
  130. if (path[sink] is not FlowLine sinkLine) break;
  131. int flow = int.MaxValue;
  132. for (FlowLine? line = sinkLine; line is not null; line = path[line.Start]) {
  133. flow = Math.Min(flow, line.Remaining);
  134. }
  135. for (FlowLine? line = sinkLine; line is not null; line = path[line.Start]) {
  136. line.Flow += flow;
  137. line.Reverse.Flow -= flow;
  138. cost += flow * line.Cost;
  139. }
  140. totalFlow += flow;
  141. }
  142. return totalFlow;
  143. }
  144. public int Count { get; }
  145. private readonly List<FlowLine>[] graph;
  146. }
Success #stdin #stdout 0.08s 29788KB
stdin
4 2
1 2 4 0
4 0 5 4
0 3 5 1
1 0 4 1
stdout
17