缩点:Power Plant;

题目传送门:[UVALive 6437]Power Plant

题目大意:T组数据,给定一幅带权图(n, m), 然后给定k个点, 与图中存在有若干条边。每个点都要至少要和这k个点的一个点直接或间接相连, 问最少的距离是多少。 1 ≤ T ≤ 100;

因为除了这k个点,其他的点是一个连通块,所以当前这个k点与其相连,我们并不需要知道原图中的点和i-k中哪个点相连,所以我们可以做一个超级汇点,让所有加的边与K相连;

即做了一个缩点的操作);做一遍最小生成树,那么这k个点必定会被选到;

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAXN 205
using namespace std;
struct node
{
       int u,v,d;
}e[MAXN*MAXN];
int father[MAXN];
int getfather(int x)
{
      if (father[x]==x) return x;
      return father[x]=getfather(father[x]);
}
bool cmp(node a,node b)
{
      return a.d<b.d;
}
int main()
{
      int C,ca,n,m,k,i,u,v,f,ans;
      scanf("%d",&C);
      for (ca=1;ca<=C;ca++)
      {
               scanf("%d%d%d",&n,&m,&k);
               for (i=1;i<=n;i++) father[i]=i;
               scanf("%d",&f);
               for (i=2;i<=k;i++) scanf("%d",&u),father[u]=f;
               for (i=1;i<=m;i++)
                   scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].d);
               sort(e+1,e+1+m,cmp);
               ans=0;
               for (i=1;i<=m;i++)
               {
                        u=e[i].u,v=e[i].v;
                        int fx=getfather(u),fy=getfather(v);
                        if(fx!=fy)
                        {
                            father[fx]=fy;
                            ans+=e[i].d;
                        }
               }
               printf("Case #%d: %d\n",ca,ans);
      }
      return 0;
}

原文地址:https://www.cnblogs.com/Tyouchie/p/10366967.html

时间: 2024-08-05 15:21:20

缩点:Power Plant;的相关文章

cf 434d Nanami&#39;s Power Plant 网络流

题目大意就是有n个发电机,每个发电机有l到r个档位可供选择,每个档位的输出功率是已知的,另外还有一些限制条件,形式为xu ≤ xv + d,表示发电机u的档位要小于v的档位加d,d是一个已知的整数.求n个发电机的最大功率. 假设没有最后那个限制条件,那么对于每个发电机i拆点成l-1,l...到r相邻两档位连边为max-f(i),f(i)是档位i的输出功率,max是一个大数,大于等于所有档位的输出功率.l-1与源点连inf的边,r与汇点连inf的边.假设最大流为flow,那么max*n-flow就

uva 6437 - Power Plant【最小生成树】

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4448 题目大意:和基本的最小生成树想比,其在最开始的时候有多个起点,从任一起点开始生成都可以. 一直到最后才发现做法,就是把起始点都提前加入生成树里面. #include<iostream> #include<stdio.h> #includ

Nuclear Power Plant ZOJ - 3840 树形dp

There are N (1 ≤ N ≤ 105) cities on land, and there are N - 1 wires connecting the cities. Therefore, each city can transmit electricity to all other cities by these wires. Dark_Sun made a decision to build a nuclear power plant to supply electricity

LA 6437 Power Plant (prim最小生成树)

还是裸的最小生成树 #include<bits/stdc++.h> using namespace std; int T,N,M,P,K,a,b,c; int dist[1020],m[1020][1020]; bool p[1020]; void prim() { for(int i=1;i<=N;i++) { if(!p[i]) { int Min=1000020; for(int j=1;j<=N;j++) { if(p[j]&&m[i][j]!=0&

用scikit-learn和pandas学习线性回归

对于想深入了解线性回归的童鞋,这里给出一个完整的例子,详细学完这个例子,对用scikit-learn来运行线性回归,评估模型不会有什么问题了. 1. 获取数据,定义问题 没有数据,当然没法研究机器学习啦.:) 这里我们用UCI大学公开的机器学习数据来跑线性回归. 数据的介绍在这: http://archive.ics.uci.edu/ml/datasets/Combined+Cycle+Power+Plant 数据的下载地址在这: http://archive.ics.uci.edu/ml/ma

【字源大挪移—读书笔记】 第二部分:字根

[2] 字根:[2.1]表示[否定]的字根.[2.2]表示[方位]的字根.[2.3]表示[程度]的字根.[2.4]表示[状态]的字根.[2.5]表示[现象]的字根.[2.6]表示[身体]的字根.[2.7]表示[姿势]的字根.[2.8]表示[心,心里活动]的字根.[2.9]表示[行为动作]的字根.[2.10]表示[感官动作]的字根.[2.11]表示[感觉]的字根.[2.12]表示[生命]的字根.[2.13]表示[死亡]的字根.[2.14]表示[社会]的字根 [2.1]表示[否定]的字根 -neg-

HDU 4756 Install Air Conditioning

Install Air Conditioning Time Limit: 2000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ID: 475664-bit integer IO format: %I64d      Java class name: Main   NJUST carries on the tradition of HaJunGong. NJUST, who keeps up the ”

Codeforce 水题报告(2)

又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数(n<=200). 首先很明显可能是区间dp,我们可以记f[i][j]为从i到j的这个多边形的三角剖分数,那么f[i][j]=f[i][k]*f[j][k]*(i,j,k是否为1个合格的三角形) CodeForces 438D:The Child and Sequence 描述:给一个序列,要求支持区

UVA - 11986 Save from Radiation

Description J Save from Radiation Most of you are aware of Nuclear Power Plant Explosion at Fukushima after devastating earth quake and tsunami. Many people in Bangladesh were seen to be concerned with radiation. The message says: BBC Flash news: Jap