http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=50#problem/D

最小生成树模板,注意的是这里有k个发电站,它们不再需要连接其他的发电站,所以任何两个发电站之间的权值是0;

 #include<stdio.h>
#include<string.h>
const int maxn = ;
const int INF = 0x3f3f3f3f;
int map[maxn][maxn],power[maxn];
int n,k; void prim()
{
int dis[maxn],vis[maxn];
int ans = ;
memset(vis,,sizeof(vis));
for(int i = ; i <= n; i++)
dis[i] = map[][i];
vis[] = ; for(int i = ; i <= n-; i++)
{
int min = INF,pos; for(int j = ; j <= n; j++)
{
if(!vis[j] && dis[j] < min)
{
min = dis[j];
pos = j;
}
}
vis[pos] = ;
ans += min;
for(int j =; j <= n; j++)
{
if(!vis[j] && dis[j] > map[pos][j])
dis[j] = map[pos][j];
}
}
printf("%d\n",ans);
} int main()
{
scanf("%d %d",&n,&k); /*for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
if(i != j)
map[i][j] = INF;
else map[i][j] = 0;
}*/
memset(power,,sizeof(power));
int x; for(int i = ; i < k; i++)
{
scanf("%d",&x);
power[x] = ;
} for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
scanf("%d",&map[i][j]);
} for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(power[i] && power[j])
map[i][j] = ;
}
}
prim();
return ;
}

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. URAL-1982 Electrification Plan 最小生成树

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

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

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

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

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

  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. Prim && Kruskal

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

  8. HDU 3080 The plan of city rebuild(除点最小生成树)

    题意  一个城市原来有l个村庄 e1条道路  又添加了n个村庄 e2条道路  后来后销毁了m个村庄  与m相连的道路也销毁了  求使全部未销毁村庄相互连通最小花费  不能连通输出what a pity ...

  9. URAL 1160 Network(最小生成树)

    Network Time limit: 1.0 secondMemory limit: 64 MB Andrew is working as system administrator and is p ...

随机推荐

  1. JDBC中事务的使用

    http://blog.sina.com.cn/s/blog_6f3ca78f01010pmj.html 当Jdbc程序向数据库获得一个Connection对象时,默认情况下这个Connection对 ...

  2. 游标、type使用示例

    declare  my_cur sys_refcursor; --定义游标变量  type v_record is record( --定义 record类型    obj_id   number,  ...

  3. swift-01-利用元组判断字符串出现次数

    //问题的提出:有一个字符串 array = ["1","2","4","4","2"," ...

  4. Codevs 1814 最长链

    1814 最长链 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少, ...

  5. 暑假集训(1)第四弹 -----Find a way(Hdu2612)

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  6. bzoj1478:Sgu282 Isomorphism

    思路:由于题目中是通过改变点的编号来判断两种染色方案是否相同,而染色的确是边,于是考虑如何将点置换转化为边置换. 对于一个有n个点的完全图,其点置换有n!个(即全排列个数),又由于每一个边置换都对应了 ...

  7. 页面mask css

    <html> <head> <style type="text/css"> .share_mask { position: fixed; top ...

  8. mini2440移植uboot-2008.10 遇到的问题

    1.mkimage的使用(u-boot-2008.10/tools/mkimage) 首先./mkimage 运行或者 将mkimage 拷贝到 /bin 目录下面 法一: #mkimage -n ' ...

  9. nodejs实现单文件上传。

    new了formidable的一个实例. formidable模块可以直接捕获当前数据流的状态并返回文件路径. 主要使用了file事件和end事件. var form = new formidable ...

  10. dom例子

    //凡是html标签中的属性和值是一样的,那么在js中用true或者false 1,阅读协议倒计时 <input type="button" name="name& ...