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. 批处理bat标准化获取当前系统日期的几种方法,补0

    首先有两个推荐的方案. 一: for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get Loc ...

  2. _itemmod_gem_remove

    该表可配置以一定代价移除宝石,移除后获得该宝石 `entry`宝石ID `reqId` 需求ID `chance`几率 `comond` 备注

  3. Mysql 强行Kill 连接

    BEGIN ; ; ; DO KILL @Temp; ; END WHILE ; END

  4. Data Structure 基本概念

    数据(data) 描述客观事物的数值 数据项(data item) 具有原子性,不可分割的最小单位 数据元素(data element)集合的个体,通常由很多数据组成 数据对象(data object ...

  5. hashtable详解

    hashtable也比称作哈希表,键值对或者关联数组 1. 先引用using System.Collections;命名空间 用于处理和表现key/value的键值对,其中key通常用来快速查找,同时 ...

  6. 学习笔记5—Python 将多维数据转为一维数组 (总结)

    <code class="language-python">import operator from functools import reduce a = [[1,2 ...

  7. isA,小赋值大

    class Student:Person{ }; Student s; 1. Person p=s;  // 可以将具体的赋值给大的,指针也行. 2. Person * p=new Student; ...

  8. 注册表的作用、bat文件中REG ADD命令添加注册表项以及bat

    注册表的用途与设置 注册表是windows的核心,里面储存着大量的系统信息,说白了就是一个庞大的数据库.如果你不懂什么是数据库,那没关系,不影响你了解注册表,不过最好对数据库有所了解.注册表里面所有的 ...

  9. seo中的竞价排名是什么

    seo中的竞价排名是什么 一.总结 一句话总结:竞价排名的基本特点是按点击付费,推广信息出现在搜索结果中(一般是靠前的位置),如果没有被用户点击,则不收取推广费. 搜索引擎的一种推广广告的方式 1.竞 ...

  10. CRC分段校验

    crc16 modbus分段校验码: const uint8_t ModbusCRCHighTab[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x ...