Description
The mayor of RMRCity wants to create a secure landline telephone network for emergency use in case of serious disasters when the city is cut off from the outside world. Some pairs of buildings in the city can be directly connected with a wire telephone line
and the municipality engineers have prepared an estimate of the cost of connecting any such pair.
The mayor needs your help to find the cheapest network that connects all buildings in the city and satisfies a particular security measure that will be explained shortly. A call from a building A to another building B may be routed through any simple path in
the network (i.e., a path that does not have any repeated building). There are 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 is 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 are numbered from 1 to n.
The second line contains p distinct integers between 1 and n (inclusive), which are 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) are
the distinct buildings the line connects, and li (1 ≤ li ≤ 10000) is the estimate of the cost of connecting these buildings. There is at most one direct link between any two 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 1 1 1 2 1 1 3 1 1 4 1 2 3 2 2 4 4 3 4 3
Sample Output
6
HINT
Source
题意:给出一个图,但是又的点是不安全的,现在要求建成一颗树,让不安全的点全部做叶子节点,输出最小花费
思路:先把所有安全点并查集成一颗树,再把不安全节点一个个的连上去
#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-8 struct 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<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; }