Codeforces Round #523 (Div. 2) E.Politics

32 篇文章 0 订阅
14 篇文章 0 订阅
Codeforces传送门
洛谷传送门

题目描述

There are n n n cities in the country.

Two candidates are fighting for the post of the President. The elections are set in the future, and both candidates have already planned how they are going to connect the cities with roads. Both plans will connect all cities using n − 1 n - 1 n1 roads only. That is, each plan can be viewed as a tree. Both of the candidates had also specified their choice of the capital among n n n cities ( x x x for the first candidate and y y y for the second candidate), which may or may not be same.

Each city has a potential of building a port (one city can have at most one port). Building a port in i i i -th city brings a i a_i ai amount of money. However, each candidate has his specific demands. The demands are of the form:

  • k k k x x x , which means that the candidate wants to build exactly x x x ports in the subtree of the k k k -th city of his tree (the tree is rooted at the capital of his choice).

Find out the maximum revenue that can be gained while fulfilling all demands of both candidates, or print − 1 -1 1 if it is not possible to do.

It is additionally guaranteed, that each candidate has specified the port demands for the capital of his choice.

输入输出格式

输入格式:

The first line contains integers n n n , x x x and y y y ( 1 ≤ n ≤ 500 1 \le n \le 500 1n500, 1 ≤ x , y ≤ n 1 \le x, y \le n 1x,yn) — the number of cities, the capital of the first candidate and the capital of the second candidate respectively.

Next line contains integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an( 1 ≤ a i ≤ 100   000 1 \le a_i \le 100\,000 1ai100000 ) — the revenue gained if the port is constructed in the corresponding city.

Each of the next n − 1 n - 1 n1 lines contains integers u i u_i ui and $ v_i$ ( 1 ≤ u i , v i ≤ n 1 \le u_i, v_i \le n 1ui,vin, u i ≠ v i u_i \ne v_i ui̸=vi ), denoting edges between cities in the tree of the first candidate.

Each of the next n − 1 n - 1 n1 lines contains integers u i ′ u'_i ui and v i ′ v'_i vi ( 1 ≤ u i ′ , v i ′ ≤ n 1 \le u'_i, v'_i \le n 1ui,vin, u i ′ ≠ v i ′ u'_i \ne v'_i ui̸=vi ), denoting edges between cities in the tree of the second candidate.

Next line contains an integer q 1 q_1 q1 ( 1 ≤ q 1 ≤ n 1 \le q_1 \le n 1q1n), denoting the number of demands of the first candidate.

Each of the next q 1 q_1 q1 lines contains two integers k k k and x x x ( 1 ≤ k ≤ n 1 \le k \le n 1kn , 1 ≤ x ≤ n 1 \le x \le n 1xn ) — the city number and the number of ports in its subtree.

Next line contains an integer q 2 q_2 q2 ( 1 ≤ q 2 ≤ n 1 \le q_2 \le n 1q2n ), denoting the number of demands of the second candidate.

Each of the next q 2 q_2 q2 lines contain two integers k k k and x x x ( 1 ≤ k ≤ n 1 \le k \le n 1kn , 1 ≤ x ≤ n 1 \le x \le n 1xn ) — the city number and the number of ports in its subtree.

It is guaranteed, that given edges correspond to valid trees, each candidate has given demand about each city at most once and that each candidate has specified the port demands for the capital of his choice. That is, the city x x x is always given in demands of the first candidate and city y y y is always given in the demands of the second candidate.

输出格式:

Print exactly one integer — the maximum possible revenue that can be gained, while satisfying demands of both candidates, or -1 if it is not possible to satisfy all of the demands.

输入输出样例

输入样例#1:
4 1 2
1 2 3 4
1 2
1 3
3 4
1 2
2 3
1 4
2
1 3
4 1
1
2 3
输出样例#1:
9
输入样例#2:
5 1 1
3 99 99 100 2
1 2
1 3
3 4
3 5
1 3
1 2
2 4
2 5
2
1 2
3 1
2
1 2
2 1
输出样例#2:
198
输入样例#3:
4 1 2
1 2 3 4
1 2
1 3
3 4
2 1
2 4
4 3
1
1 4
2
4 1
2 4
输出样例#3:
-1

说明

In the first example, it is optimal to build ports in cities 2 2 2 , 3 3 3 and 4 4 4 , which fulfills all demands of both candidates and gives revenue equal to 2 + 3 + 4 = 9 2 + 3 + 4 = 9 2+3+4=9 .

In the second example, it is optimal to build ports in cities 2 2 2 and 3 3 3 , which fulfills all demands of both candidates and gives revenue equal to 99 + 99 = 198 99 + 99 = 198 99+99=198 .

In the third example, it is not possible to build ports in such way, that all demands of both candidates are specified, hence the answer is − 1 -1 1.

解题分析

这道题费用流很明显, 主要是怎么建图。

