Codeforces 196 E. Opening Portals

Source: Internet
Author: User

Codeforces 196 E. Opening Portals


Problem E. Opening Portals
First, based on the nature of the portal, if all vertices are portals, the result is the minimum spanning tree of the graph.
For graphs with only k nodes being portals, you only need to make slight modifications based on the original algorithm.
For details, find P [I] And D [I] for each vertex. (It indicates the distance between the portal closest to this point and it...
Then, run the Minimum Spanning Tree for each edge based on D [x] + D [y] + w as the keyword ..

Use the multiple source spfa () and the slightly modified Kruskal.


E. Opening Portalstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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 hasNCities connectedMBidirectional roads of different lengths so that it is possible to get from any city to any other one. There are portals inKOf 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 integersNAndM(1? ≤?N? ≤? 105, 0? ≤?M? ≤? 105) that show how cities and roads are in the game.

Each of the nextMLines contains the description of a road as three space-separated integersXI,YI,WI(1? ≤?XI,?YI? ≤?N,XI? ?YI, 1? ≤?WI? ≤? (109)-the numbers of the cities connected byI-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 integerK(1? ≤?K? ≤?N)-The number of portals.

The next line containsKSpace-separated integersP1,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 % I64dspecifier.

Sample test (s) 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 2and finally finish the journey in city 4.





#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;const int maxn=100100;typedef long long int LL;int n,m,c;struct Edge{int from,to,next;LL weight;}edge[3*maxn];int Adj[maxn],Size;int p[maxn],cq[maxn];LL dist[maxn];bool inq[maxn];void init(){memset(Adj,-1,sizeof(Adj)); Size=0;}void Add_Edge(int u,int v,LL w){edge[Size].from=u;edge[Size].to=v;edge[Size].next=Adj[u];edge[Size].weight=w;Adj[u]=Size++;}bool spfa(){memset(dist,63,sizeof(dist));memset(p,0,sizeof(p));memset(cq,0,sizeof(cq));memset(inq,false,sizeof(inq));int k=c;queue
     
       q;for(int i=0;i
      
       dist[u]+edge[i].weight){dist[v]=dist[u]+edge[i].weight;p[v]=p[u];if(!inq[v]){inq[v]=true;cq[v]++;if(cq[v]>=n) return false;q.push(v);}}}inq[u]=false;}return true;}struct BA{int x,y;LL w;}bian[3*maxn];bool cmp(BA a,BA b){return a.w
       
        =c) break;}cout<
        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.