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. 小知识,用myeclipes找jar

    有些时候,换台电脑就换了一个框架的版本,找对应的jar是一件非常麻烦的事. 我们可以使用myeclipes快速得到我们想要的jar 首先新建一个项目,然后使用myeclipes的自动生成框架技术,生成 ...

  2. Python 水果统计

    f = open("水果.txt", mode="r", encoding="utf-8") lst = [] for line in f: ...

  3. C语言基础:函数指针 分类: iOS学习 c语言基础 2015-06-10 21:55 15人阅读 评论(0) 收藏

    函数指针:指向函数的指针变量. 函数名相当于首地址. 函数指针定义:返回值类型  (*函数指针变量名)(参数类型1,参数类型2,....)=初始值 函数指针类型:返回值类型  (*)(参数类型1,参数 ...

  4. 1.1 Linux中的进程 --fork、孤儿进程、僵尸进程、文件共享分析

    操作系统经典的三态如下: 1.就绪态 2.等待(阻塞) 3.运行态 其转换状态如下图所示: 操作系统内核中会维护多个队列,将不同状态的进程加入到不同的队列中,其中撤销是进程运行结束后,由内核收回. 以 ...

  5. matlab mod()&rem()

    一.介绍rem/mod(X,Y)当X,Y符号相同的时候,这两者一样,当符号不相同的时候才有差别,具体表现在rem结果的符号与被除数相同:mod结果的符号与除数相同 二.例子 >> a=9; ...

  6. chapter02 回归模型在''美国波士顿房价预测''问题中实践

    #coding=utf8 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_boston # 从sklearn.mo ...

  7. POJ 3069:Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13242   Accepted: 6636 D ...

  8. NeighboringCellInfo.aidl

    在src下先建立包名为android.telephony(右键src > new > package,create package-info.java打钩),然后右键刚建的android. ...

  9. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  10. 字符串的比较【string和字符数组】

    无论是string 还是 字符数组的字符串比较函数,返回的都是字典序的大小.如 1234 和 5 比较时就是1234的字典序小于5,要想比较字符串表示的数字的大小,需要自己写函数比较