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
分析:题目大意是有n个点,m条路,每条路有不满意度w[i],以及减小一个不满意度代价c[i],
   问给你s元,找到一个总不满意度最小的生成树,保证有解;
   经过观察可以发现我们用S元总是对一条路使用的;
   所以跑一遍最小生成树求得初始最小花费;
   枚举不在树上的边,加入后形成环,删去这条边两个节点路上w[j]最大的边,判断是否更优;
   两个节点经过的路边的最大值可以用倍增lca,或树链剖分;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,fa[][maxn],dep[maxn],w[maxn],c[maxn],ok[maxn],pq[maxn],u[maxn],v[maxn],p[maxn],s;
vi ans;
vector<pii>e[maxn];
pii ex,ey,st[][maxn];
ll sum,best;
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
void dfs(int p)
{
for(int i=;fa[i-][p];i++)
{
fa[i][p]=fa[i-][fa[i-][p]];
st[i][p]=max(st[i-][p],st[i-][fa[i-][p]]);
}
for(pii x:e[p])
{
int to=x.fi,q=x.se;
if(to==fa[][p])continue;
dep[to]=dep[p]+;
fa[][to]=p;
st[][to]=mp(w[q],q);
dfs(to);
}
}
pii max_lca(int a,int b)
{
pii ma=mp(,);
if(dep[a]<dep[b])swap(a,b);
for(int i=;i>=;i--)
if(dep[fa[i][a]]>=dep[b])ma=max(ma,st[i][a]),a=fa[i][a];
if(a==b)return ma;
for(int i=;i>=;i--)
{
if(fa[i][a]!=fa[i][b])
{
ma=max(ma,st[i][a]);
ma=max(ma,st[i][b]);
a=fa[i][a];
b=fa[i][b];
}
}
ma=max(ma,st[][a]);
ma=max(ma,st[][b]);
return ma;
}
int main()
{
int i,j;
n=read(),m=read();
rep(i,,m)w[i]=read();
rep(i,,m)c[i]=read();
rep(i,,m)u[i]=read(),v[i]=read(),pq[i]=i;
scanf("%d",&s);
sort(pq+,pq+m+,[](const int&x,const int&y){return w[x]<w[y];});
rep(i,,n)p[i]=i;
ex=mp(inf,inf);
rep(i,,m)
{
int x=find(u[pq[i]]),y=find(v[pq[i]]);
if(x!=y)
{
p[x]=y;
sum+=w[pq[i]];
ex=min(ex,mp(c[pq[i]],pq[i]));
ans.pb(pq[i]);
e[u[pq[i]]].pb(mp(v[pq[i]],pq[i]));
e[v[pq[i]]].pb(mp(u[pq[i]],pq[i]));
}
else ok[pq[i]]=;
}
dfs();
best=sum-s/ex.fi;
rep(i,,m)
{
if(ok[i])
{
ll now=sum;
pii better=max_lca(u[i],v[i]);
now-=better.fi;
now+=w[i]-s/c[i];
if(now<best)
{
best=now;
ex=better;
ey=mp(c[i],i);
}
}
}
printf("%lld\n",best);
for(int x:ans)
{
if(x==ex.se)
{
if(!ey.se)printf("%d %d\n",x,w[x]-s/c[x]);
}
else printf("%d %d\n",x,w[x]);
}
if(ey.fi)printf("%d %d\n",ey.se,w[ey.se]-s/c[ey.se]);
//system("Pause");
return ;
}

