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. 宇宙最帅叉叉——第五周博客 for 测试与发布(Alpha版本)

    Alpha版本测试报告 1.在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? a.修复的BUG UDP传输 recvfrom 当没有消息来的时候一直循环等待因其阻塞 ,时间戳无效了. ...

  2. 免费申请一年版Eset NOD32 Antivirus激活码(无限制)- 已失效

    You will receive a 1-year license of the antivirus software Eset NOD32 (version 6), including all up ...

  3. Linux内核参数优化记录

    //fs.file-max 最大打开文件数 //fs.nr_open=20480000 单个进程允许打开的文件句柄上限 //信号量及共享内存,可以使用ipcs -l来获取 //kernel.sem 信 ...

  4. ios 从网络上获取图片

    -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIImage * result; N ...

  5. Okhttp之CallServerInterceptor简单分析

    在Okhttp源码分析专栏的几篇博客分析了Okhttp几个拦截器的主要功能,还剩下最后一个拦截器CallServerInterceptor没有分析,本篇博客就简单分析下该拦截器的功能. 在Okhttp ...

  6. 题目1003:A+B

    题目1003:A+B 时间限制:1 秒内存限制:32 兆 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计算A+B的结果,并以正常形式 ...

  7. IDEA错误:Cannot start compilation: the output path is not specified for module "XXX".

    错误是发生在从github上checkout自己的项目时.因为没有将配置文件一起上传,所以在运行java程序时有了这个报错: Cannot start compilation: the output ...

  8. PAT 团体程序设计天梯赛 L1-046 整除光棍(模拟除法)

    L1-046. 整除光棍 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 翁恺 这里所谓的"光棍",并不是指单身汪 ...

  9. centos重启redis后,数据丢失

    编辑/etc/sysctl.conf ,改vm.overcommit_memory=1, 然后sysctl -p 使配置文件生效 T

  10. hdu1097

    hdu1097 求a^b的末位数 打表O(1) import java.util.*; public class Main { static int [][]a = new int[15][15]; ...