Csu1633:landline Telephone Network

Source: Internet
Author: User

Description

The mayor of Rmrcity wants to create a secure landline telephone network for emergency use with case of serious disasters WH En the city was cut off from the outside world. Some pairs of buildings in the city can is directly connected with a wire telephone line and the municipality engineers ha ve prepared an estimate of the cost of connecting any such pair.
The mayor needs your help to find the cheapest network, connects all buildings in the city and satisfies a particular Security measure that'll be explained shortly. A-building a to another building B is routed through any simple path in the network (i.e., a path that doe s not has any repeated building). There is also some insecure buildings that one or more persons with serious criminal records live in. The mayor wants only communications intended for these insecure buildings to reach them. In other words, no communication from any building A to any building B should pass through any insecure building C in the Network (where C is different from A and B).

Input

The input consists of a single test case. The first line contains three integers n, m, p where 1≤n≤1000 are the number of buildings, 0≤m≤100000 is the number of possible direct connections between a pair of buildings, and 0≤p≤n is the number of insecure buildings. The buildings is numbered from 1 to N.
The second line contains p distinct integers between 1 and n (inclusive), which is the numbers of insecure buildings. Each of the next m lines contains three integers xi, Yi, and Li describing one potential direct line, where Xi and Yi (1≤ XI, yi≤n) was the distinct buildings the line connects, and Li (1≤li≤10000) was the estimate of the cost of CONNECTI ng these buildings. There is at the most one direct link between any and buildings in these m lines.

Output

Display the cost of the cheapest network satisfying the security measure if it is possible. Otherwise, display
Impossible.

Sample Input
4 6 111 2 11 3 11 4 12 3 22 4 43 4 3
Sample Output
6
HINT

Source
Test instructions: Give a diagram, but the point is not safe, now requires the completion of a tree, so that unsafe points all do leaf nodes, the output of the minimum cost
Idea: First put all the safety points and check the integration of a tree, and then the non-safety node to connect up
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <bitset># Include <algorithm> #include <climits>using namespace std; #define LS 2*i#define RS 2*i+1#define Up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __gcd (A, b) #define LL long long#define N 25#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e-8str UCT node{int u,v,w;} e[100005];int father[1005],vis[1005],cnt[1005];int N,m,p;int cmp (node A,node b) {return a.w&lt ; B.W;}    int find (int x) {if (x = = Father[x]) return x; Return Father[x]=find (Father[x]);}    int main () {int i,j,k,x,y,u,v,w,flag,ans;        W (~scanf ("%d%d%d", &n,&m,&p)) {flag = ans = 0;        MEM (vis,0);        MEM (cnt,0);  Up (i,1,n) father[i]=i;      Up (i,1,p) {scanf ("%d", &x);        VIS[X] = 1;            } up (I,1,m) {scanf ("%d%d%d", &u,&v,&w);            E[i].u=u;            E[i].v=v;        E[i].w=w;            } if (n==2) {printf ("%d\n", W);        Continue        } sort (e+1,e+1+m,cmp);            Up (i,1,m) {u = E[I].U,V=E[I].V,W=E[I].W; if (vis[u]| |            VIS[V]) continue;            x = Find (u), y = find (v);            if (x==y) continue;            Father[x] = y;        Ans+=w;            } up (i,1,m) {u = E[I].U,V=E[I].V,W=E[I].W; if (! (            VIS[U]^VIS[V]) continue;            cnt[u]++;            cnt[v]++;            if (vis[u]&&cnt[u]>=2) continue;            if (vis[v]&&cnt[v]>=2) continue;            x = Find (u), y = find (v);            if (x==y) continue;            Father[x] = y;        Ans+=w; } up (I,1,n) {if (father[i]==i) flag++;        } if (flag>=2) printf ("impossible\n");    else printf ("%d\n", ans); } return 0;}


Csu1633:landline Telephone Network

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.