http://acm.timus.ru/problem.aspx?space=1&num=1982

1982. Electrification Plan

Time limit: 0.5 second
Memory limit: 64 MB
Some country has n cities. The government has decided to electrify all these cities. At first, power stations in k different cities were built. The other cities should be connected with the power stations via power lines. For any cities ij it is possible to build a power line between them incij roubles. The country is in crisis after a civil war, so the government decided to build only a few power lines. Of course from every city there must be a path along the lines to some city with a power station. Find the minimum possible cost to build all necessary power lines.

Input

The first line contains integers n and k (1 ≤ k ≤ n ≤ 100). The second line contains k different integers that are the numbers of the cities with power stations. The next n lines contain an n × ntable of integers {cij} (0 ≤ cij ≤ 105). It is guaranteed that cij = cjicij > 0 for i ≠ jcii = 0.

Output

Output the minimum cost to electrify all the cities.

Sample

input output
4 2
1 4
0 2 4 3
2 0 5 2
4 5 0 1
3 2 1 0
3
Problem Author: Mikhail Rubinchik 
Problem Source: Open Ural FU Championship 2013

分析:

无向图,给n个点,n^2条边,每条边有个一权值,其中有k个点有发电站,给出这k个点的编号,选择最小权值的边,求使得剩下的点都能接收到电。

  发电站之间显然不能有边,那么把k个点合成一个点,然后在图上就MST就可以了。

AC代码1:

    1、edge[i][j]=0是个很巧妙的设置。

2、求最小生成树,由于生成树是图的极小联通子图。最小生成树一定要包含图中所有的点。

 #include<cstdio>
#include<iostream>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std; int edge[][];
int vis[],dis[];
bool flag[];
int n,k,ans; void prim()
{
int u=,minw;
for(int i=;i<=n;i++)
{
vis[i]=;
dis[i]=edge[u][i];
}
vis[u]=;
for(int i=;i<n;i++)
{
minw=INF;
for(int j=;j<=n;j++)
{
if(!vis[j] && dis[j]<minw)
{
minw=dis[j];
u=j;
}
}
ans+=minw;
vis[u]=;
for(int j=;j<=n;j++)
{
if(!vis[j] && edge[u][j]<dis[j])
dis[j]=edge[u][j];
}
}
} int main()
{
int d;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(vis,,sizeof(vis));
memset(flag,false,sizeof(flag));
ans=;
for(int i=;i<k;i++)
{
scanf("%d",&d);
flag[d]=true;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&edge[i][j]);
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(flag[i]&&flag[j])
edge[i][j]=;
}
}
prim();
printf("%d\n",ans);
}
return ;
}

AC代码2:

 //STATUS:C++_AC_31MS_401KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v,val;
bool operator < (const Edge& a)const {
return val<a.val;
}
}e[N*N];
int n,k;
int id[N],p[N],w[N][N]; int find(int x){return p[x]==x?x:p[x]=find(p[x]);} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,x,y,ans,cnt;
while(~scanf("%d%d",&n,&k))
{
mem(id,);
for(i=;i<k;i++){
scanf("%d",&a);
id[a]=;
}
k=;
for(i=;i<=n;i++){
if(id[i])continue;
id[i]=k++;
}
mem(w,INF);
for(i=;i<=n;i++){
for(j=;j<=n;j++){
scanf("%d",&a);
w[id[i]][id[j]]=Min(w[id[i]][id[j]],a);
}
}
cnt=;
for(i=;i<k;i++){
for(j=i+;j<k;j++){
e[cnt].u=i,e[cnt].v=j;
e[cnt].val=w[i][j];
cnt++;
}
}
sort(e,e+cnt);
ans=;
for(i=;i<k;i++)p[i]=i;
for(i=;i<cnt;i++){
x=find(e[i].u);y=find(e[i].v);
if(x!=y){
p[y]=x;
ans+=e[i].val;
}
} printf("%d\n",ans);
}
return ;
}

