[CodeForces - 197F] F - Opening Portals

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 ≤ 1050 ≤ 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 ≠ yi1 ≤ 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 3
1 2 1
1 3 1
2 3 1
3
1 2 3
Output
2
Input
4 3
1 2 1
2 3 5
2 4 10
3
2 3 4
Output
16
Input
4 3
1 2 1000000000
2 3 1000000000
3 4 1000000000
4
1 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即可.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #define mp make_pair
 7 #define maxn 200005
 8 using namespace std;
 9 struct edge{int x,y; long long z;}e[maxn];
10 int n,m,k,tot,lnk[maxn],son[maxn],nxt[maxn],fa[maxn],p[maxn];
11 long long d[maxn],w[maxn],ans;
12 bool vis[maxn];
13 priority_queue< pair<long long,int> >Q;
14 bool cmp(edge a,edge b){return a.z<b.z;}
15 int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
16 void add(int x,int y,long long z){nxt[++tot]=lnk[x],son[tot]=y,w[tot]=z,lnk[x]=tot;}
17 int main(){
18     scanf("%d%d",&n,&m),tot=0;
19     int x,y; long long z;
20     for (int i=1; i<=m; i++){
21         scanf("%d%d%I64d",&x,&y,&z);
22         e[i]=(edge){x,y,z},add(x,y,z),add(y,x,z);
23     }
24     memset(d,127,sizeof d);
25     scanf("%d",&k);
26     for (int i=1; i<=k; i++) scanf("%d",&x),d[x]=0,p[x]=x,Q.push(mp(0,x));
27     while (!Q.empty()){
28         while (vis[x=Q.top().second]&&Q.size()) Q.pop();
29         if (Q.empty()) break;
30         vis[x]=1,Q.pop();
31         for (int j=lnk[x]; j; j=nxt[j]) if (d[son[j]]>d[x]+w[j]){
32             d[son[j]]=d[x]+w[j],p[son[j]]=p[x];
33             Q.push(mp(-d[son[j]],son[j]));
34         }
35     }
36     for (int i=1; 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};
37     sort(e+1,e+m+1,cmp);
38     for (int i=1; i<=n; i++) fa[i]=i;
39     for (int i=1; 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;
40     cout<<ans+d[1];
41     return 0;
42 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7215987.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值