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 ...
随机推荐
- TCP/IP网络协议基础
实验楼学习网络协议传送门 一.TCP/IP简介 TCP/IP(Transmission Control Protocol/Internet Protocol)是传输控制协议和网络协议的简称,它定义了电 ...
- JAVA中abstract,interface,final,static语法
转自:http://www.cnblogs.com/yueue/archive/2010/04/20/1715863.html 一,抽象类:abstract 1,只要有一个或一个以上抽象方法的 ...
- scala基础篇-03 if与for
一.Scala中的if是表达式** 1.定义方式 2.例子 二.for 的用法 1.定义方式: for{ x <- xs y=x+ ) }yield y 2.例子:
- R in action读书笔记(6)-第七章:基本统计分析(中)
7.2 频数表和列联表 > library(vcd) > head(Arthritis) ID Treatment Sex Age Improved 1 57 Treated Male 2 ...
- iOS循环引用
iOS循环引用 当前类的闭包/Block属性,用到了当前类,就会造成循环引用 此闭包/Block应该是当前类的属性,我们经常对Block进行copy,copy到堆中,以便后用. 单方向引用是不会产生循 ...
- VBox虚拟机安装debian
决定在win7上装一个Linux虚拟机用作Linux开发学习,虽然win7下已经有了Cygwin,还是想在一个比较完整的环境下.前面装过Ubuntu发现界面太笨重了,考虑重新换一个,同时比较喜欢apt ...
- 【C++】类型转换简述:四种类型转换方式的说明及应用
本文主要简述在C++中四种类型转换的方式:static_cast.reniterpret_cast.const_cast和dynamic_cast. 在介绍C++类型转换方式之前,我们先来看看C语言的 ...
- 【C++】模板简述(五):类型萃取
功能 类型萃取,在STL中用到的比较多,用于判断一个变量是否为POD类型. 简述来说可以用来判断出某个变量是内置类型还是自定义类型. 通过类型萃取,萃取到变量类型,对不同变量进行不同处理,可以提升程序 ...
- js面向对象之构造函数
最简单的面向对象程序<script type="text/javascript"> var obj = new Object(); obj.qq = '10791611 ...
- asterisk-java ami3 属性改变监听
asteriskServer.addAsteriskServerListener(new AsteriskListenerInit());//服务属性监听会自动连接服务 实现AsteriskServe ...