1. 程式人生 > >codeforces 733 F. Drivers Dissatisfaction(最小生成樹+lca+倍增去環)

codeforces 733 F. Drivers Dissatisfaction(最小生成樹+lca+倍增去環)

題目連結

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

題意:

給出一個n個點,m條邊的帶權連通無向圖,每條邊有個權值,還有個ci,表示將第i條邊權值減1需要花費ci元,你一共有S元,求用完S元后的最小生成樹。

題解:

考慮最後得到的子樹,要獲得這樣一棵最小的樹,那麼S元一定全部花在某一條邊上(樹上邊權值最小的邊上),那麼首先貪心求原圖的最小生成樹,然後列舉每一條邊就是那條權值減小的邊,有兩種情況:

1.這條邊在最小生成樹上,那麼直接算出答案。

2.不在最小生成樹上,那麼將這條邊加入最終的樹上,那麼此時會形成一個環,那麼就刪掉環上最大值(求lca並用倍增類似預處理)。

2017-03-14 04:47:15 Magic GNU C++ Accepted 514 ms 80500 KB
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=2e5+100;
int head[maxn],pa[maxn];
struct edge
{
    int from,to,next,w,id;
}e[maxn*2];   //
struct node
{
    int u,v,w,id;
    node(int a=0,int b=0,int c=0,int d=0):u(a),v(b),w(c),id(d) {}
    bool operator <(const node& t)const{
        return w<t.w;
    }
}E[maxn];
int tol=0;
void add(int u,int v,int w,int id)
{
    e[++tol].to=v,e[tol].from=u,e[tol].id=id,e[tol].w=w,e[tol].next=head[u],head[u]=tol;
}
int w[maxn],c[maxn];

int vis[maxn],deep[maxn],fa[maxn][19];
PII d[maxn][19];//記錄最大邊長和該邊標號
void dfs(int x)
{
    vis[x]=1;
    rep(i,1,19)
    {
        if(deep[x]<(1<<i)) break;
        fa[x][i]=fa[fa[x][i-1]][i-1];
        d[x][i]=max(d[x][i-1],d[fa[x][i-1]][i-1]);
    }
    for(int i=head[x];i;i=e[i].next)
    {
        if(vis[e[i].to]) continue;
        fa[e[i].to][0]=x;
        d[e[i].to][0]=make_pair(e[i].w,e[i].id);
        deep[e[i].to]=deep[x]+1;
        dfs(e[i].to);
    }
}
int lca(int x,int y)
{
    if(deep[x]<deep[y]) swap(x,y);
    int d=deep[x]-deep[y];
    rep(i,0,19) if(d&(1<<i)) x=fa[x][i];
    if(x==y) return x;
    per(i,0,19) if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}
PII solve(int x,int y)
{
    int f=lca(x,y);
    PII ans1=make_pair(-1,0),ans2=make_pair(-1,0);
    int di=deep[x]-deep[f];
    rep(i,0,19) if(di&(1<<i)) ans1=max(ans1,d[x][i]),x=fa[x][i];
    di=deep[y]-deep[f];
    rep(i,0,19) if(di&(1<<i)) ans2=max(ans2,d[y][i]),y=fa[y][i];
    return max(ans1,ans2);
}
int find(int x)
{
    return pa[x]==x? x:pa[x]=find(pa[x]);
}
bool cmp(node a,node b)
{
    return a.id<b.id;
}
set<int> st;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    rep(i,1,maxn) pa[i]=i;
    st.clear();
    rep(i,1,m+1) scanf("%d",&w[i]);
    rep(i,1,m+1) scanf("%d",&c[i]);
    rep(i,1,m+1)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        E[i]=node(u,v,w[i],i);
    }
    int s;
    scanf("%d",&s);
    sort(E+1,E+m+1);
    ll ans=0;
    rep(i,1,m+1)
    {
        int u=E[i].u,v=E[i].v;
        int fu=find(u),fv=find(v);
        if(fu==fv) continue;
        pa[fu]=fv;
        ans+=1ll*E[i].w;
        add(u,v,E[i].w,E[i].id);
        add(v,u,E[i].w,E[i].id);
        st.insert(E[i].id);
    }
    dfs(1);
    ll ans1=ans;
    int f=-1;//
    set<int>::iterator it;
    for(it=st.begin();it!=st.end();it++)
    {
        int u=*it;
        ll res=ans1-s/c[u];
        if(res<ans)
        {
            f=u;
            ans=res;
        }
    }
    int f1=-1;
    sort(E+1,E+m+1,cmp);
    rep(i,1,m+1)
    {
        if(st.find(i)!=st.end()) continue;
        PII t=solve(E[i].u,E[i].v);
        ll res=ans1+w[i]-t.first-s/c[i];
        //        if(i==8) cout << "ff" << endl;
        //        printf("%lld\n",t.first);
        if(res<ans)
        {
            f=i;f1=t.second;
            ans=res;
        }
    }
    printf("%lld\n",ans);
    if(f1==-1)
    {
        if(f!=-1)
        {
            printf("%d %d\n",f,w[f]-s/c[f]);
            for(it=st.begin();it!=st.end();it++)
            {
                if(*it!=f)
                    printf("%d %d\n",*it,w[*it]);
            }
        }
        else
        {
            for(it=st.begin();it!=st.end();it++)
            {
                printf("%d %d\n",*it,w[*it]);
            }
        }
    }
    else
    {
        printf("%d %d\n",f,w[f]-s/c[f]);
        for(it=st.begin();it!=st.end();it++)
        {
            if(*it!=f1)
                printf("%d %d\n",*it,w[*it]);
        }
    }
    return 0;
}