ural 1982. Electrification Plan -最小生成树
来源:互联网 发布:js 金币掉落效果 编辑:程序博客网 时间:2024/06/04 19:03
1982. Electrification Plan
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
Some country has n cities. The government has decided to electrify all these cities. At first, power stations ink different cities were built. The other cities should be connected with the power stations via power lines. For any citiesi, j it is possible to build a power line between them in cij 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.
Input
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 nextn lines contain an n × n table of integers {cij} (0 ≤cij ≤ 105). It is guaranteed thatcij = cji , cij > 0 fori ≠ j, cii = 0.
Output
Output the minimum cost to electrify all the cities.
Sample
4 21 40 2 4 32 0 5 24 5 0 13 2 1 0
3
Problem Author: Mikhail Rubinchik
Problem Source: Open Ural FU Championship 2013
Problem Source: Open Ural FU Championship 2013
Tags: graph theory
)题意:
n个城市,其中k个城市有发电站,问,怎么才能让每个城市都有电,并且花费最小
解题思路
每个有发电站的城市都能做出一个最小生成树来,然后好几个最小生成树。但是这样不好写
我们设置一个超级发电站,一开始所有发电站都与之相连,让超级发电站当这课树的根,
这样,既达到了将好几棵树变成一棵树的目的,又使得发电站与发电站之间边的权值不会被加进结果去
然后用 kruskl 算法做最小生成树,
这里注意,因为是从边权值最小开始,权值最小的边的节点的父亲可能都不是超级发电站,
所以后期遇到一个以超级发电站为父亲节点的,要让不是超级发电站为父亲节点的连进去。(有点乱,见代码)
代码
#include <iostream>#include <cstdio>#include <cstring>#include <functional>#include <algorithm>#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 110;int n,k;int fa[maxn];int suroot;int root[maxn];void init(){ for(int i = 0;i<maxn;i++){ ///让所有发电站都以超级发电站为父亲 ///这样就不会把发电站与发电站之间连线了,相当于多个生成树了 if(root[i]) fa[i] = suroot; else fa[i] = i; }}struct edge{ int u,v,cost;}e[maxn*maxn];bool comp(const edge& e1,const edge& e2){ return e1.cost < e2.cost;}int findp(int x){ int xp = x; while(fa[xp] != xp){ xp = fa[xp]; } int nowx = x,newx; while(fa[nowx] != xp){ newx = fa[nowx]; fa[nowx] = xp; nowx = newx; } return xp;}int main(){ scanf("%d%d",&n,&k); for(int i = 0;i<k;++i){ scanf("%d",&suroot);///指定一个超级发电站,只要是发电站就好,无所谓哪个 root[suroot] = 1; } init();///初始化父亲 int E = 0;///边的编号,一共E条边,下标到E-1 for(int i = 1;i<=n;++i){ for(int j=1;j<=n;++j){ int t; scanf("%d",&t); e[E].u = i; e[E].v = j; e[E].cost = t; E++; } } sort(e,e+E,comp); int res = 0; for(int i = 0; i<E; i++){ edge te = e[i]; int fu = findp(te.u),fv = findp(te.v); if(fu == fv) continue;///因为可能最小的边连得不是超级发电站,///所以当我们遇到一个父亲是超级发电站的时候,///再让超级发电站当 根 父亲 if(fu == suroot){ fa[fv] = fu; res += te.cost; } else if(fv == suroot){ fa[fu] = fv; res += te.cost; } else{ fa[fu] = fv; res += te.cost; } } printf("%d",res); return 0;}
1 0
- ural 1982. Electrification Plan 【最小生成树】
- ural 1982. Electrification Plan -最小生成树
- ural 1982. Electrification Plan (最小生成树)
- Ural 1982 Electrification Plan (prim最小生成树)
- timus1982. Electrification Plan---最小生成树
- ural 1982. Electrification Plan 并查集
- URAL 1982 Electrification Plan
- URAL 1982. Electrification Plan(并查集)
- Ural1982-Electrification Plan
- URAL - 1160 Network(最小生成树)
- URAL 1160. Network 最小生成树
- URAL 1982 最小生成树变形
- hdu3080 The plan of city rebuild(最小生成树)
- Timus Online Judge 1982 Electrification Plan
- HDU 3080 The plan of city rebuild(除点最小生成树)
- URAL 1416 Confidential --最小生成树与次小生成树
- Ural 1416 Confidential(最小生成树+次小生成树)
- [最小极差生成树 LCT || 二分答案 CDQ分治 并查集] Ural 2055 Urban Geography
- Linux中常用的命令都是哪些单词的缩写?
- 前端复习
- iOS 定位服务编程详解
- wxss样式不兼容css3的样式列表
- Linux挂载系统光盘,配置系统本地yum源
- ural 1982. Electrification Plan -最小生成树
- 移动端搭建Http Server(二)—— 实现监听并验证
- spring security 表达式
- 语言基础知识小结
- 网易云信实现自定义消息
- 文章标题
- 链式队列设计
- Linux移植
- 玲珑杯(树状数组)