Opening Portals

我们先考虑如果所有点都是特殊点, 那么就是对整个图求个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 (看题解)的更多相关文章

  1. Codeforces 1017F The Neutral Zone (看题解)

    这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Codeforces 513E2 Subarray Cuts dp (看题解)

    我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...

  3. Codeforces 822E Liar dp + SA (看题解)

    Liar 刚开始感觉只要开个dp[ i ][ j ][ 0 / 1 ]表示处理了s的前 i 个用了 k 段, i 是否是最后一段的最后一个字符 的 t串最长匹配长度, 然后wa24, 就gg了.感觉这 ...

  4. Codeforces 725E Too Much Money (看题解)

    Too Much Money 最关键的一点就是这个贪心可以在sqrt(n)级别算出答案. 因为最多有sqrt(n)个不同的数值加入. 我们可以发现最优肯定加入一个. 然后维护一个当前可以取的最大值, ...

  5. [CodeForces - 197F] F - Opening Portals

    F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country ...

  6. Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)

    New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...

  7. xtu summer individual 3 F - Opening Portals

    Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  8. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

  9. Codeforces Round #668 (Div. 2)A-C题解

    A. Permutation Forgery 题目:http://codeforces.com/contest/1405/problem/A 题解:这道题初看有点吓人,一开始居然想到要用全排序,没错我 ...

随机推荐

  1. java泛型类型变量能调用的方法

    public class Person { } public class Student extends Person{ private String name; public Student(Str ...

  2. hadoop 透明加密

    hadoop 透明加密 hadoop 透明加密 kms transparent 2015年04月09日 18:12:20 糖糖_ 阅读数:12248 标签: transparenthadoop kms ...

  3. Java jvisualvm 远程监控tomcatt

    第一步 在远程tomcat 的bin目录下的start.sh 文件中添加一些内容(添加在exec "$PRGDIR"/"$EXECUTABLE" start & ...

  4. Confluence 6 审查日志

    日志审查能够允许管理查看你 Confluence 站点所做的修改.这个在你希望对你的 Confluence 进行问题查看或者是你希望对你 Confluence 保留重要的修改事件,例如修改了全局权限. ...

  5. automaticallyAdjustsScrollViewInsets 详解

    automaticallyAdjustsScrollViewInsets 自动缩进 20 像素 默认是 True 项目中如果有UIViewController 和ScrollView 一般都要设置成f ...

  6. Mycat实现mysql主从复制(读写分离)

    数据库性能瓶颈主要原因: 随着用户数的增多,带来的是数据库连接的大幅度增长 随着业务体量的增长,表数据量(空间存储的问题)的大幅增长,其中涉及到索引的优化,mysql默认的索引是硬盘级别的,BTREE ...

  7. Let the Balloon Rise <map>的应用

    Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the ...

  8. N阶楼梯上楼问题

    N阶楼梯上楼问题 时间限制: 1 Sec  内存限制: 32 MB 题目描述 样例输出 13 #include <stdio.h> int main() { int i, n; long ...

  9. C++ 关闭显示器

    好困,想躺一下,关灯.上床,笔记本的屏幕还亮着,好刺眼,睡不着! 脑子里出现一个疑问,怎么用C++写一个关闭屏幕的小程序呢? 参考了网上已有的例子,最简化: #include <windows. ...

  10. linux-umount挂载点无法卸载:device is busy(解决)

    umount不了的原因一般是由于有程序有用户在占用 解决方法: 1.      首先查找谁在占用:#fuser /mnt/nfs 得到进程号. 2.      查找进程:#ps –ef|grep 进程 ...