Opening Portals

Time Limit: 2000ms
Memory Limit: 262144KB

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 xiyiwi (1 ≤ xi, yi ≤ nxi ≠ 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

Input
  1. 3 3
    1 2 1
    1 3 1
    2 3 1
    3
    1 2 3
Output
  1. 2
Input
  1. 4 3
    1 2 1
    2 3 5
    2 4 10
    3
    2 3 4
Output
  1. 16
Input
  1. 4 3
    1 2 1000000000
    2 3 1000000000
    3 4 1000000000
    4
    1 2 3 4
Output
  1. 3000000000

Source

 
 
 
解题:还是有些看不懂的地方。。。。。。。。。哎。。。。。。。。
 
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #define LL long long
  13. #define INF 0x3f3f3f3f3f3f3f3f
  14. #define mk make_pair
  15. using namespace std;
  16. const int maxn = ;
  17. int uf[maxn],be[maxn];
  18. LL d[maxn];
  19. bool vis[maxn] = {false};
  20. vector< pair<int,int> >g[maxn];
  21. vector< pair<LL,pair<int,int> > >e;
  22. priority_queue< pair<LL,int> >q;
  23. int Find(int x){
  24. if(uf[x] != x)
  25. uf[x] = Find(uf[x]);
  26. return uf[x];
  27. }
  28. int main(){
  29. int u,v,i,k,tu,tv,n,m;
  30. LL w,ans = ;
  31. scanf("%d%d",&n,&m);
  32. for(i = ; i < m; i++){
  33. scanf("%d%d%I64d",&u,&v,&w);
  34. g[u].push_back(mk(v,w));
  35. g[v].push_back(mk(u,w));
  36. }
  37. memset(d,,sizeof(d));
  38. scanf("%d",&k);
  39. for(i = ; i < k; i++){
  40. scanf("%d",&u);
  41. d[u] = ;
  42. uf[u] = u;
  43. be[u] = u;
  44. q.push(mk(,u));
  45. }
  46. while(!q.empty()){
  47. u = q.top().second;
  48. q.pop();
  49. if(vis[u]) continue;
  50. vis[u] = true;
  51. for(i = ; i < g[u].size(); i++){
  52. v = g[u][i].first;
  53. w = g[u][i].second;
  54. if(be[v]) e.push_back(mk(d[u]+d[v]+w,mk(be[u],be[v])));
  55. if(d[v] > d[u]+w){
  56. d[v] = d[u]+w;
  57. be[v] = be[u];
  58. q.push(mk(-d[v],v));
  59. }
  60. }
  61. }
  62. sort(e.begin(),e.end());
  63. for(i = ; i < e.size(); i++){
  64. u = e[i].second.first;
  65. v = e[i].second.second;
  66. tu = Find(u);
  67. tv = Find(v);
  68. if(tu != tv){
  69. ans += e[i].first;
  70. uf[tu] = tv;
  71. }
  72. }
  73. printf("%I64d\n",ans+d[]);
  74. return ;
  75. }

xtu summer individual 3 F - Opening Portals的更多相关文章

  1. [CodeForces - 197F] F - Opening Portals

    F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country ...

  2. xtu summer individual 6 F - Water Tree

    Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

  3. xtu summer individual 5 F - Post Office

    Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...

  4. Codeforces 196E Opening Portals MST (看题解)

    Opening Portals 我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST. 想在如果不是所有点是特殊点的话, 我们能不能也 转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然 ...

  5. 【做题】CF196E. Opening Portals 排除无用边&最小生成树

    题意:给出一个有\(n\)个结点,\(m\)条边的连通无向图,边有边权,等于经过这条边所需的时间.有\(k\)个点设有传送门.一开始,所有传送门关闭.你从\(1\)号点出发,每当你到达一个有传送门的点 ...

  6. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  7. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  8. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  9. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

随机推荐

  1. 洛谷 P2048 [NOI2010]超级钢琴 || Fantasy

    https://www.luogu.org/problemnew/show/P2048 http://www.lydsy.com/JudgeOnline/problem.php?id=2006 首先计 ...

  2. mysql各个版本下载地址

    之所记录下来是因为我找了好久才找到:这下记着了:http://downloads.mysql.com/archives/community/ 希望对没有找到的朋友有帮助

  3. 【转】在Ubuntu中安装HBase

    原博客出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/ 感谢! Posted: Apr 3, 2014 Tags: Hado ...

  4. MS SqlServer 通过数据库日志文件找回已删除的记录

    1.建立演示数据(创建数据库数据表添加基础数据) 1.1 创建数据库 1.2 创建数据表 1.3填充数据 1.4做数据库完整备份 2.模拟误删除.记录操作时间.备份数据库日志 2.1删除数据并记录操作 ...

  5. Java对象创建

    今天发现一个问题: 如果加上第一个输出,那么两个per1的对象是不一样的,如果不加,那么下一个输出的对象的是和第一个是一致的? 思考为什么???? 代码如下: package com.lgq.serv ...

  6. AJPFX简述可变参数概述和使用

    A:可变参数概述 定义方法的时候不知道该定义多少个参数 B:格式 修饰符 返回值类型 方法名(数据类型… 变量名){} C:注意事项: 这里的变量其实是一个数组 如果一个方法有可变参数,并且有多个参数 ...

  7. Node.js(二)常用的系统模块

    http模块 第一章已经介绍了 node.js 的模块都可以传一个回调函数  回调函数支持两个参数  error , data let fs = require('fs'); fs.readFile( ...

  8. Jenkins .NET项目持续集成配置

    基本步骤 1. 安装并配置MSBUILD 在系统管理->插件管理->添加MSBuild插件 在系统管理->系统设置->找到MSBuild配置部分,配置不同的MSbuild版本 ...

  9. LigerUI用Post\Get\Ajax前后台交互方式的写法

    parms 参数统一 json格式的数据 url 访问后台的url 设置同步参数 [javascript] view plain copy   $.ajaxSetup({ async : false} ...

  10. 【C++】模板简述(五):类型萃取

    功能 类型萃取,在STL中用到的比较多,用于判断一个变量是否为POD类型. 简述来说可以用来判断出某个变量是内置类型还是自定义类型. 通过类型萃取,萃取到变量类型,对不同变量进行不同处理,可以提升程序 ...