Timusoj 1982. Electrification Plan的更多相关文章

  1. timus 1982 Electrification Plan(最小生成树)

    Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The govern ...

  2. zufeoj Electrification Plan (最小生成树,巧妙设e[i][j]=0)

    Electrification Plan 时间限制: 1 Sec  内存限制: 128 MB提交: 31  解决: 13[提交][状态][讨论版] 题目描述 Some country has n ci ...

  3. URAL-1982 Electrification Plan 最小生成树

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982 题意:无向图,给n个点,n^2条边,每条边有个一权值,其中有k个点有发电站,给出这 ...

  4. Electrification Plan(最小生成树)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=50#problem/D 最小生成树模板,注意的是这里有k个发电站,它们不再需要连 ...

  5. Electrification Plan 最小生成树(prim+krusl+堆优化prim)

    题目 题意: 无向图,给n个城市,n*n条边,每条边都有一个权值 代表修路的代价,其中有k个点有发电站,给出这k个点的编号,要每一个城市都连到发电站,问最小的修路代价. 思路: prim:把发电站之间 ...

  6. URAL-1982-Electrification Plan最小生成树或并查集

    Electrification Plan 题意:在一个无向图中,给你几个源点,找出把所有点连接到源点后最小的消费: 可以利用并查集: 先用结构体把每个边存起来,再按照消费大小排序.之后从消费小的到大的 ...

  7. Prim && Kruskal

    Electrification Plan Prim #include<iostream> #include<cstring> using namespace std; cons ...

  8. 测试计划(Test Plan)

    测试计划(Test Plan) 版权声明:本文为博主原创文章,未经博主允许不得转载. 测试计划的概念: 测试计划是一个文档,描述了进行测试的测试范围,测试策略和方法,测试资源和进度.是对整个测试活动进 ...

  9. SQL Tuning 基础概述02 - Explain plan的使用

    1.explain plan的使用 SQL> explain plan for delete from t_jingyu; Explained. SQL> select * from ta ...

随机推荐

  1. 逗号分割符--字段中含逗号等情况的解析方法Java实现

    最近在处理文本字符串时,没一行数据都是按照逗号分割的,每个字段值一般情况是带有双引号的,但是有的字段值里面还包含逗号,甚至有的字段就没有双引号,这个分割起来就有点麻烦了 下面说一下我解决方法,如果谁有 ...

  2. 创业方向:O2O及移动社交 from 沈博阳

    十个最大的互联网公司,六个在美国,四个在中国 中国创业的特殊 市场巨大self-contained,做足中国市场就很够 ... ... 监管   领导力,人的要求 之前成功带领过团队 有海外的工作经历 ...

  3. java基础-继承

    浏览以下内容前,请点击并阅读 声明 一个由其他类继承的类叫子类(也叫继承类,扩展类等),该类继承的类叫父类或超类.除了Object类意外,所有的类都有切仅有一个父类,如果一个类没有用extends关键 ...

  4. Android入门(一):Android发展史

    Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发.尚未有统一中文名称,中国大陆地区较多人使用“安卓” ...

  5. 学习js 优先级

    以前很少关注js优先级 主要哦是技术菜鸟老加班没时间技术菜鸟 最重要的是记不住特点.......... 1级 . [] () 字段访问.数组索引.函数调用和表达式分组 通过观察可以发现 . 字段访问- ...

  6. type of 操作符和instanceof操作符的区别以及使用方法

    经常见到用typeof和instanceof检测一个变量类型,作为前端小白经常不知道这两者具体的详细用法和区别,今天就整理一下谨记! javaScript中有6中数据类型: 1.Undefinde 2 ...

  7. [代码]label增加删除线

    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 100, 30)];    [self.view addSubvi ...

  8. How does Unity.Resolve know which constructor to use?

    Question: Given a class with several constructors - how can I tell Resolve which constructor to use? ...

  9. 《bootstrap》实战---小问题,大Bug

    参照书中代码写了个示例,能够实现大页面单行导航,小页面显示收缩按钮,但是就是不能让收缩按钮发挥作用.也不知道哪儿出了问题. 想想算了,代码也不多,重新来吧.写道导航的时候,突然发现一个<nav& ...

  10. Python实战:扫描key完整性

    之前在国际版本中,需要支持中英文切换功能,在如此繁多的源文件里要查找源文件里的key是语言资源包是否对应. 正好运用在之前学的python,写了个工具,支持自定义替换标签,批量处理源文件.现在看来,效 ...