CSU 1633:landline Telephone Network (minimum spanning tree)

Source: Internet
Author: User

1633:landline Telephone Network Time limit: 2 Sec Memory Limit: MB
Submit: Solved: 8
[Submit] [Status] [Web Board] 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 Test instructions: There are n points, m bars, p unsafe points (p<=n), and the next line of input p different numbers represent unsafe points. Then input m row a B C, indicating that point A and B are connected to the cost of C,   now need to connect all the points to spend the least, but must meet a condition: any point A to any point B through the path can not go through the unsafe point C   (A,b,c are different), Ask at least how much to make this n points connected, if not meet the requirements of the output impossible problem solving: The first n points divided into two parts: safety point, unsafe point. First of all, the safety point with the prime algorithm to build a minimum spanning tree, if not built can be output impossible, if possible, and then to this tree add unsafe point as a leaf node, each add an unsafe point to find a connection with the security of the minimum edge. You cannot full point a requirement if you cannot find a secure point to which it is attached. There is a special sentence: the safety point is 0, the unsafe points are equal to 1 or 2 or >=3.
#include <stdio.h> #include <string.h>const int N = 1005;const int INF = 1<<28;int Mapt[n][n],n,flag[n],    P;int Prime () {int s[n],dis[n],mint,m,tm,k=0,sum=0;        for (int i=1; i<=n; i++) {s[i]=0; dis[i]=inf;    if (flag[i]==0) m=i,k=1;    } if (k==0) return 0; dis[m]=0; S[m]=1;    Tm=m;        while (k<n-p) {mint=inf; for (int i=1; i<=n; i++) if (!s[i]&&flag[i]==0) {if (Dis[i]>mapt[m][i]) dis[i]            =mapt[m][i];        if (mint>dis[i]) mint=dis[i],tm=i;        } if (Mint==inf) return-1; M=tm; S[m]=1; Sum+=mint;    k++; } return sum;}    int main () {int m,a,b,c,aa[n];            while (scanf ("%d%d%d", &n,&m,&p) >0) {for (int i=1; i<=n; i++) {flag[i]=0;        for (int j=1; j<=n;j++) Mapt[i][j]=inf;            } for (int i=1; i<=p; i++) {scanf ("%d", &a); Flag[a]=1;        Aa[i]=a; }        while (m--) {scanf ("%d%d%d", &a,&b,&c);        if (c<mapt[a][b]) mapt[a][b]=mapt[b][a]=c;        } int sum=prime ();        if (sum==-1) printf ("impossible\n");                 else{if (n-p==0) {if (p==1) printf ("0\n");                    else if (p==2) {a=aa[1]; b=aa[2];                    if (mapt[a][b]==inf) printf ("impossible\n");                 else printf ("%d\n", Mapt[a][b]);                 } else printf ("impossible\n");             Continue            } int i,j,mint;                for (I=1; i<=p; i++) {a=aa[i]; mint=inf;                for (j=1;j<=n;j++) if (mapt[a][j]<mint&&flag[j]==0) mint=mapt[a][j];                if (mint==inf) break;            Sum+=mint;  } if (i<=p) printf ("impossible\n");          else printf ("%d\n", sum); }} return 0;}


CSU 1633:landline Telephone Network (minimum spanning tree)

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.