timus 1982 Electrification Plan(最小生成树)
Electrification Plan
Memory limit: 64 MB
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 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
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 {cij} (0 ≤ cij ≤ 105).
It is guaranteed that cij = cji, cij > 0 for i ≠ j, cii = 0.
Output
Sample
| input | output |
|---|---|
4 2 |
3 |
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 10000000
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int a[N],w[N][N],vis[N],lowcost[N];
int n,m,k;
void prim()
{
int sum=;lowcost[]=-;
for(int i=;i<=n;i++){
lowcost[i]=w[][i];
}
for(int i=;i<n;i++){
int minn=inf,k;
for(int j=;j<=n;j++){
if(lowcost[j]!=-&&lowcost[j]<minn){
k=j;minn=lowcost[j];
}
}
sum+=minn;
lowcost[k]=-;
for(int j=;j<=n;j++){
lowcost[j]=min(lowcost[j],w[k][j]);
}
}
printf("%d\n",sum);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)for(int j=;j<=n;j++)scanf("%d",&w[i][j]);
for(int i=;i<=m;i++)for(int j=;j<=m;j++)w[a[i]][a[j]]=;
prim();
return ;
}
timus 1982 Electrification Plan(最小生成树)的更多相关文章
- Timusoj 1982. Electrification Plan
http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...
- URAL-1982 Electrification Plan 最小生成树
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982 题意:无向图,给n个点,n^2条边,每条边有个一权值,其中有k个点有发电站,给出这 ...
- Electrification Plan(最小生成树)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=50#problem/D 最小生成树模板,注意的是这里有k个发电站,它们不再需要连 ...
- Electrification Plan 最小生成树(prim+krusl+堆优化prim)
题目 题意: 无向图,给n个城市,n*n条边,每条边都有一个权值 代表修路的代价,其中有k个点有发电站,给出这k个点的编号,要每一个城市都连到发电站,问最小的修路代价. 思路: prim:把发电站之间 ...
- zufeoj Electrification Plan (最小生成树,巧妙设e[i][j]=0)
Electrification Plan 时间限制: 1 Sec 内存限制: 128 MB提交: 31 解决: 13[提交][状态][讨论版] 题目描述 Some country has n ci ...
- URAL-1982-Electrification Plan最小生成树或并查集
Electrification Plan 题意:在一个无向图中,给你几个源点,找出把所有点连接到源点后最小的消费: 可以利用并查集: 先用结构体把每个边存起来,再按照消费大小排序.之后从消费小的到大的 ...
- URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)
Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construc ...
- Prim && Kruskal
Electrification Plan Prim #include<iostream> #include<cstring> using namespace std; cons ...
- HDU 3080 The plan of city rebuild(除点最小生成树)
题意 一个城市原来有l个村庄 e1条道路 又添加了n个村庄 e2条道路 后来后销毁了m个村庄 与m相连的道路也销毁了 求使全部未销毁村庄相互连通最小花费 不能连通输出what a pity ...
随机推荐
- ctime、atime
Linux系统文件有三个主要的时间属性,分别是ctime(change time, 而不是create time), atime(access time), mtime(modify time).后来 ...
- 推荐可以代替Visio的HTML开发的作图工具:ProcessOn
过去作图的时候一直都是在用visio,每一次换了电脑使用都要重新安装,这大家都知道,最头疼的就是激活问题,曾经因为激活问题我“找遍了”正个互联网,最后还没找到...从08年开始到现在,visio用了这 ...
- T420修改wifi灯闪动模式
给T420新装了centos7发现默认的配置wifi灯是工作时闪动的,有点晃眼,想改成简单的on 的时候常亮,off的时候常暗的模式 添加配置文件: vi /etc/modprobe.d/wlanle ...
- iOS开发之runtime运行时机制
最近参加三次面试都有被问到runtime,因为不太懂runtime我就只能支支吾吾的说点零碎.我真的好几次努力想看一看runtime的知识,因为知道理解它对理解OC代码内部变化有一定帮助,不过真心觉得 ...
- 从github上面拷贝源码
http://www.cnblogs.com/xing901022/p/4287064.html
- MySQL数据类型(四)
一.数据类型 二.整型类型 tinyInt: 1个字节:-128-127(有符号) 是否有符号,可以定义时,使用unsign标识,表示无符号的,不写表示有符号的 Create table studen ...
- C#线程状态简析
在C# 语言世界中,当我们创建一个新的子线程,该线程状态为unstarted, 子线程开始之后,子线程状态为Running,IsAlive 为true: 线程响应 Thread.Start 并开始运行 ...
- PAT 06-3 单词长度
此题为简化版,题设为“以'.'结尾”.“单词与语言无关”.三种情况下的输入输出为 . -> 无输出 a b. ->1 1(无空格) a b . ->1 1(无空格) 程 ...
- struts2+hibernate整合开发步骤
百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...
- (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制
前面讲过,spring的生命周期为:实例化前奏-->实例化-->实例化后期-->初始化前期-->初始化-->初始化后期-->bean的具体调用-->销毁前-- ...