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 ...
随机推荐
- bzoj1925 [Sdoi2010] 地精部落【DP】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1925 一个多月前“过”了这道题,还自欺欺人地认为懂了这道题,这直接导致了昨晚多校联测2的T3 ...
- Android偏好设置(7)自定义Preference,和PreferenceDialog
Building a Custom Preference The Android framework includes a variety of Preference subclasses that ...
- ORACLE数据库的备份分为物理备份和逻辑备份两种。
物理备份是将实际组成数据库的操作系统文件从一处拷贝到另一处的备份过程,通常是从磁盘到磁带.可以使用 Oracle 的恢复管理器(Recovery Manager,RMAN)或操作系统命令进行数据库的物 ...
- Ubuntu卸载软件包
sudo apt-get autoremove --purge mysql-server-5.0 ,purge连同配置文件一起删除,autoremove自动卸载依赖包sudo apt-get remo ...
- Webform 内置对象 Session对象、Application全局对象,ViewState
Session 每台电脑访问服务器,都有独立的session,key值都一样,内容不一样. 1.session保存在服务器上. 2.session没有持久性,保存周期就是20分钟. 重点: sessi ...
- 置换测试: Mock, Stub 和其他
简介 在理想情况下,你所做的所有测试都是能应对你实际代码的高级测试.例如,UI 测试将模拟实际的用户输入(Klaas 在他的文章中有讨论)等等.实但际上,这并非永远都是个好主意.为每个测试用例都访问一 ...
- Java操作pdf: JarsperReport的简单使用
在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...
- 浅谈网上的zoomlistview存在的问题
最近项目主要是做一个类似wps文档阅历的功能,以列表的形式显示文档,并且需要实现缩放平移.而网上关于此类功能的实现主要是通过自定义的listview实现的,类名为ZoomListView. 网上的zo ...
- JavaScript——XMLHttpRequest 家族
https://www.zhangxinxu.com/wordpress/2013/10/understand-domstring-document-formdata-blob-file-arrayb ...
- HTML5——loading
https://www.cnblogs.com/wangmeijian/p/4449150.html https://www.cnblogs.com/yunser/p/canvas-baidu-loa ...