F - Opening Portals

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 p1p2, ..., 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.

Example

Input
3 31 2 11 3 12 3 131 2 3
Output
2
Input
4 31 2 12 3 52 4 1032 3 4
Output
16
Input
4 31 2 10000000002 3 10000000003 4 100000000041 2 3 4
Output
3000000000

Note

In the second sample the player has to come to city 2, open a portal there, then go to city 3, open a portal there, teleport back to city 2 and finally finish the journey in city 4.

题目大意就是, 给定n个点,m条边,再给出k个有传送门的点,如果一个点已经被访问过且上面有传送门,则传送门可打开。

如果有两个点上面有传送门且传送门都打开,则可以直接在这两个点上瞬间移动(不耗费时间)。问遍历所有有传送门的点至少要多久。

毫无疑问,这就是将K个传送门点都联通(还有起点1)的代价,即MST的代价.但是直接去做MST,建边都建不了.由于很多遍都没有不会用到,那么我们只需要考虑那最优的边,我们需要在原图上将边进行修改.

怎么进行修改?我们主要需要的只是传送门点之间的路径而已.所以我们设d[i]表示从点i到最近的传送点的时间,p[i]即为最近的传送点.这两个东西可以通过最短路跑出来.

那么一条原来的边(x,y,z)现在就变成了(p[x],p[y],z+d[x]+d[y]).这样,我们既改变了每条边的两个端点,又保证了正确性.最后以新边跑一便MST即可.

 #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<queue>
 #define mp make_pair
 #define maxn 200005
 using namespace std;
 struct edge{int x,y; long long z;}e[maxn];
 int n,m,k,tot,lnk[maxn],son[maxn],nxt[maxn],fa[maxn],p[maxn];
 long long d[maxn],w[maxn],ans;
 bool vis[maxn];
 priority_queue< pair<long long,int> >Q;
 bool cmp(edge a,edge b){return a.z<b.z;}
 int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
 void add(int x,int y,long long z){nxt[++tot]=lnk[x],son[tot]=y,w[tot]=z,lnk[x]=tot;}
 int main(){
     scanf(;
     int x,y; long long z;
     ; i<=m; i++){
         scanf("%d%d%I64d",&x,&y,&z);
         e[i]=(edge){x,y,z},add(x,y,z),add(y,x,z);
     }
     memset(d,,sizeof d);
     scanf("%d",&k);
     ; i<=k; i++) scanf(,p[x]=x,Q.push(mp(,x));
     while (!Q.empty()){
         while (vis[x=Q.top().second]&&Q.size()) Q.pop();
         if (Q.empty()) break;
         vis[x]=,Q.pop();
         for (int j=lnk[x]; j; j=nxt[j]) if (d[son[j]]>d[x]+w[j]){
             d[son[j]]=d[x]+w[j],p[son[j]]=p[x];
             Q.push(mp(-d[son[j]],son[j]));
         }
     }
     ; i<=m; i++) e[i].z+=d[e[i].x]+d[e[i].y],e[i]=(edge){p[e[i].x],p[e[i].y],e[i].z};
     sort(e+,e+m+,cmp);
     ; i<=n; i++) fa[i]=i;
     ; i<=m; i++) if (get(e[i].x)!=get(e[i].y)) fa[get(e[i].x)]=get(e[i].y),ans+=e[i].z;
     cout<<ans+d[];
     ;
 }

[CodeForces - 197F] F - Opening Portals的更多相关文章

  1. xtu summer individual 3 F - Opening Portals

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

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

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

  3. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  4. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  5. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  6. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  7. Codeforces 622 F. The Sum of the k-th Powers

    \(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...

  8. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  9. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

随机推荐

  1. 51nod 1351 吃点心(贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 题意: 思路: 要么先选low值大的,要么先选high值大的,分两 ...

  2. ASP.NET开发总结

    ASP.NET的界面可以是.aspx,会对应有一个.aspx.cs的逻辑处理文件,.aspx的所有控件对应着变量,变量名就是控件的ID. 为了代码编写方便起见,一般将数据库表的新增字段,放在最后. 日 ...

  3. 在GeoServer里设置图层的默认自定义样式,出现不显示预览图的情况(不起作用)

    在GeoServer里设置图层的默认自定义样式 点击"Layers-->world:country"图层,点击"Publishing"标签,在下面的&qu ...

  4. Educational Codeforces Round 23 F. MEX Queries 离散化+线段树

    F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. mysql5.7.23手动配置安装windows版

    1.mysql下载地址 官网:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 官网我下载的是: 百度网盘:链接: https://pa ...

  6. Oracle其他简单查询

    范例:查询公司中所有雇员的职位信息 SELECT job FROM emp; 实际在公司里面,一个职位会有多个人员.如果查询全部职位,肯定会存在重复.要消除掉重复,利用DISTINCT完成.(dist ...

  7. JavaScript 局部刷新

    JavaScript局部刷新具体代码展示如下 1.  #tabList代表需要刷新的元素的对象 2.  第二个#tabList 如果后面有第三个元素,那么后面需要加>*符号,如果不加,容易造成C ...

  8. java转义字符处理——“\\”替换为“/”

    replaceAll("\\\\", "/"); 例如 //获取项目路径,并将\转换为/ File directory = new File("&qu ...

  9. C++ 实现sqilte创建数据库插入、更新、查询、删除

    C/C++ Interface APIs Following are important C/C++ SQLite interface routines, which can suffice your ...

  10. gulp自动化打包工具

    /** * Created by hasee on 2016/7/5. */var gulp = require('gulp');var sass = require('gulp-sass');//容 ...