Beispiel #1
0
    void Solve()
    {
        var N = F;
        var a = new Point[N];
        var b = new Point[N];

        for (var i = 0; i < N; i++)
        {
            a[i] = new Point(G);
        }
        for (var i = 0; i < N; i++)
        {
            b[i] = new Point(G);
        }
        var g = new Dinic(2 * N + 2);

        for (var i = 0; i < N; i++)
        {
            for (var j = 0; j < N; j++)
            {
                if (a[i].x < b[j].x && a[i].y < b[j].y)
                {
                    g.AddEdge(i + 1, j + N + 1, 1);
                }
            }
        }
        for (var i = 0; i < N; i++)
        {
            g.AddEdge(0, i + 1, 1); g.AddEdge(i + N + 1, 2 * N + 1, 1);
        }
        Console.WriteLine(g.MaxFlow(0, 2 * N + 1));
    }
Beispiel #2
0
    static void Main()
    {
        var N = int.Parse(ReadLine());
        var b = ReadLine().Split().Select(t => - int.Parse(t)).ToArray();
        var g = new Dinic(N + 2);
        var s = 0L;

        for (var i = 0; i < N; i++)
        {
            if (b[i] < 0)
            {
                s -= b[i];
                g.AddEdge(0, i + 1, 0);
                g.AddEdge(i + 1, N + 1, -b[i]);
            }
            else
            {
                g.AddEdge(0, i + 1, b[i]);
                g.AddEdge(i + 1, N + 1, 0);
            }
        }
        for (var i = 1; i <= N; i++)
        {
            for (var j = 2 * i; j <= N; j += i)
            {
                g.AddEdge(i, j, Dinic.INF);
            }
        }
        WriteLine(s - g.MaxFlow(0, N + 1));
    }
Beispiel #3
0
    void Solve()
    {
        var N = F();
        var b = G().Select(x => - x).ToList();
        var g = new Dinic(N + 2);
        var s = 0L;

        for (var i = 0; i < N; i++)
        {
            if (b[i] < 0)
            {
                s -= b[i];
                g.AddEdge(0, i + 1, 0);
                g.AddEdge(i + 1, N + 1, -b[i]);
            }
            else
            {
                g.AddEdge(0, i + 1, b[i]);
                g.AddEdge(i + 1, N + 1, 0);
            }
        }
        for (var i = 1; i <= N; i++)
        {
            for (var j = 2 * i; j <= N; j += i)
            {
                g.AddEdge(i, j, Dinic.INF);
            }
        }
        Console.WriteLine(s - g.MaxFlow(0, N + 1));
    }
Beispiel #4
0
    public void Solve()
    {
        int H = Reader.Int(), W = Reader.Int();
        var grid  = Reader.StringArray(H);
        var dinic = new Dinic(H * W * 2);
        Predicate <char>     OK = c => c == 'o' || c == 'S' || c == 'T';
        Func <int, int, int> InId = (r, c) => r * W + c;
        Func <int, int, int> OutId = (r, c) => H * W + r * W + c;
        int sr = 0, sc = 0, tr = 0, tc = 0;

        for (int r = 0; r < H; r++)
        {
            for (int c = 0; c < W; c++)
            {
                if (OK(grid[r][c]))
                {
                    dinic.AddEdge(InId(r, c), OutId(r, c), 1);
                    if (grid[r][c] == 'S')
                    {
                        sr = r; sc = c;
                    }
                    if (grid[r][c] == 'T')
                    {
                        tr = r; tc = c;
                    }
                    for (int nr = 0; nr < r; nr++)
                    {
                        if (OK(grid[nr][c]))
                        {
                            dinic.AddEdge(OutId(r, c), InId(nr, c), Dinic.INF);
                            dinic.AddEdge(OutId(nr, c), InId(r, c), Dinic.INF);
                        }
                    }
                    for (int nc = 0; nc < c; nc++)
                    {
                        if (OK(grid[r][nc]))
                        {
                            dinic.AddEdge(OutId(r, c), InId(r, nc), Dinic.INF);
                            dinic.AddEdge(OutId(r, nc), InId(r, c), Dinic.INF);
                        }
                    }
                }
            }
        }

        if (sr == tr || sc == tc)
        {
            Console.WriteLine(-1);
        }
        else
        {
            Console.WriteLine(dinic.MaxFlow(OutId(sr, sc), InId(tr, tc)));
        }
    }
Beispiel #5
0
    public void Solve()
    {
        int             H = Reader.Int(), W = Reader.Int();
        var             grid = Reader.StringArray(H);
        Func <int, int> RowId = r => r;
        Func <int, int> ColId = c => H + c;
        int             S = H + W, T = S + 1;
        var             dinic = new Dinic(H + W + 2);

        for (int r = 0; r < H; r++)
        {
            for (int c = 0; c < W; c++)
            {
                if (grid[r][c] == 'o')
                {
                    dinic.AddEdge(RowId(r), ColId(c), 1);
                    dinic.AddEdge(ColId(c), RowId(r), 1);
                }
                else if (grid[r][c] == 'S')
                {
                    dinic.AddEdge(S, RowId(r), Dinic.INF);
                    dinic.AddEdge(S, ColId(c), Dinic.INF);
                }
                else if (grid[r][c] == 'T')
                {
                    dinic.AddEdge(RowId(r), T, Dinic.INF);
                    dinic.AddEdge(ColId(c), T, Dinic.INF);
                }
            }
        }

        int ans = dinic.MaxFlow(S, T);

        Console.WriteLine(ans >= Dinic.INF ? -1 : ans);
    }
            public static void Test(int NumTestCases)
            {
                string        path_running_time = "running_time/Dinc.txt";
                List <string> running_times     = new List <string>();

                for (int test_case = 1; test_case <= NumTestCases; ++test_case)
                {
                    string       path         = "tests/" + test_case.ToString() + ".txt";
                    FileStream   file         = new FileStream(path, FileMode.Open, FileAccess.Read);
                    StreamReader sr           = new StreamReader(file);
                    string[]     line         = sr.ReadLine().Split(" ");
                    int          num_of_nodes = int.Parse(line[0]);
                    int          num_of_edges = int.Parse(line[1]);
                    Dinic        dinic        = new Dinic(num_of_nodes);
                    line = sr.ReadLine().Split(" ");
                    int source = int.Parse(line[0]) - 1;
                    int sink   = int.Parse(line[1]) - 1;
                    for (int i = 0; i < num_of_edges; ++i)
                    {
                        line = sr.ReadLine().Split(" ");
                        int u = int.Parse(line[0]) - 1, v = int.Parse(line[1]) - 1, c = int.Parse(line[2]);
                        dinic.AddEdge(u, v, c);
                    }
                    int       model_answer = int.Parse(sr.ReadLine().Split(" ")[0]);
                    Stopwatch sw           = Stopwatch.StartNew();
                    int       max_flow     = dinic.MaxFlow(source, sink);
                    sw.Stop();
                    if (max_flow == model_answer)
                    {
                        Console.WriteLine("Test #" + test_case + " is sucessfull and took " + sw.ElapsedMilliseconds + " milliseconds");
                        running_times.Add(num_of_nodes + "," + num_of_edges + "," + max_flow + "," + sw.ElapsedMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("Wrong answer on test #" + test_case + ", the right one is " + model_answer);
                        break;
                    }
                }
                File.WriteAllLines(path_running_time, running_times.ToArray());
            }