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 wi.

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;
}

 

转载于:https://www.cnblogs.com/wans-caesar-02111007/p/9911336.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爱关系中体验到的安全感和信任感,以及对伴侣的依恋程度和稳定性。依恋安全是维持恋爱品质和长期关系的关键。我们可以通过建立亲密对于400+ K-Pop Groups Dataset数据集,我们可以对其中的各个变量进行描述性统计,包括关系、培养信任感和稳定感、以及积极解决冲突和矛盾等方式来提高团名、出道时间、国家、成员数等。下面是一个使用Python中的pandas库进行描述性依恋安全感,从而促进恋爱中的心理健康(Mikulincer & Shaver, 2007)。 3.心理支持 心理支持是指在恋爱关系中给予伴侣情感上统计的示例代码,其中对团名、出道时间、国家和成员数进行了描述性统计: 的支持和理解,以及帮助伴侣解决问题和应对挑战的能力。心理支持```python import pandas as pd # 读取数据 data = pd.read_csv('K-Pop_Groups.csv') # 对是维持恋爱品质和长期关系的关键。我们可以通过关注伴侣的情感和需求、积极倾听和理解对方、以及提供实际帮助和支持等方式来提高心理团名进行描述性统计 print('团名的描述性统计:') print(data['Group Name'].describe()) # 对支持能力,从而促进恋爱中的心理健康(Reis & Shaver, 1988)。 出道时间进行描述性统计 print('出道时间的描述性统计:') print(data['Debut'].describe()) 参考文献: Barnett, P. A., & Gotlib, I. H. (1988). Psychosocial functioning and depression: Distinguishing among antecedents, concomitants, and consequences. Psychological Bulletin, 104(1), 97# 对国家进行描述性统计 print('国家的描述性统计:') print(data['Country'].describe()) #-126. Gross, J. J., & John, O. P. (2003). Individual differences in two emotion regulation processes: 对成员数进行描述性统计 print('成员数的描述性统计:') print(data['Members'].describe()) Implications for affect, relationships, and well-being. Journal of Personality and Social Psychology, 85(2), 348-362. Grossmann, I., & Kross, E. (2010). Exploring Emotion Regulation in Close Relationships. In Handbook of Em``` 上面的代码使用了pandas库中的describe()函数对团名、出道时间、国家和成员otion Regulation (pp. 423-440). Guilford Press. Kiecolt-Glaser, J. K., & Newton, T. L. (2001). Marriage and health: His and hers. Psychological Bulletin, 127(4), 472-503. 数进行了描述性统计。describe()函数会计算数据的基本统计量,包括计数、均值Mikulincer, M., & Shaver, P. R. (2007). Attachment in adulthood: Structure, dynamics, and change. Guilford Press. Reis, H. T., & Shaver, P. (1988). Intimacy as an interpersonal process、标准差、最小值、最大值和四分位数等,并将结果输出到控制台。下面. In Handbook of personal relationships (pp. 367-389). Wiley. Whisman, M. A. (2001). The association between depression and marital dissatisfaction. In Psychological aspects of marital disruption (pp. 3-24). American Psychological Association.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值