結果

問題 No.2604 Initial Motion
ユーザー kakel-sankakel-san
提出日時 2024-02-10 20:52:07
言語 C#
(.NET 8.0.203)
結果
RE  
実行時間 -
コード長 8,289 bytes
コンパイル時間 8,004 ms
コンパイル使用メモリ 168,200 KB
実行使用メモリ 204,936 KB
最終ジャッジ日時 2024-09-28 17:18:05
合計ジャッジ時間 21,910 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
30,720 KB
testcase_01 AC 60 ms
30,848 KB
testcase_02 AC 61 ms
30,704 KB
testcase_03 AC 96 ms
35,568 KB
testcase_04 AC 92 ms
35,456 KB
testcase_05 AC 94 ms
34,944 KB
testcase_06 AC 95 ms
35,200 KB
testcase_07 AC 93 ms
35,072 KB
testcase_08 AC 95 ms
35,456 KB
testcase_09 AC 92 ms
34,944 KB
testcase_10 AC 94 ms
35,440 KB
testcase_11 AC 97 ms
34,944 KB
testcase_12 AC 97 ms
35,072 KB
testcase_13 AC 614 ms
57,600 KB
testcase_14 AC 307 ms
57,344 KB
testcase_15 AC 247 ms
56,192 KB
testcase_16 AC 396 ms
57,600 KB
testcase_17 AC 763 ms
57,728 KB
testcase_18 AC 695 ms
57,728 KB
testcase_19 AC 689 ms
57,968 KB
testcase_20 AC 377 ms
57,344 KB
testcase_21 AC 313 ms
57,216 KB
testcase_22 AC 663 ms
57,972 KB
testcase_23 AC 470 ms
57,216 KB
testcase_24 AC 437 ms
57,984 KB
testcase_25 AC 341 ms
57,980 KB
testcase_26 AC 504 ms
57,344 KB
testcase_27 AC 296 ms
56,436 KB
testcase_28 AC 353 ms
57,344 KB
testcase_29 AC 600 ms
57,600 KB
testcase_30 AC 316 ms
57,216 KB
testcase_31 AC 381 ms
57,600 KB
testcase_32 AC 310 ms
57,468 KB
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 AC 72 ms
33,280 KB
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (97 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (k, n, m) = (c[0], c[1], c[2]);
        var a = NList;
        var b = NList;
        var map = NArr(m);

        var hnum = new int[n];
        foreach (var ai in a) ++hnum[ai - 1];
        var g = new MinCostFlowGraph(n + 2);
        for (var i = 0; i < n; ++i)
        {
            g.AddEdge(n, i, hnum[i], 0);
            g.AddEdge(i, n + 1, b[i], 0);
        }
        foreach (var edge in map)
        {
            g.AddEdge(edge[0] - 1, edge[1] - 1, k, edge[2]);
            g.AddEdge(edge[1] - 1, edge[0] - 1, k, edge[2]);
        }
        var ans = g.Flow(n, n + 1);
        WriteLine(ans.cost);
    }
    class MinCostFlowGraph
    {
        public class Edge
        {
            public int To;
            public int Rev;
            public long Cap;
            public long Flow;
            public long Cost;
            public Edge(int to, int rev, long cap, long cost)
            {
                To = to;
                Rev = rev;
                Cap = cap;
                Cost = cost;
            }
            public Edge(int to, int rev, long cap, long flow, long cost)
            {
                To = to;
                Rev = rev;
                Cap = cap;
                Flow = flow;
                Cost = cost;
            }
        }
        int size;
        List<(int from, int to)> pos;
        List<Edge>[] g;
        public MinCostFlowGraph(int size)
        {
            this.size = size;
            pos = new List<(int from, int cnt)>();
            g = new List<Edge>[size];
            for (var i = 0; i < g.Length; ++i) g[i] = new List<Edge>();
        }
        public int AddEdge(int from, int to, int cap, long cost)
        {
            var m = pos.Count;
            pos.Add((from, g[from].Count));
            g[from].Add(new Edge(to, g[to].Count, cap, cost));
            g[to].Add(new Edge(from, g[from].Count - 1, 0, -cost));
            return m;
        }
        public Edge GetEdge(int i)
        {
            var _e = g[pos[i].from][pos[i].to];
            var _re = g[_e.To][_e.Rev];
            return new Edge(pos[i].from, _e.To, _e.Cap + _re.Cap, _re.Cap, _e.Cost);
        }
        public List<Edge> GetEdges()
        {
            var res = new List<Edge>(pos.Count);
            for (var i = 0; i < pos.Count; ++i)
            {
                res.Add(GetEdge(i));
            }
            return res;
        }
        public (long cap, long cost) Flow(int s, int t)
        {
            return Flow(s, t, long.MaxValue);
        }
        public (long cap, long cost) Flow(int s, int t, long limit)
        {
            return Slope(s, t, limit).Last();
        }
        public List<(long cap, long cost)> Slope(int s, int t)
        {
            return Slope(s, t, int.MaxValue);
        }
        public List<(long cap, long cost)> Slope(int s, int t, long limit)
        {
            var dual = new long[size];
            var dist = new long[size];
            var pv = new int[size];
            var pe = new int[size];
            var vis = new bool[size];
            bool DualRef()
            {
                for (var i = 0; i < size; ++i)
                {
                    dist[i] = long.MaxValue;
                    pv[i] = -1;
                    pe[i] = -1;
                    vis[i] = false;
                }
                var q = new PriorityQueue<Q>(size, false);
                q.Enqueue(new Q(0, s));
                dist[s] = 0;
                while (q.Count > 0)
                {
                    var cur = q.Dequeue();
                    var v = cur.To;
                    if (vis[v]) continue;
                    vis[v] = true;
                    if (v == t) break;
                    for (var i = 0; i < g[v].Count; ++i)
                    {
                        var e = g[v][i];
                        if (vis[e.To] || e.Cap == 0) continue;
                        var cost = e.Cost - dual[e.To] + dual[v];
                        if (dist[e.To] - dist[v] > cost)
                        {
                            dist[e.To] = dist[v] + cost;
                            pv[e.To] = v;
                            pe[e.To] = i;
                            q.Enqueue(new Q(dist[e.To], e.To));
                        }
                    }
                }
                if (!vis[t]) return false;
                for (var v = 0; v < size; ++v)
                {
                    if (!vis[v]) continue;
                    dual[v] -= dist[t] - dist[v];
                }
                return true;
            }
            var flow = 0L;
            var cost = 0L;
            var prevCost = -1L;
            var res = new List<(long cap, long cost)>();
            res.Add((flow, cost));
            while (flow < limit)
            {
                if (!DualRef()) break;
                var c = limit - flow;
                for (var v = t; v != s; v = pv[v])
                {
                    c = Math.Min(c, g[pv[v]][pe[v]].Cap);
                }
                for (var v = t; v != s; v = pv[v])
                {
                    var e = g[pv[v]][pe[v]];
                    e.Cap -= c;
                    g[v][e.Rev].Cap += c;
                }
                var d = -dual[s];
                flow += c;
                cost += c * d;
                if (prevCost == d) res.RemoveAt(res.Count - 1);
                res.Add((flow, cost));
                prevCost = cost;
            }
            return res;
        }
        class Q : IComparable<Q>
        {
            public long Cost;
            public int To;
            public Q (long cost, int to)
            {
                Cost = cost;
                To = to;
            }
            public int CompareTo(Q b)
            {
                return Cost.CompareTo(b.Cost);
            }
        }
    }
    class PriorityQueue<T> where T : IComparable<T>
    {
        public T[] List;
        public int Count;
        bool IsTopMax;
        public PriorityQueue(int count, bool isTopMax)
        {
            IsTopMax = isTopMax;
            List = new T[Math.Max(128, count)];
        }
        public void Enqueue(T value)
        {
            if (Count == List.Length)
            {
                var newlist = new T[List.Length * 2];
                for (var i = 0; i < List.Length; ++i) newlist[i] = List[i];
                List = newlist;
            }
            var pos = Count;
            List[pos] = value;
            ++Count;
            while (pos > 0)
            {
                var parent = (pos - 1) / 2;
                if (Calc(List[parent], List[pos], true)) break;
                Swap(parent, pos);
                pos = parent;
            }
        }
        public T Dequeue()
        {
            --Count;
            Swap(0, Count);
            var pos = 0;
            while (true)
            {
                var child = pos * 2 + 1;
                if (child >= Count) break;
                if (child + 1 < Count && Calc(List[child + 1], List[child], false)) ++child;
                if (Calc(List[pos], List[child], true)) break;
                Swap(pos, child);
                pos = child;
            }
            return List[Count];
        }
        bool Calc(T a, T b, bool equalVal)
        {
            var ret = a.CompareTo(b);
            if (ret == 0 && equalVal) return true;
            return IsTopMax ? ret > 0 : ret < 0;
        }
        void Swap(int a, int b)
        {
            var tmp = List[a];
            List[a] = List[b];
            List[b] = tmp;
        }
    }
}
0