xtu summer individual 3 F - Opening Portals
Opening Portals
This problem will be judged on CodeForces. Original ID: 196E
64-bit integer IO format: %I64d Java class name: (Any)
Pavel plays a famous computer game. A player is responsible for a whole country and he can travel there freely, complete quests and earn experience.
This country has n cities connected by m bidirectional roads of different lengths so that it is possible to get from any city to any other one. There are portals in k of these cities. At the beginning of the game all portals are closed. When a player visits a portal city, the portal opens. Strange as it is, one can teleport from an open portal to an open one. The teleportation takes no time and that enables the player to travel quickly between rather remote regions of the country.
At the beginning of the game Pavel is in city number 1. He wants to open all portals as quickly as possible. How much time will he need for that?
Input
The first line contains two space-separated integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) that show how many cities and roads are in the game.
Each of the next m lines contains the description of a road as three space-separated integers xi, yi, wi (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ wi ≤ 109) — the numbers of the cities connected by the i-th road and the time needed to go from one city to the other one by this road. Any two cities are connected by no more than one road. It is guaranteed that we can get from any city to any other one, moving along the roads of the country.
The next line contains integer k (1 ≤ k ≤ n) — the number of portals.
The next line contains k space-separated integers p1, p2, ..., pk — numbers of the cities with installed portals. Each city has no more than one portal.
Output
Print a single number — the minimum time a player needs to open all portals.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Sample Input
3 3
1 2 1
1 3 1
2 3 1
3
1 2 3
2
4 3
1 2 1
2 3 5
2 4 10
3
2 3 4
16
4 3
1 2 1000000000
2 3 1000000000
3 4 1000000000
4
1 2 3 4
3000000000
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
#define mk make_pair
using namespace std;
const int maxn = ;
int uf[maxn],be[maxn];
LL d[maxn];
bool vis[maxn] = {false};
vector< pair<int,int> >g[maxn];
vector< pair<LL,pair<int,int> > >e;
priority_queue< pair<LL,int> >q;
int Find(int x){
if(uf[x] != x)
uf[x] = Find(uf[x]);
return uf[x];
}
int main(){
int u,v,i,k,tu,tv,n,m;
LL w,ans = ;
scanf("%d%d",&n,&m);
for(i = ; i < m; i++){
scanf("%d%d%I64d",&u,&v,&w);
g[u].push_back(mk(v,w));
g[v].push_back(mk(u,w));
}
memset(d,,sizeof(d));
scanf("%d",&k);
for(i = ; i < k; i++){
scanf("%d",&u);
d[u] = ;
uf[u] = u;
be[u] = u;
q.push(mk(,u));
}
while(!q.empty()){
u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = true;
for(i = ; i < g[u].size(); i++){
v = g[u][i].first;
w = g[u][i].second;
if(be[v]) e.push_back(mk(d[u]+d[v]+w,mk(be[u],be[v])));
if(d[v] > d[u]+w){
d[v] = d[u]+w;
be[v] = be[u];
q.push(mk(-d[v],v));
}
}
}
sort(e.begin(),e.end());
for(i = ; i < e.size(); i++){
u = e[i].second.first;
v = e[i].second.second;
tu = Find(u);
tv = Find(v);
if(tu != tv){
ans += e[i].first;
uf[tu] = tv;
}
}
printf("%I64d\n",ans+d[]);
return ;
}
xtu summer individual 3 F - Opening Portals的更多相关文章
- [CodeForces - 197F] F - Opening Portals
F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country ...
- xtu summer individual 6 F - Water Tree
Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...
- xtu summer individual 5 F - Post Office
Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...
- Codeforces 196E Opening Portals MST (看题解)
Opening Portals 我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST. 想在如果不是所有点是特殊点的话, 我们能不能也 转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然 ...
- 【做题】CF196E. Opening Portals 排除无用边&最小生成树
题意:给出一个有\(n\)个结点,\(m\)条边的连通无向图,边有边权,等于经过这条边所需的时间.有\(k\)个点设有传送门.一开始,所有传送门关闭.你从\(1\)号点出发,每当你到达一个有传送门的点 ...
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
随机推荐
- [POI2009]救火站Gas
Description 给你一棵树,现在要建立一些消防站,有以下要求: 1. 消防站要建立在节点上,每个节点可能建立不只一个消防站. 2. 每个节点应该被一个消防站管理,这个消防站不一定建立在该节点上 ...
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- 转 php中$_request与$_post、$_get的区别
php中有$_REQUEST与$_POST.$_GET用于接受表单数据,当时他们有何种区别,什么时候用那种最好. 一.$_REQUEST与$_POST.$_GET的区别和特点 $_REQUEST[]具 ...
- jsp声明周期
https://www.w3cschool.cn/jsp/jsp-life-cycle.html 几点注意: jsp初始化期: 容器载入jsp文件后,它会在为请求提供任何服务前调用jspinit()方 ...
- 220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- Oracle中的日期数据类型
TimeStamp日期类型 TimeStamp数据类型用于存储日期的年.月.日,以及时间的小时.分和秒,其中秒值精确到小数点后6位,该数据类型 同时包含时区信息.systimestamp函数的功能是返 ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- php debug/phpstorm调试
apache+phpstorm调试php代码,修改php.ini配置文件开启调试,没有以下代码加上即可, [XDebug]zend_extension="C:\php\php-7.0.12- ...
- 使用Kotlin,抛弃findViewById
有没有觉得Android的findViewById挺烦人的.使用Kotlin可以让你彻底抛弃这个烦恼 步骤1.在build.gradle(Module:app)中添加如下一句话 这个在老一点版本的An ...
- Android学习笔记(十) Activity的生命周期
一.如何在一个应用程序中定义多个Activity -定义一个类,继承Activity -复写onCreate() setContentView(R.layout.secondLayout):设定该Ac ...