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 ≠ 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 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

 
 
 
解题:还是有些看不懂的地方。。。。。。。。。哎。。。。。。。。
 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
#define mk make_pair
using namespace std;
const int maxn = ;
int uf[maxn],be[maxn];
LL d[maxn];
bool vis[maxn] = {false};
vector< pair<int,int> >g[maxn];
vector< pair<LL,pair<int,int> > >e;
priority_queue< pair<LL,int> >q;
int Find(int x){
if(uf[x] != x)
uf[x] = Find(uf[x]);
return uf[x];
}
int main(){
int u,v,i,k,tu,tv,n,m;
LL w,ans = ;
scanf("%d%d",&n,&m);
for(i = ; i < m; i++){
scanf("%d%d%I64d",&u,&v,&w);
g[u].push_back(mk(v,w));
g[v].push_back(mk(u,w));
}
memset(d,,sizeof(d));
scanf("%d",&k);
for(i = ; i < k; i++){
scanf("%d",&u);
d[u] = ;
uf[u] = u;
be[u] = u;
q.push(mk(,u));
}
while(!q.empty()){
u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = true;
for(i = ; i < g[u].size(); i++){
v = g[u][i].first;
w = g[u][i].second;
if(be[v]) e.push_back(mk(d[u]+d[v]+w,mk(be[u],be[v])));
if(d[v] > d[u]+w){
d[v] = d[u]+w;
be[v] = be[u];
q.push(mk(-d[v],v));
}
}
}
sort(e.begin(),e.end());
for(i = ; i < e.size(); i++){
u = e[i].second.first;
v = e[i].second.second;
tu = Find(u);
tv = Find(v);
if(tu != tv){
ans += e[i].first;
uf[tu] = tv;
}
}
printf("%I64d\n",ans+d[]);
return ;
}

xtu summer individual 3 F - Opening Portals的更多相关文章

  1. [CodeForces - 197F] F - Opening Portals

    F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country ...

  2. xtu summer individual 6 F - Water Tree

    Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

  3. xtu summer individual 5 F - Post Office

    Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...

  4. Codeforces 196E Opening Portals MST (看题解)

    Opening Portals 我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST. 想在如果不是所有点是特殊点的话, 我们能不能也 转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然 ...

  5. 【做题】CF196E. Opening Portals 排除无用边&最小生成树

    题意:给出一个有\(n\)个结点,\(m\)条边的连通无向图,边有边权,等于经过这条边所需的时间.有\(k\)个点设有传送门.一开始,所有传送门关闭.你从\(1\)号点出发,每当你到达一个有传送门的点 ...

  6. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  7. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  8. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  9. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

随机推荐

  1. C++入门知识点总结

    阅读目录 1 C++中的命名空间 C++中使用命名空间来解决在相同文件或范围的同名变量问题,示例程序如下: #include <iostream> using namespace std; ...

  2. [POI2007]洪水pow

    Description AKD市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD市全被水淹没了.Blue Mary,AKD市的市长,召集了他的所有顾问(包括你)参加一个紧急会议.经过细致的商 ...

  3. [ZPG TEST 110] 多边形个数【DP】

    1. 多边形个数 (polygons.pas/c/cpp) [问题描述] 给定N线段,编号1到n.并给出这些线段的长度,用这些线段组成一个K边形,并且每个线段做多使用一次.若使用了一条不同编号的线段, ...

  4. BFS(最短路) HDOJ 4308 Saving Princess claire_

    题目传送门 题意:一个(r*c<=5000)的迷宫,起点'Y‘,终点'C',陷阱‘#’,可行路‘*’(每走一个,*cost),传送门P,问Y到C的最短路 分析:一道最短路问题,加了传送门的功能, ...

  5. Service官方教程(7)Bound Service示例之1-同进程下直接继承Service

    Extending the Binder class If your service is used only by the local application and does not need t ...

  6. 【转载】WebConfigurationManager和ConfigurationManager

    原文链接 今天在写程序时偶然发现有的示例代码中是用WebConfigurationManager获取web.config中的配置信息的,因为之前一直都是用的ConfigurationManager,所 ...

  7. mysql之流程控制

    目录 分支结构 循环结构 分支结构: 1.if condition then [statement] elseif condition then [statement] else [statement ...

  8. Android GetTimeAgo(时间戳转换几天前,几分钟前,刚刚等)

    package com.studychen.seenews.util; import android.content.Context; /** * Created by tomchen on 2/26 ...

  9. Int 1的实现过程 (一)

    闲话少说,直奔主题,首先OD载入一个程序,然后执行一下单步(调试器会将TF置1) 此时,CPU会在基于当前线程上下文的环境中,进入int 1的中断门,也就是KiTrap01 kd> !idt - ...

  10. laravel学习:模块化caffeinated

    # Modules Extract and modularize your code for maintainability. Essentially creates "mini-larav ...