xtu summer individual 3 F - Opening Portals

本文探讨了一款游戏中玩家如何快速开启所有传送门的问题。利用图论中的最短路径算法和并查集来解决该问题,确保玩家能在最短时间内完成任务。

Opening Portals

Time Limit: 2000ms
Memory Limit: 262144KB
This problem will be judged on  CodeForces. Original ID: 196E
64-bit integer IO format: %I64d      Java class name: (Any)
 
 

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 ≠ 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 p1, p2, ..., 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.

 

Sample Input

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

Source

 
 
 
解题:还是有些看不懂的地方。。。。。。。。。哎。。。。。。。。
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f3f3f3f3f
14 #define mk make_pair
15 using namespace std;
16 const int maxn = 100010;
17 int uf[maxn],be[maxn];
18 LL d[maxn];
19 bool vis[maxn] = {false};
20 vector< pair<int,int> >g[maxn];
21 vector< pair<LL,pair<int,int> > >e;
22 priority_queue< pair<LL,int> >q;
23 int Find(int x){
24     if(uf[x] != x)
25         uf[x] = Find(uf[x]);
26     return uf[x];
27 }
28 int main(){
29     int u,v,i,k,tu,tv,n,m;
30     LL w,ans = 0;
31     scanf("%d%d",&n,&m);
32     for(i = 0; i < m; i++){
33         scanf("%d%d%I64d",&u,&v,&w);
34         g[u].push_back(mk(v,w));
35         g[v].push_back(mk(u,w));
36     }
37     memset(d,3,sizeof(d));
38     scanf("%d",&k);
39     for(i = 0; i < k; i++){
40         scanf("%d",&u);
41         d[u] = 0;
42         uf[u] = u;
43         be[u] = u;
44         q.push(mk(0,u));
45     }
46     while(!q.empty()){
47         u = q.top().second;
48         q.pop();
49         if(vis[u]) continue;
50         vis[u] = true;
51         for(i = 0; i < g[u].size(); i++){
52             v = g[u][i].first;
53             w = g[u][i].second;
54             if(be[v]) e.push_back(mk(d[u]+d[v]+w,mk(be[u],be[v])));
55             if(d[v] > d[u]+w){
56                 d[v] = d[u]+w;
57                 be[v] = be[u];
58                 q.push(mk(-d[v],v));
59             }
60         }
61     }
62     sort(e.begin(),e.end());
63     for(i = 0; i < e.size(); i++){
64         u = e[i].second.first;
65         v = e[i].second.second;
66         tu = Find(u);
67         tv = Find(v);
68         if(tu != tv){
69             ans += e[i].first;
70             uf[tu] = tv;
71         }
72     }
73     printf("%I64d\n",ans+d[1]);
74     return 0;
75 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3893759.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值