我们可以直接按题目要求建出两棵树, 每个点分成两个节点, 中间连下界为 l i m i lim_i limi, 上界为 l i m i lim_i limi(如果没有限制直接连成 I N F INF INF), 然后两棵树对应节点连流量为 1 1 1, 费用为收益的边, 跑最大费用可行流, 判断是否满流即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 2050
#define INF 1e8
template <class C>
IN void in(C &x)
{
    x = 0; R char c = gc;
    for (; !isdigit(c); c = gc);
    for (;  isdigit(c); c = gc)
    x = (x << 1) + (x << 3) + c - 48;
}
template <class C> IN C max(C a, C b) {return a > b ? a : b;}
template <class C> IN C min(C a, C b) {return a < b ? a : b;}
bool inq[MX];
int S, T, n, q1, q2, ct, cnt, flow, sum, x, y, tot, tar;
int head[MX], h1[MX], h2[MX], dis[MX], pre[MX], del[MX], deg[MX];
int lim1[MX], lim2[MX], pos1[MX], pos2[MX], val[MX];
struct Edge {int to, fl, len, nex;} edge[MX * 10];
struct EDGE {int to, nex;} e[MX * 10];
IN void add(R int from, R int to, R int fl, R int cost)
{
    edge[++cnt] = {to, fl, cost, head[from]}, head[from] = cnt;
    edge[++cnt] = {from, 0, -cost, head[to]}, head[to] = cnt;
}
IN void add1(R int from, R int to) {e[++ct] = {to, h1[from]}, h1[from] = ct;}
IN void add2(R int from, R int to) {e[++ct] = {to, h2[from]}, h2[from] = ct;}
namespace MCMF
{
    std::queue <int> q;
    IN bool SPFA()
    {
        std::memset(dis, 63, sizeof(dis));
        dis[S] = 0, del[S] = INF, q.push(S);
        R int now;
        W (!q.empty())
        {
            now = q.front(); q.pop();
            for (R int i = head[now]; ~i; i = edge[i].nex)
            {
                if (dis[edge[i].to] > dis[now] + edge[i].len && edge[i].fl)
                {
                    dis[edge[i].to] = dis[now] + edge[i].len;
                    pre[edge[i].to] = i;
                    del[edge[i].to] = min(del[now], edge[i].fl);
                    if (!inq[edge[i].to]) inq[edge[i].to] = true, q.push(edge[i].to);
                }
            }
            inq[now] = false;
        }
        return dis[T] < INF;
    }
    IN void update()
    {
        R int now = T, pr;
        sum -= del[T] * dis[T], flow += del[T];
        W (now ^ S)
        {
            pr = pre[now];
            edge[pr].fl -= del[T], edge[pr ^ 1].fl += del[T];
            now = edge[pr ^ 1].to;
        }
    }
    IN void init() {W (SPFA()) update();}
}
void DFS1(R int now, R int fa)
{
    if (lim1[now]) deg[now] -= lim1[now], deg[now + n] += lim1[now];
    else add(now, now + n, INF, 0);
    add(now + n, now + tot + n, 1, val[now]);
    for (R int i = h1[now]; i; i = e[i].nex)
    {
        if (e[i].to == fa) continue;
        add(now + n, e[i].to, INF, 0);
        DFS1(e[i].to, now);
    }
}
void DFS2(R int now, R int fa)
{
    if (lim2[now]) deg[now + tot + n] -= lim2[now], deg[now + tot] += lim2[now];
    else add(now + tot + n, now + tot, INF, 0);
    for (R int i = h2[now]; i; i = e[i].nex)
    {
        if (e[i].to == fa) continue;
        add(e[i].to + tot, now + tot + n, INF, 0);
        DFS2(e[i].to, now);
    }
}
int main(void)
{
    int a, b;
    std::memset(head, cnt = -1, sizeof(head));
    in(n), in(x), in(y); tot = n << 1;
    for (R int i = 1; i <= n; ++i) in(val[i]), val[i] = -val[i];
    for (R int i = 1; i <  n; ++i)
    {
        in(a), in(b);
        add1(a, b), add1(b, a);
    }
    for (R int i = 1; i < n; ++i)
    {
        in(a), in(b);
        add2(a, b), add2(b, a);
    }
    in(q1);
    for (R int i = 1; i <= q1; ++i)
    {
        in(a), in(b);
        lim1[a] = b;
    }
    in(q2);
    for (R int i = 1; i <= q2; ++i)
    {
        in(a), in(b);
        lim2[a] = b;
    }
    S = 4 * n + 1, T = 4 * n + 2;
    DFS1(x, 0); DFS2(y, 0);
    for (R int i = 1; i <= 4 * n; ++i)
    {
        if (deg[i] > 0) add(S, i, deg[i], 0), tar += deg[i];
        else add(i, T, -deg[i], 0);
    }
    add(y + tot, x, INF, 0);
    MCMF::init();
    printf("%d", flow == tar ? sum : -1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值