A8642 | Opening Portals
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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?
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?
输入格式
The first line contains two space-separated integers $n$ and $m$ ( $1<=n<=10^{5}$ , $0<=m<=10^{5}$ ) 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 $x_{i}$ , $y_{i}$ , $w_{i}$ ( $1<=x_{i},y_{i}<=n$ , $x_{i}≠y_{i}$ , $1<=w_{i}<=10^{9}$ ) — 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 $p_{1}$ , $p_{2}$ , ..., $p_{k}$ — numbers of the cities with installed portals. Each city has no more than one portal.
Each of the next $m$ lines contains the description of a road as three space-separated integers $x_{i}$ , $y_{i}$ , $w_{i}$ ( $1<=x_{i},y_{i}<=n$ , $x_{i}≠y_{i}$ , $1<=w_{i}<=10^{9}$ ) — 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 $p_{1}$ , $p_{2}$ , ..., $p_{k}$ — numbers of the cities with installed portals. Each city has no more than one portal.
输出格式
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.
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.
输入输出样例
输入 #1
3 3 1 2 1 1 3 1 2 3 1 3 1 2 3
输出 #1
2
输入 #2
4 3 1 2 1 2 3 5 2 4 10 3 2 3 4
输出 #2
16
输入 #3
4 3 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 1 2 3 4
输出 #3
3000000000
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted