Metropolis
Https://www.nowcoder.com/acm/contest/203/I
Title Description Rubik's Cube State N-Cities, numbered. Cities are connected by n-1 strips to form a tree-shaped structure.
Several years later, the city of P-Cities has grown into a metropolis, and the number of roads has increased to M-bars.
There is often trade between metropolis, so for every metropolis, you can find the distance to the other metropolis closest to it. Input Description:
First row three integers n,m,p (1≤n,m≤2*10
5
, 2≤p≤n), second line, p integer
The number that represents the Metropolis (1≤x
I
≤n). Next m line three integers per line a
I
B
I
, l
I
Represents a connection A
I
and b
I
, Length is L
I
The Road (1≤a
I
B
I
≤n,1≤l
I
≤10
9
)。
The guarantee diagram is connected.
Output Description:
Outputs a line of p integers, and the I integer represents x
I
The answer.
Example 1 input
5 6 32 4 51 2 41 3 11 4 11 5 42 3 13 4 3
Output
3 3 5
1#include <iostream>2#include <cmath>3#include <vector>4#include <cstring>5#include <algorithm>6#include <queue>7 #defineMAXN 2000058 #defineMem (A, B) memset (A,b,sizeof (a))9typedefLong Longll;Ten Constll inf=0x3f3f3f3f3f3f3f3f; One using namespacestd; A -vector<pair<int,ll> >VE[MAXN]; - intn,p; the intA[MAXN],BEL[MAXN];///which metropolis is nearest ? - ll DIS[MAXN]; - intVIS[MAXN]; -ll ANS[MAXN];///the distance from your nearest metropolis. + structsair{ - intPos; + ll Len; AFriendBOOL operator<(Sair a,sair b) { at returnA.len>B.len; - } - }; - voidDijstra () { - for(intI=0; i<=n;i++){ -dis[i]=INF; inans[i]=INF; -vis[i]=0; to } + Sair s,e; - intPos; the ll Len; *Priority_queue<sair>Q; $ for(intI=1; i<=p;i++){Panax Notoginsengs.len=0, s.pos=A[i]; - Q.push (s); thedis[a[i]]=0; +bel[a[i]]=A[i]; A } the while(!Q.empty ()) { +s=q.top (); - Q.pop (); $ if(!Vis[s.pos]) { $vis[s.pos]=1; - for(intI=0; I<ve[s.pos].size (); i++){ -pos=Ve[s.pos][i].first; thelen=Ve[s.pos][i].second; - if(dis[pos]>dis[s.pos]+Len) {Wuyidis[pos]=dis[s.pos]+Len; thebel[pos]=Bel[s.pos]; -e.len=Dis[pos]; Wue.pos=Pos; - Q.push (e); About } $ Else if(bel[pos]!=Bel[s.pos]) { - ///when two cities belong to different metropolises, the nearest distance from A to B is the distance from a to K to B -Ans[bel[pos]]=min (ans[bel[pos]],dis[pos]+dis[s.pos]+len); -Ans[bel[s.pos]]=min (ans[bel[s.pos]],dis[pos]+dis[s.pos]+len); A } + } the } - } $ the the } the the intMain () { -Std::ios::sync_with_stdio (false); in intm; theCin>>n>>m>>p; the for(intI=1; i<=p;i++){ AboutCin>>A[i]; the } the intAA,BB,VV; the while(m--){ +Cin>>aa>>bb>>VV; - Ve[aa].push_back (Make_pair (BB,VV)); the Ve[bb].push_back (Make_pair (AA,VV));Bayi } the Dijstra (); the for(intI=1; i<=p;i++){ - if(i!=1){ -cout<<" "; the } thecout<<Ans[a[i]]; the } thecout<<Endl; -}
View Code
Metropolis (multi-source point shortest)