CSUOJ--1633 Landline Telephone Network

其实就是一道最小生成树的题目.我们只需要将坏点排除在外,然后对其它点做一次最小生成树,然后在将坏点连接到这颗生成树上(每次都选代价最小的点).当然如果最后不是所有的点都连接到一起,就是impossible了.

还有一种情况需要特判,就是如果一共只有两个点,并且都是坏点,这种情况也是合法的.

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

#define maxn 210000
#define ll long long
#define INF 0x3f3f3f3f

vector<int>g[maxn],w[maxn];

typedef struct
{
    int x,y,w;
}P;
P p[maxn];

int cmp(P p1,P p2)
{
    return p1.w<p2.w;
}

int e,fa[maxn];
int a[maxn],flag[maxn];

int Find(int x)
{
     return fa[x]==x?x:Find(fa[x]);
}

///如果只有两个点,且全部是坏点则要特判
int main()
{
      int n,m,k;
     // freopen("in.txt","r",stdin);
      while(scanf("%d%d%d",&n,&m,&k)!=EOF)
      {
              int h;
              memset(flag,0,sizeof(flag));
              for(int i=1;i<=n;i++)
                  fa[i]=i;
              for(int i=1;i<=n;i++)
                 g[i].clear(),w[i].clear();
              for(int i=0;i<k;i++)
              {
                    scanf("%d",&a[i]);
                    flag[a[i]]=1;
              }
              e=0;
              for(int i=1;i<=m;i++)
              {
                    int x,y,z;
                    scanf("%d%d%d",&x,&y,&z);
                    if(k==2&&x==a[0]&&y==a[1]) h=z;
                    if(k==2&&y==a[0]&&x==a[1]) h=z;
                    p[e].x=x; p[e].y=y; p[e].w=z; e++;
                    p[e].x=y; p[e].y=x; p[e].w=z; e++;
                    g[x].push_back(y); w[x].push_back(z);
                    g[y].push_back(x); w[y].push_back(z);
              }
              if(n==2&&k==2)
              {
                    printf("%d\n",h);
                    continue;
              }
              sort(p,p+e,cmp);
              int cnt=1;
              bool bl=true;
              ll ans=0;
              for(int i=0;i<e;i++)
              {
                   int x=p[i].x,y=p[i].y;
                   if(!flag[x]&&!flag[y])
                   {
                         int xx=Find(x);
                         int yy=Find(y);
                         if(xx!=yy)
                         {
                              ans+=p[i].w; cnt++;
                              fa[xx]=yy;
                         }
                   }
              }
              if(cnt<n-k)  bl=false;
              int sum1=0,sum2=0;
              for(int i=0;i<k;i++)
              {
                     int x=a[i],y,tmp=INF;
                     for(int j=0;j<g[x].size();j++)
                     {
                           y=g[x][j];
                           if(flag[y])
                              continue;
                           if(tmp>w[x][j])
                               tmp=w[x][j];
                     }
                     if(tmp<INF)
                        ans+=tmp,cnt++;
              }
              if(cnt<n) bl=false;
              if(!bl)
                  printf("impossible\n");
              else
                  printf("%lld\n",ans);
      }
   return 0;
}
时间: 2024-08-27 12:29:37

CSUOJ--1633 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

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

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利用代理服务器向用户