CSU1633: Landline Telephone Network

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;
}
时间: 2024-08-03 13:38:49

CSU1633: Landline Telephone Network的相关文章

CSU 1633: Landline Telephone Network (最小生成树)

1633: Landline Telephone Network Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 63  Solved: 8 [Submit][Status][Web Board] Description The mayor of RMRCity wants to create a secure landline telephone network for emergency use in case of serious disas

CSUOJ--1633 Landline Telephone Network

其实就是一道最小生成树的题目.我们只需要将坏点排除在外,然后对其它点做一次最小生成树,然后在将坏点连接到这颗生成树上(每次都选代价最小的点).当然如果最后不是所有的点都连接到一起,就是impossible了. 还有一种情况需要特判,就是如果一共只有两个点,并且都是坏点,这种情况也是合法的. 代码如下: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #incl

UVA 1456 六 Cellular Network

Cellular Network Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1456 A cellular network is a radio network made up of a number of cells each served by a base station located in the cell. The base sta

UVa1456Cellular Network(概率DP)

Cellular Network Description A cellular network is a radio network made up of a number of cells each served by a base station located in the cell. The base station receives call signals from mobile users (mobiles) in the cell it serves, which then co

湖南多校对抗5.24

据说A,B,C题都比较水这里就不放代码了 D:Facility Locations 然而D题是一个脑经急转弯的题:有m行,n列,每个位置有可能为0,也可能不为0,问最多选K行是不是可以使得每一列都至少有一个0,其中代价c有个约束条件:These costs satisfy a locality property: for two clients j and j’ and two facilities i and i’, we have cij ≤ ci’j + ci’j’ + cij’ . 一看

Lists of network protocols

https://en.wikipedia.org/wiki/Lists_of_network_protocols Protocol stack: List of network protocol stacks WIFI/WIMAX Protocols Bluetooth protocol Fibre Channel network protocols Internet Protocol Suite or TCP/IP model or TCP/IP stack OSI protocols fam

Smart internet of things services

A method and apparatus enable Internet of Things (IoT) services based on a SMART IoT architecture by integrating connectivity, content, cognition, context, cloud, and collaboration. Joint optimization of a combination of any of connectivity, content,

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

SIP 协议文档翻译

SIP:会话发起协议 文档地位 这篇文档制订了一个用于互联网通信,请求讨论和提升建议的互联网标准追踪协议.请参考“互联网官方协议标准”为了这个协议的标准化状态.转发不限! 版权声明 版权(c)互联网社区(2002).保留所有权利! 概要 这篇文档描述了会话发起协议(SIP),一个用于创建,修改,和终止会话与一个或更多参与者的应用层控制(发信号)协议.这些会话包括互联网电话,多媒体分发和多媒体会议. SIP邀请用于创建会话携带允许参与者同意一系列的兼容媒体类型的会话描述.SIP利用代理服务器向用户