Electrification Plan

时间限制: 1 Sec  内存限制: 128 MB
提交: 31  解决: 13
[提交][状态][讨论版]

题目描述

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 i, j it is possible to build a power line between them in c[i][j] 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.

输入

多组测试数据。

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 × n table of integers c[i][j] (0 ≤ c[i][j] ≤ 10^5). It is guaranteed that c[i][j] = c[j][i], c[i][j] > 0 for i ≠ j, c[i][i] = 0.

输出

Output the minimum cost to electrify all the cities.

样例输入

4 2
1 4
0 2 4 3
2 0 5 2
4 5 0 1
3 2 1 0

样例输出

3

输入的时候很巧妙,要把发电站相互之间的权值设为0

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f;
using namespace std;
int n, k;
int di[];
int o;
int pra[];
int sum = ;
struct node
{
int u, v, w;
}a[];
bool cmp(node a, node b)
{
return a.w < b.w;
}
int find(int x)
{
if (pra[x] == x) return x;
else return pra[x] = find(pra[x]);
}
bool same(int x,int y)
{
return find(x) == find(y);
}
void unite(int xx,int yy)
{
int x = find(xx);
int y = find(yy);
if (x == y) return;
else pra[x] = y;
}
void kruskal()//套模版
{
sum = ;
int i;
sort(a + , a + o, cmp);
for (i = ; i <= o - ;i++)
{
node x = a[i];
if (same(a[i].u, a[i].v)) continue;
else
{
sum = sum + a[i].w;
unite(a[i].u, a[i].v);
}
}
}
int main()
{
while (cin >> n >> k)
{
int i, j;
for (i = ; i <= k; i++)
{
cin >> di[i];//发电站
}
o = ;
for (i = ; i <= k; i++)
{
for (j = ; j <= k; j++)
{//把发电站之间的w设为0就可以了
a[o].u = di[i];
a[o].v = di[j];
a[o++].w = ;
}
}
for (i = ; i <= n; i++)
{
pra[i] = i;
for (j = ; j <= n; j++)
{
a[o].u = i;
a[o].v = j;
cin >> a[o++].w;
}
}
kruskal();
cout << sum << endl;
}
}

 

zufeoj Electrification Plan (最小生成树,巧妙设e[i][j]=0)的更多相关文章

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

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

  2. Electrification Plan(最小生成树)

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

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

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

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

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

  5. Timusoj 1982. Electrification Plan

    http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...

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

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

  7. 设子数组A[0:k]和A[k+1:N-1]已排好序(0≤K≤N-1)。试设计一个合并这2个子数组为排好序的数组A[0:N-1]的算法。

    设子数组A[0:k]和A[k+1:N-1]已排好序(0≤K≤N-1).试设计一个合并这2个子数组为排好序的数组A[0:N-1]的算法.要求算法在最坏情况下所用的计算时间为O(N),只用到O(1)的辅助 ...

  8. JBPM5流程设计器jbpm-designer-2.4.0.Final-tomcat.war的部署没法访问的问题

    转自:http://blog.csdn.net/steveguoshao/article/details/8840607 在http://sourceforge.net/projects/jbpm/f ...

  9. [数据结构]最小生成树算法Prim和Kruskal算法

    最小生成树 在含有n个顶点的连通图中选择n-1条边,构成一棵极小连通子图,并使该连通子图中n-1条边上权值之和达到最小,则称其为连通网的最小生成树.  例如,对于如上图G4所示的连通网可以有多棵权值总 ...

随机推荐

  1. hibernate一对一关联

    hibernate一对一主键关联 一对一主键关联指的是两个表通过主键形成的一对一映射. 数据表要求:A表的主键也是B表的主键同时B表的主键也是A表的外键 sql: create table peopl ...

  2. MacBook常用软件

    本文分享一些我在mac上的常用软件,也为以后重新配置工作环境做一个记录. 其中提到的大多数软件在网上都有丰富教程,所以仅仅简单描述其功能,不再赘述. 通用 ---Typora Markdown写作工具 ...

  3. Object -c基础知识(5)--release 之后 retainCount为何为1

    在XCode中加入如下代码:  UILabel *label=[UILabel alloc]; [label setText:@"TestLabel"]; NSLog(@" ...

  4. Linux shell —— 数组与关联数组

    使用 declare -A(declare 的用法请使用 help 进行查看,help declare) 进行声明关联数组变量: $ declare -A fruits_price $ fruits_ ...

  5. BZOJ3864: Hero meet devil【dp of dp】

    Description There is an old country and the king fell in love with a devil. The devil always asks th ...

  6. (2)bytes类型

    bytes类型就是字节类型 把8个二进制一组称为一个byte,用16进制来表示 Python2里面字符串其实更应该称为字节串,但是python2里面有一个类型是butes,所以在Python2里面by ...

  7. GridView实现数据编辑和删除

    <asp:GridView ID="gv_Emplogin" runat="server" AutoGenerateColumns="False ...

  8. GridView 相同单元格合并

    效果如下: 主要代码如下:public class GridDecorator { public static void MergeRows(GridView gridView) { for (int ...

  9. 模板引擎jade学习

    语言参考 标签列表 doctype Tags Block Expansion Attributes Boolean Attributes Class Attributes Class Literal ...

  10. mac下hbase安装

    出处:https://www.jianshu.com/p/510e1d599123 安装到的路径:/usr/local/Cellar/hbase/1.2.6 linux操作: linux命令 作用 . ...