1. 程式人生 > >CF733F Drivers Dissatisfaction【鏈剖】【最小生成樹應用】

CF733F Drivers Dissatisfaction【鏈剖】【最小生成樹應用】

F. Drivers Dissatisfaction

time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output

In one kingdom there are n cities and m two-way roads. Each road connects a pair of cities, and for each road we know the level of drivers dissatisfaction — the value w

i.

For each road we know the value ci — how many lamziks we should spend to reduce the level of dissatisfaction with this road by one. Thus, to reduce the dissatisfaction with the i-th road by k, we should spend k·ci lamziks. And it is allowed for the dissatisfaction to become zero or even negative.

In accordance with the king's order, we need to choose n - 1 roads and make them the main roads. An important condition must hold: it should be possible to travel from any city to any other by the main roads.

The road ministry has a budget of S lamziks for the reform. The ministry is going to spend this budget for repair of some roads (to reduce the dissatisfaction with them), and then to choose the n

 - 1 main roads.

Help to spend the budget in such a way and then to choose the main roads so that the total dissatisfaction with the main roads will be as small as possible. The dissatisfaction with some roads can become negative. It is not necessary to spend whole budget S.

It is guaranteed that it is possible to travel from any city to any other using existing roads. Each road in the kingdom is a two-way road.

Input

The first line contains two integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of cities and the number of roads in the kingdom, respectively.

The second line contains m integers w1, w2, ..., wm (1 ≤ wi ≤ 109), where wi is the drivers dissatisfaction with the i-th road.

The third line contains m integers c1, c2, ..., cm (1 ≤ ci ≤ 109), where ci is the cost (in lamziks) of reducing the dissatisfaction with the i-th road by one.

The next m lines contain the description of the roads. The i-th of this lines contain a pair of integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi) which mean that the i-th road connects cities ai and bi. All roads are two-way oriented so it is possible to move by the i-th road from aito bi, and vice versa. It is allowed that a pair of cities is connected by more than one road.

The last line contains one integer S (0 ≤ S ≤ 109) — the number of lamziks which we can spend for reforms.

Output

In the first line print K — the minimum possible total dissatisfaction with main roads.

In each of the next n - 1 lines print two integers x, vx, which mean that the road x is among main roads and the road x, after the reform, has the level of dissatisfaction vx.

Consider that roads are numbered from 1 to m in the order as they are given in the input data. The edges can be printed in arbitrary order. If there are several answers, print any of them.

Examples input
6 9
1 3 1 1 3 1 2 2 2
4 1 4 2 2 5 3 1 6
1 2
1 3
2 3
2 4
2 5
3 5
3 6
4 5
5 6
7
output
0
1 1
3 1
6 1
7 2
8 -5
input
3 3
9 5 1
7 7 2
2 1
3 1
3 2
2
output
5
3 0
2 5

Solution

題意:給一個無向圖,每條邊有一個邊權$w$和一個費用$c$,你現在有$s$元,對於每條邊可以選擇花費$c$將這條邊邊權減少1(允許負邊權),詢問這種操作過後最小生成樹的最小總權值以及樹上的所有邊和它們的權值。

思路很簡單,明顯可以把所有的花費全部砸在一條邊上,可以首先建一棵最小生成樹,如果把錢砸在樹邊,那麼選擇的一定是樹邊中$c$最小的那條,往死裡減就可以了。

如果要砸在非樹邊上,那麼就是在這條非樹邊兩端點在樹上的鏈中找到最長的邊刪除,用這條邊代替即可。

