Codeforces 196E Opening Portals MST (看题解)
我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST。 想在如果不是所有点是特殊点的话, 我们能不能也
转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然后求出两两之间的最短路, 然后求MST, 但直接这样暴力做
肯定不行。 我们先跑个多元最短路, 找到离 i 最近的特殊点 p[ i ], 并且距离为d[ i ]。 对于每两个特殊点a, b之间的最短路
我们都能找到一条边(u, v, w)对应它, 并且p[ u ] = a, p[ v ] = b, 且在所有的p[ u ] = a, p[ v ] = b的边中 d[ u ] + d[ v ] + w
是最小的那个, 这就是a, b之间的最短路。 我们将边排序之后, 跑克鲁斯卡尔就好啦。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ;
const double eps = 1e-;
const double PI = acos(-); int n, m, a[N], k;
vector<PLI> G[N]; LL d[N];
int p[N]; int fa[N];
int getRoot(int x) {
return x == fa[x] ? x : fa[x] = getRoot(fa[x]);
} pair<PII, int> e[N];
int id[N]; bool cmp(const int& a, const int& b) {
return d[e[a].fi.fi] + d[e[a].fi.se] + e[a].se < d[e[b].fi.fi] + d[e[b].fi.se] + e[b].se;
} int main() {
memset(d, INF, sizeof(d));
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i <= m; i++) {
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
e[i].fi.fi = x, e[i].fi.se = y, e[i].se = w;
G[x].push_back(mk(w, y));
G[y].push_back(mk(w, x));
id[i] = i;
}
scanf("%d", &k);
priority_queue<PLI, vector<PLI>, greater<PLI> > que;
for(int i = ; i <= k; i++) {
scanf("%d", &a[i]);
p[a[i]] = a[i];
d[a[i]] = ;
que.push(mk(, a[i]));
}
while(!que.empty()) {
int u = que.top().se;
LL dis = que.top().fi;
que.pop();
if(dis > d[u]) continue;
for(auto& e : G[u]) {
if(dis + e.fi < d[e.se]) {
d[e.se] = dis + e.fi;
p[e.se] = p[u];
que.push(mk(d[e.se], e.se));
}
}
}
LL ans = ;
sort(id + , id + + m, cmp);
for(int i = ; i <= m; i++) {
int u = p[e[id[i]].fi.fi];
int v = p[e[id[i]].fi.se];
LL w = e[id[i]].se + d[e[id[i]].fi.fi] + d[e[id[i]].fi.se];
int x = getRoot(u);
int y = getRoot(v);
if(x != y) {
fa[y] = x;
ans += w;
}
}
ans += d[];
printf("%lld\n", ans);
return ;
} /*
*/
Codeforces 196E Opening Portals MST (看题解)的更多相关文章
- Codeforces 1017F The Neutral Zone (看题解)
这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...
- Codeforces 513E2 Subarray Cuts dp (看题解)
我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...
- Codeforces 822E Liar dp + SA (看题解)
Liar 刚开始感觉只要开个dp[ i ][ j ][ 0 / 1 ]表示处理了s的前 i 个用了 k 段, i 是否是最后一段的最后一个字符 的 t串最长匹配长度, 然后wa24, 就gg了.感觉这 ...
- Codeforces 725E Too Much Money (看题解)
Too Much Money 最关键的一点就是这个贪心可以在sqrt(n)级别算出答案. 因为最多有sqrt(n)个不同的数值加入. 我们可以发现最优肯定加入一个. 然后维护一个当前可以取的最大值, ...
- [CodeForces - 197F] F - Opening Portals
F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country ...
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
- xtu summer individual 3 F - Opening Portals
Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- Codeforces Round #668 (Div. 2)A-C题解
A. Permutation Forgery 题目:http://codeforces.com/contest/1405/problem/A 题解:这道题初看有点吓人,一开始居然想到要用全排序,没错我 ...
随机推荐
- MySQL联结查询和子查询
2018-2-24 16:18:12 星期六 今天需要统计一个运营活动的数据, 涉及三个表, 分组比较多 活动描述: 每个人可以领取多张卡片, 好友也可以赠送其卡片, 20或40张卡片可以兑换一个奖 ...
- jQuery传参
<a href="#" onClick="click_scroll('here1');">滚动到here1</a><a href= ...
- 金九银十中,看看这31道Android面试题
阅读目录 1.如何对 Android 应用进行性能分析 2.什么情况下会导致内存泄露 3.如何避免 OOM 异常 4.Android 中如何捕获未捕获的异常 5.ANR 是什么?怎样避免和解决 ANR ...
- NPOI写Excel,Microsoft.Office.Interop.excel.dll 转换Excel为PDF
首先要引用NPOI动态库和Microsoft.Office.Interop.excel.dll (Microsoft.Office.Interop.excel.dll 下载链接 ,下载以后解压文件,把 ...
- Pl/SQL 编程
Pl/SQL 编程 一:前言 二:Pl/Sql 概述 二 —— 1: Pl/Sql块结构 [declare] --声明部分,可选 begin --执行部分,必须 [exception] -- ...
- sench touch 时间插件 扩展
因项目 需要 老项目需要用到 时分 的插件 而本身sencha touch 自己木有这个功能,因此在网上找到了 一个可以扩展的插件. 相关目录复制如下代码: /** * The picke ...
- gnuradio 创建cos_source
C++教程 ys_linux@computer:~$ gr_modtool nm kcd Creating out-of-tree module in ./gr-kcd... Done. Use 'g ...
- 自定义Form组件
一.wtforms源码流程 1.实例化流程分析 # 源码流程 1. 执行type的 __call__ 方法,读取字段到静态字段 cls._unbound_fields 中: meta类读取到cls._ ...
- py4测试题
1.8<<2等于? 32 2.通过内置函数计算5除以2的余数 print(divmod(5,2))------>1 3.s=[1,"h",2,"e&qu ...
- PDF文件如何标注,怎么使用PDF标注工具
我们在使用文件的时候需要给文件的部分添加标注,能够更加直观的了解文件,但是有很多小伙伴们对于PDF文件怎么添加标注都不知道,也不知道PDF标注工具要怎么使用,那么下面就跟大家分享一下怎么使用PDF标注 ...