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. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  2. C#微信公众号开发 -- (六)自定义菜单事件之CLICK

    微信公众号中当用户手动点击了按钮,微信公众号会被动的向用户发送文字消息或者图文消息. 通过C#微信公众号开发 -- (五)自定义菜单创建 我们知道了如何将CLICK类型的按钮添加到自己的微信公众平台上 ...

  3. sdk 提示至少需要Build-tools 19.1.0以上的解决方式

    网上搜了很多办法,例如修改host文件或者设置sdk manager tool 为force方式都解决不了,最简单的方式是手动下载build-tools下的包,例如直接百度搜索build-tools ...

  4. Help View修复

    好吧,手贱把ProgramData里关于Help View的某些数据删除了 (在任何情况下都不要删除此文件夹中的任何数据).即使卸载后重新安装也出现错误,可以参考的http://social.msdn ...

  5. 初识Angular2

    Angular2是面向未来的科技,要求浏览器支持ES6+,我们现在要尝试的话,需要加一些 垫片来抹平当前浏览器与ES6的差异: angular2-polyfills - 为ES5浏览器提供ES6特性支 ...

  6. spring mvc 错误

    No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin  //跨域问题 respons ...

  7. HDU 4294 A Famous Equation(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...

  8. LA 3902 Network(树上最优化 贪心)

    Network Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes ...

  9. Entity SQL 初入

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  10. (转载)delphi中获取汉字的拼音首字母

    delphi中获取汉字的拼音首字母1.py: array[216..247] of string = ({216}'CJWGNSPGCGNESYPB' + 'TYYZDXYKYGTDJNMJ' + ' ...