主要是程式碼實現太複雜了!!!!(雖然我一次a掉嘻嘻嘻嘻嘻

捋清楚每條邊的標號是最複雜的???鏈剖+線段樹隨便搞搞就好。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;

LL n, m, s;

struct tree {
    LL w, id;
    tree operator + (const tree &a) const {
        tree c;
        if(a.w > w)    c.w = a.w, c.id = a.id;
        else        c.w = w, c.id = id;
        return c;
    }
} TR[1000005];

struct Node {
    LL u, v, w, id, tag, nex;
} Edge[400005], Edge_inv[200005];
bool cmp(Node a, Node b) { return a.w < b.w; }

LL h[200005], stot;
void add(LL u, LL v, LL w, LL id) {
    Edge[++stot] = (Node) {u, v, w, id, 0, h[u]};
    h[u] = stot;
}

LL stot_inv;
void add_inv(LL u, LL v, LL w, LL id) {
    Edge_inv[++stot_inv] = (Node) {u, v, w, id, 0, 0};
}

LL fa[200005], dep[200005], siz[200005], son[200005], sw[200005], sid[200005];
void dfs1(LL u, LL f) {
    fa[u] = f;    dep[u] = dep[f] + 1;  siz[u] = 1;
    for(LL i = h[u]; i; i = Edge[i].nex) {
        LL v = Edge[i].v;
        if(v == f)    continue;
        dfs1(v, u);
        siz[u] += siz[v];
        if(siz[v] > siz[son[u]])    son[u] = v, sw[u] = Edge[i].w, sid[u] = Edge[i].id;
    }
}

LL top[200005], seq[200005], seq1[200005], in[200005], idc;
void dfs2(LL u, LL t, LL w, LL id) {
    top[u] = t;    seq[++idc] = w; seq1[idc] = id, in[u] = idc;
    if(son[u])    dfs2(son[u], t, sw[u], sid[u]);
    for(LL i = h[u]; i; i = Edge[i].nex) {
        LL v = Edge[i].v;
        if(v == fa[u] || v == son[u])    continue;
        dfs2(v, v, Edge[i].w, Edge[i].id);
    }
}

void update(LL nd) {
    TR[nd] = TR[nd << 1] + TR[nd << 1 | 1];
}

void build(LL nd, LL l, LL r) {
    if(l == r) {
        TR[nd].w = seq[l];
        TR[nd].id = seq1[l];
        return ;
    }
    LL mid = (l + r) >> 1;
    build(nd << 1, l, mid);    build(nd << 1 | 1, mid + 1, r);
    update(nd);
}

tree query(LL nd, LL l, LL r, LL L, LL R) {
    if(l >= L && r <= R)    return TR[nd];
    LL mid = (l + r) >> 1; tree ans;    ans.w = -0x3f3f3f3f, ans.id = 0;
    if(L <= mid)    ans = ans + query(nd << 1, l, mid, L, R);
    if(R > mid)        ans = ans + query(nd << 1 | 1, mid + 1, r, L, R);
    return ans;
}

tree query(LL u, LL v) {
    tree ans; ans.w = -0x3f3f3f3f, ans.id = 0;
    while(top[u] != top[v]) {
        if(dep[top[u]] < dep[top[v]])    swap(u, v);
        ans = ans + query(1, 1, n, in[top[u]], in[u]);
        u = fa[top[u]];
    }
    if(dep[u] < dep[v])    swap(u, v);
    ans = ans + query(1, 1, n, in[v] + 1, in[u]);
    return ans;
}

LL f[200005];
LL find(LL x) {
    if(x != f[x])    f[x] = find(f[x]);
    return f[x];
}

LL w[200005], c[200005], tot, ans1, ans2;
void Kruskal() {
    sort(Edge_inv + 1, Edge_inv + 1 + m, cmp);
    for(LL i = 1; i <= n; i ++)    f[i] = i;
    for(LL i = 1; i <= m; i ++) {
        LL u = Edge_inv[i].u, v = Edge_inv[i].v, id = Edge_inv[i].id;
        LL uu = find(u), vv = find(v);
        if(uu != vv) {
            Edge_inv[i].tag = 1;
            f[uu] = vv;
            add(u, v, w[id], id);    add(v, u, w[id], id);
            tot += w[id];
            if(c[id] < c[ans1])    ans1 = id;
        }
    }
}

int main() {
    scanf("%lld%lld", &n, &m);
    for(LL i = 1; i <= m; i ++)    scanf("%lld", &w[i]);
    for(LL i = 1; i <= m; i ++)    scanf("%lld", &c[i]);    c[0] = 0x3f3f3f3f;
    for(LL i = 1; i <= m; i ++) {
        LL u, v;
        scanf("%lld%lld", &u, &v);
        add_inv(u, v, w[i], i);
    }
    scanf("%lld", &s);
    Kruskal();    ans2 = tot - s / c[ans1];
    dfs1(1, 0);    dfs2(1, 0, -0x3f3f3f3f, 0);    build(1, 1, n);
    LL flag = 0;
    for(LL i = 1; i <= m; i ++) {
        if(!Edge_inv[i].tag) {
            LL u = Edge_inv[i].u, v = Edge_inv[i].v, id = Edge_inv[i].id;
            tree a = query(u, v);
            LL tmp = tot - a.w + w[id] - s / c[id];
            if(tmp < ans2)    ans1 = id, ans2 = tmp, flag = a.id;
        }
    }
    printf("%lld\n", ans2);
    for(LL i = 1; i <= m; i ++) {
        LL id = Edge_inv[i].id;
        if(ans1 == id) {
            printf("%lld %lld\n", id, w[id] - s / c[id]);    
        } else if(Edge_inv[i].tag) {
            if(flag != id) {
                printf("%lld %lld\n", id, w[id]);
            }
        }
    }
    return 0;
}