CSU1633: Landline Telephone Network
来源:互联网 发布:webpack.prod.js 编辑:程序博客网 时间:2024/06/10 17:12
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 111 2 11 3 11 4 12 3 22 4 43 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-8struct 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;}
- CSU1633: Landline Telephone Network
- CSUOJ--1633 Landline Telephone Network
- CSU 1633: Landline Telephone Network (最小生成树)
- Telephone
- PSTN(Public Switched Telephone Network, 公共交换电话网络)简介
- telephone conference
- telephone interview
- Android telephone
- Telephone interview
- Telephone Meeting
- The Telephone
- Telephone Lines
- Available Telephone Service Features
- Telephone Call(R)
- Telephone Interview Summary
- poj 3612 Telephone Wire
- SGU 127 Telephone directory
- [SGU]127. Telephone directory
- Unity3D入门——初识Unity3D
- SQL 常用语句
- JS 拖拽原理
- 投影矩阵的推导(Deriving Projection Matrices)(转)
- 【oracle11g自增主键】
- CSU1633: Landline Telephone Network
- C 语言restrict 关键字的使用浅谈
- osx : Linking a Library to xcode
- Android回调函数理解
- Anroid之WakeLock
- 有序数组,输出和为m的所有数对(pair)
- Maven运行JUnit测试之mvn test的默认行为
- 文章标题
- ORA-28001: the password has expired