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. 每天一个小程序—0013题(爬图片+正则表达式 or BeautifulSoup)

    第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 关于python3的urllib模块,可以看这篇博客:传送门 首先是用urlopen打开网站并且获取网页 ...

  2. Appium典型问题处理

    1. http://ask.testfan.cn/article/902 Appium 服务端安装-windows2. http://ask.testfan.cn/article/1078 最新版本a ...

  3. Codeforces 785 D. Anton and School - 2

    题目链接:http://codeforces.com/contest/785/problem/D 我们可以枚举分界点,易知分界点左边和右边分别有多少个左括号和右括号,为了不计算重复我们强制要求选择分界 ...

  4. Linux硬盘分区满,但没有找到占用文件

    原因查找: 此服务器上有写日志的操作,在写的同时我把要写入的文件删除了..因为在写操作不能创建文件的时候会写硬盘的block,这样会使硬盘利用率越来越低,可以使用lsof -n |grep delet ...

  5. node项目配置成nginx启动

    node项目配置成nginx启动 1.新建ant.conf upstream antNodeJs{ server 127.0.0.1:8000; keepalive 64; } server { li ...

  6. 设计模式(六)Prototype Pattern 原型模式

    通过new产生一个对象非常繁琐,可以使用原型模式 原型模式实现: ——Cloneable接口和clone方法 ——Prototype模式实现起来最困难的地方是实现内存的复制和操作,Java中提供了cl ...

  7. 聊聊 Nginx 的反向代理

    背景 最近在优化服务基础设施这块,正好有时间写一下Nginx的体会.相信大家都听说过反向代理,一提到反向代理一定会想到Nginx.什么你没听过Nginx?那么你一定听说过Apache吧!Apache是 ...

  8. Node.js 常用命令

    1. 查看node版本 node --version 2. 查看npm 版本,检查npm 是否正确安装. npm -v 3. 安装cnpm (国内淘宝镜像源),主要用于某些包或命令程序下载不下来的情况 ...

  9. Mac python 环境配置

    问题:mac 只带了python2.7,要想使用高版本的Python,如python3.x,只能再次安装了,这样就会遇到 两个版本的切换问题了 如下图 : 执行 which python 如下图,查看 ...

  10. linux yum+wget详解

    在做自动化测试的时候,有个test需要执行命令:wget http://www.aliyun.com,但是返回的结果是未找到命令wget,于是百度了相关资料,发现没有安装wget,于是利用yum in ...