首先我们强制要求几条待定价的边在MST中,建出MST

我们发现这个MST中原来的边是一定要被选上的,所以可以把点缩起来,搞成一棵只有$K$个点的树

然后$2^K$枚举每条边在不在最终的MST中,让在最终MST中的待定价的边尽量大,只需要在Kruskal的时候暴力更新每条边的定价即可

时间复杂度$O(m * logm + 2^K * K^2)$

 /**************************************************************
Problem: 3206
User: rausen
Language: C++
Result: Accepted
Time:8040 ms
Memory:8232 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
const int N = 1e5 + ;
const int M = 3e5 + ;
const int K = ;
const int inf = 1e9; inline int read(); struct Edge {
int x, y, v; inline void get(int f) {
x = read(), y = read();
if (f) v = read();
} inline bool operator < (const Edge &E) const {
return v < E.v;
}
} E[M], Ek[K], s[K]; struct edge {
int next, to;
edge() {}
edge(int _n, int _t) : next(_n), to(_t) {}
} e[K << ]; int first[N], tot; struct tree_node {
int fa, dep, mn;
ll v, sum;
} tr[N]; int n, m, k, S, top;
int fa[][N];
int root[K], cnt_root;
int u[K];
ll ans; inline void Add_Edges(int x, int y) {
e[++tot] = edge(first[x], y), first[x] = tot;
e[++tot] = edge(first[y], x), first[y] = tot;
} int find(int x, int f) {
return x == fa[f][x] ? x : fa[f][x] = find(fa[f][x], f);
} #define y e[x].to
void dp(int p) {
int x;
tr[p].sum = tr[p].v;
for (x = first[p]; x; x = e[x].next)
if (y != tr[p].fa) {
tr[y].dep = tr[p].dep + , tr[y].fa = p;
dp(y);
tr[p].sum += tr[y].sum;
}
}
#undef y ll work() {
static int i, x, y, p;
static ll res;
for (tot = , i = ; i <= k + ; ++i) {
p = root[i];
fa[][p] = p;
first[p] = tr[p].fa = , tr[p].mn = inf;
}
for (i = ; i <= k; ++i)
if (u[i]) {
x = find(Ek[i].x, ), y = find(Ek[i].y, );
if (x == y) return ;
fa[][x] = y;
Add_Edges(Ek[i].x, Ek[i].y);
}
for (i = ; i <= k; ++i) {
x = find(s[i].x, ), y = find(s[i].y, );
if (x != y) fa[][x] = y, Add_Edges(s[i].x, s[i].y);
}
dp(S);
for (i = ; i <= k; ++i) {
x = s[i].x, y = s[i].y;
if (tr[x].dep < tr[y].dep) swap(x, y);
while (tr[x].dep != tr[y].dep)
tr[x].mn = min(tr[x].mn, s[i].v), x = tr[x].fa;
while (x != y) {
tr[x].mn = min(tr[x].mn, s[i].v);
tr[y].mn = min(tr[y].mn, s[i].v);
x = tr[x].fa, y = tr[y].fa;
}
}
#define x Ek[i].x
#define y Ek[i].y
for (res = , i = ; i <= k; ++i)
if (u[i])
res += tr[x].dep > tr[y].dep ? tr[x].mn * tr[x].sum : tr[y].mn * tr[y].sum;
#undef x
#undef y
return res;
} void dfs(int p) {
if (p == k + ) {
ans = max(ans, work());
return;
}
u[p] = , dfs(p + );
u[p] = , dfs(p + );
} int main() {
int i, x, y;
n = read(), m = read(), k = read();
for (i = ; i <= m; ++i) E[i].get();
for (i = ; i <= k; ++i) Ek[i].get();
for (i = ; i <= n; ++i) tr[i].v = read();
sort(E + , E + m + );
for (i = ; i <= n; ++i) fa[][i] = fa[][i] = i; for (i = ; i <= k; ++i)
fa[][find(Ek[i].x, )] = find(Ek[i].y, );
#define x E[i].x
#define y E[i].y
for (i = ; i <= m; ++i)
if (find(x, ) != find(y, ))
fa[][find(x, )] = fa[][find(y, )], fa[][find(x, )] = fa[][find(y, )];
#undef x
#undef y
S = find(, );
for (i = ; i <= n; ++i)
if (find(i, ) != i) tr[find(i, )].v += tr[i].v;
else root[++cnt_root] = i;
#define x Ek[i].x
#define y Ek[i].y
for (i = ; i <= k; ++i)
x = find(x, ), y = find(y, );
#undef x
#undef y
#define x E[i].x
#define y E[i].y
for (i = ; i <= m; ++i)
x = find(x, ), y = find(y, );
for (i = ; i <= m; ++i)
if (find(x, ) != find(y, ))
s[++top] = E[i], fa[][find(x, )] = find(y, );
#undef x
#undef y
dfs();
printf("%lld\n", ans);
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}

BZOJ3206 [Apio2013]道路费用的更多相关文章

  1. [BZOJ3206][APIO2013]道路费用(最小生成树)

    3206: [Apio2013]道路费用 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 568  Solved: 266[Submit][Status ...

  2. [Bzoj3206][Apio2013]道路费用(kruscal)(缩点)

    3206: [Apio2013]道路费用 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 536  Solved: 252[Submit][Status ...

  3. 洛谷P3639 [APIO2013] 道路费用 [生成树的特殊算法]

    题目传送门 道路费用 格式难调,题面就不放了. 分析: 这是一道要细(yan)心(jing)的生成树的好(gui)题. 首先我们看到$k$的范围非常小,那么我们就可以直接$2^k$枚举每一条加边是否选 ...

  4. [APIO2013]道路费用

    题目描述 幸福国度可以用 N 个城镇(用 1 到 N 编号)构成的集合来描述,这些城镇 最开始由 M 条双向道路(用 1 到 M 编号)连接.城镇 1 是中央城镇.保证一个 人从城镇 1 出发,经过这 ...

  5. 题解 [APIO2013]道路费用

    link Description 幸福国度可以用 N 个城镇(用 1 到 N 编号)构成的集合来描述,这些城镇 最开始由 M 条双向道路(用 1 到 M 编号)连接.城镇 1 是中央城镇.保证一个 人 ...

  6. bzoj 3206: [Apio2013]道路费用【最小生成树+并查集】

    参考:http://hzwer.com/6888.html 把k条道路权值设为0,和其他边一起跑MST,然后把此时选中的其他边设为必选,在新图中加上必选变缩成k个点,把所有边重标号,枚举k跳边的选取情 ...

  7. 题解 洛谷 P3639 【[APIO2013]道路费用 】

    不难想到可以\(2^k\)去枚举\(k\)条新边的选择方案,然后加入原图中的边来使图连通,用当前方案的收益去更新答案,但是这样复杂度过不去. 可以先把\(k\)条新边都连上,然后再加入边权从小到大排序 ...

  8. PKUSC2018训练日程(4.18~5.30)

    (总计:共66题) 4.18~4.25:19题 4.26~5.2:17题 5.3~5.9: 6题 5.10~5.16: 6题 5.17~5.23: 9题 5.24~5.30: 9题 4.18 [BZO ...

  9. [APIO2013]

    A.机器人 题目大意:给定一个n*m的地图,有一些障碍物和k个机器人,你每次可以选择一个机器人往任意一个方向推,遇到转向器会转向,两个编号相邻的机器人可以合并,求最少推多少次可以全部合并. $n,m\ ...

随机推荐

  1. STORM_0007_Multi-Lang protocol of Storm/多语言协议的翻译

    原始地址: http://storm.apache.org/releases/1.0.1/Multilang-protocol.html 这个协议试用0.7.1之后的版本   通过ShellBolt和 ...

  2. eclipse查看jdk源码,及反编译查看

    jdk中的包: dt.jar是关于运行环境的类库,主要是swing的包 tools.jar是关于一些工具的类库 rt.jar包含了jdk的基础类库,也就是你在java doc里面看到的所有的类的cla ...

  3. CentOS的包/库的找的地方

    1. http://pkgs.org/ 在这个网站里面 搜索相应的 包名,看能不能找到. 2. http://rpm.pbone.net/ 在这个网站里面 搜索相应的 包名,看能不能找到. 3.

  4. 几种HtmlEncode的区别(转)

    一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...

  5. vitamio框架

    import io.vov.vitamio.LibsChecker; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.MediaPla ...

  6. jquery 获取鼠标位置

    //获取鼠标位置 $(function(){ $('body').mousemove(function(e) { e = e || window.event; __xx = e.pageX || e. ...

  7. 转:Teach Yourself Programming in Ten Years——用十年教会自己编程

    转自:http://blog.csdn.net/UndeadWraith/article/details/6140455 作者:Peter Norvig 译者:刘海粟 本文原文为:http://nor ...

  8. Android_进化史和平台架构介绍

    一.Android平台发展史  2008年9月,谷歌正式发布了Android 1.0系统,全球第一台Android设备HTC (G1)  2009年4月,谷歌正式推出了Android 1.5      ...

  9. grep的用法

    grep的用法首先创建我们练习grep命令时需要用到的demo文件demo_file. $ cat demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN ...

  10. 20160808_Shell书

    1. http://item.jd.com/11075150.html 2.