[CF733F]Drivers Dissatisfaction

Description

outputstandard 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

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

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

The third line contains m integers c1, c2, ..., cm (1 ≤ ci ≤ 10^9), 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 ≤ n, ai ≠ 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 ai to 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 ≤ 10^9) — 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.

Sample 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

Sample Output

0
1 1
3 1
6 1
7 2
8 -5

Sample Input

3 3
9 5 1
7 7 2
2 1
3 1
3 2
2

Sample Output

5
3 0
2 5

题解

先跑一次最小生成树,在枚举每条边,在树上的直接计算,否则在两点之间的路径中删掉一条最大边计算

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;

LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

LL ans,MST;
int n,m,S;
int in[200050];
int ecnt, head[200050];
int bel[200050];
int deep[200050],parent[200050][25],mx[200050][25],mxpos[200050][25];
struct data{int u,v,c,w,id;}e[200050];
struct edge{int to,nxt,cost,id;}g[400050];

int comp(const data&a,const data&b){return a.w<b.w;}
int findset(int a){return a==bel[a]?a:bel[a]=findset(bel[a]);}
inline void addedge(int u,int v,int w,int id)
{
    g[ecnt]=(edge){v,head[u],w,id},head[u]=ecnt++;
    g[ecnt]=(edge){u,head[v],w,id},head[v]=ecnt++;
}

void getdeep(int root,int step,int fa)
{
    deep[root]=step;
    for(int i=1;i<25;i++)
    {
        int k=parent[root][i-1];
        parent[root][i]=parent[k][i-1];
        if(mx[root][i-1]<mx[k][i-1])
        mx[root][i]=mx[k][i-1],mxpos[root][i]=mxpos[k][i-1];
        else mx[root][i]=mx[root][i-1],mxpos[root][i]=mxpos[root][i-1];
    }
    for(int i=head[root];~i;i=g[i].nxt)
    {
        int v=g[i].to,w=g[i].cost,id=g[i].id;
        if(v==fa)continue;
        parent[v][0]=root,mx[v][0]=w,mxpos[v][0]=id;
        getdeep(v,step+1,root);
    }
}

int pos;
int getmax(int a,int b)
{
    int res=0;
    if(deep[a]>deep[b])swap(a,b);
    for(int i=24;i>=0;i--)if((deep[b]-deep[a])&(1<<i))
    {
        if(mx[b][i]>res)res=mx[b][i],pos=mxpos[b][i];
        b=parent[b][i];
    }
    if(a==b)return res;
    for(int i=24;i>=0;i--)if(parent[a][i]!=parent[b][i])
    {
        if(mx[a][i]>res)res=mx[a][i],pos=mxpos[a][i];
        if(mx[b][i]>res)res=mx[b][i],pos=mxpos[b][i];
        a=parent[a][i],b=parent[b][i];
    }
    if(mx[a][0]>res)res=mx[a][0],pos=mxpos[a][0];
    if(mx[b][0]>res)res=mx[b][0],pos=mxpos[b][0];
    return res;
}

int main()
{
    memset(head,-1,sizeof(head));
    n=read(),m=read();
    for(int i=1;i<=m;i++)e[i].id=i;
    for(int i=1;i<=m;i++)e[i].w=read();
    for(int i=1;i<=m;i++)e[i].c=read();
    for(int i=1;i<=m;i++)e[i].u=read(),e[i].v=read();
    sort(e+1,e+m+1,comp);
    for(int i=1;i<=n;i++)bel[i]=i;
    for(int i=1,cnt=0;i<=m;i++)
    {
        int u=e[i].u,v=e[i].v,w=e[i].w;
        if(findset(u)==findset(v))continue;
        bel[findset(u)]=findset(v);
        addedge(u,v,w,i),in[i]=1,MST+=w;
        cnt++;if(cnt==n-1)break;
    }
    getdeep(1,1,0);
    int k=0,t=0;ans=MST;S=read();
    for(int i=1;i<=m;i++)
    {
        int u=e[i].u,v=e[i].v,w=e[i].w,c=e[i].c;
        if(in[i]){if(MST-S/c<ans)ans=MST-S/c,k=i;}
        else {int x=getmax(u,v);if(MST-x+w-S/c<ans)
        ans=MST-x+w-S/c,t=pos,k=i;}
    }
    cout<<ans<<endl;
    if(in[k]){for(int i=1;i<=m;i++)if(in[i])
    printf("%d %d\n",e[i].id,e[i].w-bool(i==k)*S/e[i].c);}
    else for(int i=1;i<=m;i++)
    {
        if(in[i]){if(i!=t)printf("%d %d\n",e[i].id,e[i].w);}
        else if(i==k)printf("%d %d\n",e[i].id,e[i].w-S/e[i].c);
    }
    return 0;
}

转载于:https://www.cnblogs.com/ljzalc1022/p/8744224.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值