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

F - Drivers Dissatisfaction

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

思路:因为一条边的权重没有下限所以s元肯定是用在一条边上的。 那么我们先跑一个最小生成树,把这棵最小生成树建出来,然后我们枚举用了

s元之后的边,如果这条边不在树上那么加上这条边之后肯定形成了一个环,最优的方案肯定是删掉这个环中权值最大的边再次变成一棵树。

对于边(u,v)来说,如果把这条边加上,那么删掉的边肯定在,u到 lca(u,v) 和 v到 lca(u,v)中的权值最大的那条边。 可以用倍增快速找边,就能解决

这个问题啦。  码起来好麻烦啊,借鉴了某个学长的代码。。

  1 #include<bits/stdc++.h>
  2 #define fi first
  3 #define se second
  4 #define mk make_pair
  5 #define ll long long
  6 #define pll pair<ll,ll>
  7 #define pil pair<int,ll>
  8 #define pli pair<ll,int>
  9 #define pii pair<int,int>
 10 #define read(x) scanf("%d",&x)
 11 #define sread(x) scanf("%s",x)
 12 #define dread(x) scanf("%lf",&x)
 13 #define lread(x) scanf("%lld",&x)
 14 using namespace std;
 15
 16 const int N=2e5+7;
 17 const int M=2e6+7;
 18 const int inf=0x3f3f3f3f;
 19 const ll INF=0x3f3f3f3f3f3f3f3f;
 20 const int mod=1e9+7;
 21
 22 int n,m,tot,s,fa[N],depth[N],nx[N][20],mx[N][20];
 23
 24 struct node
 25 {
 26     int u,v,w,c,id;
 27     bool operator < (const node &rhs)const
 28     {
 29         return w<rhs.w;
 30     }
 31 }e[N<<1];
 32
 33 bool cmp(node a,node b){
 34     return a.id<b.id;
 35 }
 36 vector<pii> edge[N];
 37 map<int,int> result;
 38
 39 int Find(int x) {
 40     return x==fa[x]? x : fa[x]=Find(fa[x]);
 41 }
 42
 43 ll kruscal()
 44 {
 45     ll ans=0;
 46     for(int i=1;i<=n;i++)
 47         fa[i]=i;
 48
 49     sort(e,e+m);
 50
 51     for(int i=0;i<m;i++)
 52     {
 53         int u=e[i].u,v=e[i].v,w=e[i].w;
 54         int nx_u=Find(u);
 55         int nx_v=Find(v);
 56         if(nx_u!=nx_v)
 57         {
 58             edge[u].push_back(mk(v,e[i].id));
 59             edge[v].push_back(mk(u,e[i].id));
 60
 61             fa[nx_u]=nx_v;
 62             ans+=w;
 63             result[e[i].id]=w;
 64         }
 65     }
 66
 67     sort(e,e+m,cmp);
 68
 69     return ans;
 70 }
 71
 72 int getMxId(int a,int b)
 73 {
 74     if(a==-1) return b;
 75     if(b==-1) return a;
 76     return e[a].w>e[b].w? a : b;
 77 }
 78 void dfs(int u,int pre)
 79 {
 80     nx[u][0]=pre;
 81     for(int i=1;i<20;i++)
 82     {
 83         if(nx[u][i-1]!=-1)
 84         {
 85             nx[u][i]=nx[nx[u][i-1]][i-1];
 86             mx[u][i]=getMxId(mx[u][i-1],mx[nx[u][i-1]][i-1]);
 87         }
 88         else
 89             nx[u][i]=mx[u][i]=-1;
 90     }
 91
 92     for(auto t : edge[u])
 93     {
 94         int v=t.first;
 95         if(v==pre)
 96             continue;
 97         depth[v]=depth[u]+1;
 98         mx[v][0]=t.second;
 99
100         dfs(v,u);
101     }
102 }
103
104 int getLca(int a,int b)
105 {
106     if(depth[a]<depth[b])
107         swap(a,b);
108     for(int i=0;i<20;i++)
109         if(depth[a]-depth[b]>>i & 1)
110             a=nx[a][i];
111
112     if(a==b) return a;
113     for(int i=19;i>=0;i--)
114         if(nx[a][i]!=nx[b][i])
115             a=nx[a][i],b=nx[b][i];
116
117     return nx[a][0];
118 }
119
120 int getMxIdPath(int a,int b)
121 {
122     int lca=getLca(a,b);
123     int ret=-1;
124     for(int i=19;i>=0;i--)
125     {
126         if(nx[a][i]!=-1 && depth[nx[a][i]]>=depth[lca])
127         {
128             ret=getMxId(ret,mx[a][i]);
129             a=nx[a][i];
130         }
131         if(nx[b][i]!=-1 && depth[nx[b][i]]>=depth[lca])
132         {
133             ret=getMxId(ret,mx[b][i]);
134             b=nx[b][i];
135         }
136     }
137     return ret;
138 }
139
140 int main()
141 {
142     read(n); read(m);
143
144     for(int i=0;i<m;i++)
145         read(e[i].w);
146
147     for(int i=0;i<m;i++)
148         read(e[i].c);
149
150     for(int i=0;i<m;i++)
151     {
152         read(e[i].u);
153         read(e[i].v);
154         e[i].id=i;
155     }
156
157     read(s);
158
159     ll cost=kruscal();
160
161     dfs(1,-1);
162
163     ll ans=cost, p=0;
164
165     for(int id=0;id<m;id++)
166     {
167         int big=getMxIdPath(e[id].u,e[id].v);
168         ll tmp=cost-e[big].w+e[id].w-s/e[id].c;
169         if(tmp<ans)
170             ans=tmp,p=id;
171     }
172
173     printf("%lld\n",ans);
174     result.erase(getMxIdPath(e[p].u,e[p].v));
175     result[p]=e[p].w-s/e[p].c;
176
177     for(auto t: result)
178         printf("%d %d\n",t.first+1,t.second);
179
180     return 0;
181 }
182 /*
183 */

