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. tcpdump抓取mysql语句

    抓包工具tcpdump的使用,抓取具体的sql语句 [root@test7_chat_api_im ~]# tcpdump -s -l - |strings tcpdump: listening on ...

  2. Solidity函数view,pure,constant的用法

    函数访问权限:public,external,private,internal //子类可以得到父类下面的方法,父类用了public,external的话 pragma solidity ^; con ...

  3. js获取当前星期几

    使用Date对象的getDay方法可以获取当前日期的星期数. getDay() 方法可返回表示星期的某一天的数字. 示例: var date = new Date(); alert(date.getD ...

  4. C#生成不重复的N位随机数

    直接上代码: #region 生成N位随机数 /// <summary> /// 生成N位随机数 /// </summary> /// <param name=" ...

  5. Node.js 调存储过程

    var spring = require("spring"); //当前登录人ID var account_id = require('nodejava').toJs.parse( ...

  6. Confluence 6 Oracle 连接问题解决

    如果 Confluence 提示没有 class 文件,你可能将你的 JDBC 驱动放置到了错误的文件夹. 下面的页面包含了一些你在使用 Oracle 数据库连接的时候可能会遇到的常见问题,请参考: ...

  7. Confluence 6 发送 Confluence 通知到其他 Confluence 服务器

    你可以配置 Confluence 服务器向其他的 Confluence 服务器发送消息.在这种情况下,Confluence 服务器将不会显示 workbox. 希望发送消息到其他 Confluence ...

  8. vue-cli脚手架(框架)

    一.创建vue项目 npm install vue-cli -g #-g全局 (sudo)npm install vue-cli -g #mac笔记本 vue-init webpack myvue # ...

  9. eclipse的安装及使用

    1.安装 2工作区 3透视图添加透视图 关闭和显示各个子视图 点击视图右上角的关闭按钮可以关闭当前视图 可以选择Window-->Show View菜单项打开各个子视图 4创建项目 选择File ...

  10. python修改hosts

    #coding=utf-8 host = ['192.168.10.240 store.wondershare.com', '192.168.10.240 store.wondershare.jp', ...