Drivers Dissatisfaction的更多相关文章

  1. CF733F Drivers Dissatisfaction【链剖】【最小生成树应用】

    F. Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  2. Codeforces Round #378 (Div. 2) F - Drivers Dissatisfaction

    F - Drivers Dissatisfaction 题目大意:给你n个点,m条边,每个边都有一个权重w,每条边也有一个c表示,消耗c元可以把这条边的权重减1,求最多消耗s元的最小生成树. 思路:因 ...

  3. codeforce 378 div 2 F —— Drivers Dissatisfaction (最小生成树,LCA,倍增)

    官方题解: If you choose any n - 1 roads then price of reducing overall dissatisfaction is equal to min(c ...

  4. 【codeforces 733F】 Drivers Dissatisfaction

    http://codeforces.com/problemset/problem/733/F (题目链接) 题意 给出一张n个点的无向图,每一条变有两个特征值:${w,c}$:分别表示这条边的权值为$ ...

  5. Codeforces 733F Drivers Dissatisfaction

    题意:有n个点,m条边,每条边有不满意度w[i],以及减小一个不满意度代价c[i],问给你s元用来减少代价,找到一个总不满意度最小的生成树,保证有解.(减少后的不满意度可以为负数)思路:显然所有的钱都 ...

  6. Drivers Dissatisfaction 最小生成树+LCA

    题意:给一张n个点m条边的连通图,每条边(ai,bi)有一个权值wi和费用ci, 表示这条边每降低1的权值需要ci的花费.现在一共有S费用可以用来降低某些边的权值 (可以降到负数),求图中的一棵权值和 ...

  7. Codeforces Round #378 (Div. 2)F - Drivers Dissatisfaction GNU

    http://codeforces.com/contest/733/problem/F 题意:给你一些城市和一些路,每条路有不满意程度和每减少一点不满意程度的花费,给出最大花费,要求找出花费小于s的最 ...

  8. 【CF733F】Drivers Dissatisfaction(最小瓶颈生成树,倍增)

    题意:给出一个图,每条边有权值和花费c,每次花费c能使的权值-1.给出一个预算,求减完权值后的一个最小生成树. 思路:感谢CC大神 有这样一个结论:最佳方案里必定存在一种,预算全部花费全部分配在一条边 ...

  9. 【codeforces 733F】Drivers Dissatisfaction

    [题目链接]:http://codeforces.com/problemset/problem/733/F [题意] 给你n个点m条边; 让你从中选出n-1条边; 形成一个生成树; (即让n个点都联通 ...

随机推荐

  1. php日期转时间戳,指定日期转换成时间戳

    写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储.处理方便,但 是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么 ...

  2. C++构造与析构

    C++语言构造函数与析构函数需要注意的地方. 构造 考虑以下定义 struct Node { char *name; int value; Node() { name = ]; strcpy(name ...

  3. webapi中的自定义路由约束

    Custom Route Constraints You can create custom route constraints by implementing the IHttpRouteConst ...

  4. webapi中的扩展点

    Extension Points Web API provides extension points for some parts of the routing process. Interface ...

  5. 多线程---静态同步函数的锁是class(转载)

    /** 如果同步函数被静态修饰,那么他的锁就是该方法所在类的字节码文件对象 类名.class 静态进内存时,内存中没有本类对象,但是一定有该类对应的字节码文件对象. 该对象就是:类名.class   ...

  6. erlang nif小结

    一.nif获取string参数的两种方式 1.eif_get_string 实例如下: static ERL_NIF_TERM erl_sm4_encrypt(ErlNifEnv* env, int ...

  7. 5.编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、 姓名(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age) 用于对对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类—— TestClass,在主类的main方法中创建多个Student类的对象,使用这些对象来测 试St

    Student类: package com.bao; public class Student { int stuNo;String name,sex;int age,weight; Student( ...

  8. 浅析js模板引擎

    js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...

  9. .Net_把文件数据添加到数据库中(面试题)

    一个文本文件含有如下内容: 4580616022644994|3000|赵涛 4580616022645017|6000|张屹 4580616022645090|3200|郑欣夏 上述文件每行为一个转 ...

  10. mave 安装本地jar包到maven库

    Maven 安装 JAR 包的命令是: mvn install:install-file -Dfile=本地jar包的位置 -DgroupId=groupId -DartifactId=artifac ...