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. PHP程序守护进程化

    一般Server程序都是运行在系统后台,这与普通的交互式命令行程序有很大的区别.glibc里有一个函数daemon.调用此函数,就可使当前进程脱离终端变成一个守护进程,具体内容参见man daemon ...

  2. 使用参数innodb_file_per_table支持MySQL InnoDB表数据共享空间自动收缩

    http://heylinux.com/archives/2367.html http://blog.csdn.net/ywh147/article/details/8996022 使用过MySQL的 ...

  3. python学习第42、43天 HTML\CSS

    前端是什么? 帮助不了解后端程序的客户轻松使用程序的工具,可以提升工作效率,提供各种各样的体验. 通用的前端大致会使用三种语言,用在三个不同的方面对前端进行架构和优化,这里也只介绍这三种 web前端常 ...

  4. Vue - Router 路由

    路由的注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  5. python-常用模块xml、shelve、configparser、hashlib

    一.shelve模块 shelve模块也是用来序列化的. 使用方法: 1.open 2.读写 3.close import shelve # 序列化 sl = shelve.open('shlvete ...

  6. 你的B计划在哪里?

    春节同学聚会,大家聊起近况. 甲在实体经济部门工作,企业效益不好,正酝酿减员增效,他忧心忡忡,跳槽都不知道怎么跳,因为全行业都不景气. 乙从事互联网工作,行业发展热火朝天,新事物层出不穷,但是他已人到 ...

  7. Weblogic12c 单节点安装

    第一节weblogic12c 的安装   WebLogic除了包括WebLogic Server服务器之外,还包括一些围绕WebLogic的产品,习惯上我们说的WebLogic是指WebLogic S ...

  8. Confluence 6 包括从其他 Confluence 服务器上来的通知

    Confluence workbox 可以显示从 Confluence 服务器上发送过来的消息. 让我们假设你有 2 个 Confluence 服务器, ConfluenceChatty 和 Conf ...

  9. git码云上传本地项目

    可参考:https://blog.csdn.net/huangfei711/article/details/69388230 .在你的项目上鼠标右击点击Git bash git config --gl ...

  10. Springboot+MyBatis+JPA集成

      1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...