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 1055 最长等差数列

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1055 题意: 思路:先固定一个位置,然后从该中心点出发向两边扫,确实很难 ...

  2. 如何看待Arcsoft虹软,推出的人脸认知引擎免费SDK?

    虹软公司是一家具有硅谷背景的图像处理公司,除了人脸技术以外,还有多项图像及视频处理技术.他们的双摄像头处理算法和人脸美化算法囊括了包括OPPO VIVO,SUMAMNG一系列手机厂商. 整个人脸识别运 ...

  3. 有关C#中List排序的总结

    这里有一篇文章作者总结的就比较详细: https://blog.csdn.net/jimo_lonely/article/details/51711821 在这里只记录一点: 对list或者数组中的数 ...

  4. 动态规划-击爆气球 Burst Balloons

    2018-10-03 19:29:43 问题描述: 问题求解: 很有意思的题目,首先想到的是暴力遍历解空间,当然也用到了memo,可惜还是TLE,因为时间复杂度确实有点过高了,应该是O(n!). Ma ...

  5. jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第二话):安装插件,配置JDK、Git、Ant

    jenkins之所以这么强大,离不开丰富的插件库. 要确保jenkins上安装好Git plugin.GitHub plugin.AntPlugin插件,一般在启动jenkins时默认安装的插件中就包 ...

  6. m_Orchestrate learning system---mo系统权限思考

    m_Orchestrate learning system---mo系统权限思考 一.总结 一句话总结:注意不同身份访问同一客户端时候的权限,比如面板显示,比如功能按钮 权限 面板 功能 1.小组之间 ...

  7. 数据结构(C语言版)-第2章 线性表

    #define MAXSIZE 100 //最大长度 typedef struct { ElemType *elem; //指向数据元素的基地址 int length; //线性表的当前长度 }SqL ...

  8. 【转】 H.264编码原理以及I帧B帧P帧

    转自:http://www.cnblogs.com/herenzhiming/articles/5106178.html 前言 ----------------------- H264是新一代的编码标 ...

  9. Django使用admin管理后台管理数据库表

    1.在admin.py文件中注册需要创建的表,例: from .models import * # Register your models here. admin.site.register(Use ...

  10. POJ-3693/HDU-2459 Maximum repetition substring 最多重复次数的子串(需要输出具体子串,按字典序)

    http://acm.hdu.edu.cn/showproblem.php?pid=2459 之前hihocoder那题可以算出最多重复次数,但是没有输出子串.一开始以为只要基于那个,每次更新答案的时 ...