1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include<cstdio> #include<algorithm> using namespace std; int f[10100]; struct numm { int x,y; int v; }qwq[10100]; bool cmp(numm a,numm b) { return (a.v<b.v); } int find( int x) { while (x!=f[x]) x=f[x]; return x; } int main() { int fx,fy,qxq; int dis,ans; int R[110]; int len; int a,b,c; int n,m,r; scanf ( "%d%d%d" ,&n,&m,&r); for ( int i=1;i<=r;i++) scanf ( "%d" ,&R[i]); sort(R+1, R+1+r); len=0; for ( int i=1;i<=m;i++) { scanf ( "%d%d%d" ,&a,&b,&c); bool isx,isy; isx=isy= false ; for ( int k=1;k<=r;k++) { if (R[k]==a) isx= true ; if (R[k]==b) isy= true ; if (isx== true && isy== true ) break ; } if (isx== false || isy== false ) continue ; //printf("addpair: %d %d ==%d\n",a,b,c); len++; qwq[len].x=a; qwq[len].y=b; qwq[len].v=c; } qxq=0; ans=0; for ( int i=1;i<=n;i++) f[i]=i; sort(qwq+1,qwq+1+len,cmp); for ( int i=1;i<=len;i++) { fx=find(qwq[i].x); fy=find(qwq[i].y); if (fx!=fy) { f[fx]=fy; qxq++; //printf("addline= %d\n",qwq[i]); ans+=qwq[i].v; if (qxq==r-1) break ; } } printf ( "%d\n" ,ans); return 0; |