原文地址:https://www.cnblogs.com/CJLHY/p/8676162.html

时间: 2024-07-31 15:42:21

Codeforces Round #378 (Div. 2) F - Drivers Dissatisfaction的相关文章

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can

Codeforces Round #501 (Div. 3) F. Bracket Substring

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #392 (Div. 2) F. Geometrical Progression

原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output For given n, l and r find the number of distinct geometrical pro

Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行,最后从上到下,从左到右形成一个序列s1,s2.....snm,满足对于任意相邻的两个数,它们差的绝对值的最大值为k. 现在问怎么交换行与行,可以使得最后的这个k最大. 题解: 人生中第一道状压dp~其实还是参考了这篇博客:https://blog.csdn.net/CSDNjiangshan/art

Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\),\(pref_j\),一个人可以买一道菜的条件是 1. \(p_i \leq inc_j \leq s_i\) 2. \(|b_i - pref_j| \leq inc_j-p_i\) ,问每个人分别能买多少道菜 题解 转化一下公式 \(p_i \leq inc_j \leq s_i\) 下面两个满

Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y=x^2+bx+c=>y+x^2=bx+c\),转换为点\((x,y+x^2)\)在bx+c的直线上 两个点确定一条抛物线,同时也确定了一条直线 需要选择最上面那些点相邻确定的抛物线,所以维护一个上凸包即可 维护上凸包,当前点在前进方向左边需要向后退,cross(a,b)>=0 代码 #inclu

Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的时间.然后有先手和后手俩个人. ?先手可以这么操作:在规定总时间T到达某个节点然后一定要返回根节点1,期间可以选择吃掉某些节点上的某些饼干(前提是保证剩下的时间能够回到根节点): ?后手可以这么操作:在先手到达的位置和这个位置的孩子之间的连边选择一条让先手吃得更多的边摧毁掉,也可以跳过这个过程: 问

Codeforces Round #615 (Div. 3) F. Three Paths on a Tree

F. Three Paths on a Tree 原题链接:https://codeforces.com/contest/1294/problem/F 题目大意: 给定一棵树,选出三点,使三点连成的j简单路径最大.简而言之,三个点连成的边的集合大小. 解题思路: 假设任取一点为三点连线的公共点,最长路径就是这个点到其他三个点的三条最长边之和,可知这个点一定在直径上(画图分析假设不在时的最长路径可反证).所以先求出树的直径,在使用$ans =(a b+a c+b c) / 2$遍历可以得到第三个点

Codeforces Round #629 (Div. 3) F - Make k Equal (离散化 树状数组维护前缀和)

https://codeforces.com/contest/1328/problem/F 首先把a数组处理成pair对(num,cnt),表示数字num有cnt个,然后按num升序排序离散化一下. 对于一个数x,若想使得小于x的数字都变成x,必须先把所有小于x的数变成x-1,然后再+1变成x. 同理,要使得大于x的数变成x,必须把所有大于x的数字变成x+1,然后再-1变成x. 以上是题意所要求的必须操作. 思路: 1. 用f[i]数组记录离散化后前i大的数字的总数,那么对于任意第i大数字,可以