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