Electrification Plan

Time limit: 0.5 second
Memory limit: 64 MB
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 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 next n lines
contain an n × n table of integers {cij} (0 ≤ cij ≤ 105).
It is guaranteed that cij = cji, cij > 0 for ij, cii = 0.

Output

Output the minimum cost to electrify all the cities.

Sample

input output
4 2
1 4
0 2 4 3
2 0 5 2
4 5 0 1
3 2 1 0
3
Problem Author: Mikhail Rubinchik
【分析】将任意两个带有发电站的城市之间的距离赋为0,然后prim求最小生成树。(这么简单的思路我居然没有想到,还是学长教我的)。
#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(最小生成树)的更多相关文章

  1. Timusoj 1982. Electrification Plan

    http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...

  2. URAL-1982 Electrification Plan 最小生成树

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

  3. Electrification Plan(最小生成树)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=50#problem/D 最小生成树模板,注意的是这里有k个发电站,它们不再需要连 ...

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

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

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

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

  6. URAL-1982-Electrification Plan最小生成树或并查集

    Electrification Plan 题意:在一个无向图中,给你几个源点,找出把所有点连接到源点后最小的消费: 可以利用并查集: 先用结构体把每个边存起来,再按照消费大小排序.之后从消费小的到大的 ...

  7. URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)

    Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construc ...

  8. Prim && Kruskal

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

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

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

随机推荐

  1. C/C++学习之基础-001

    1.C++虚函数的工作原理 虚函数(virtual function)需要虚函数表(virtual table)才能实现.如果一个类有函数声明成虚拟的,就会生成一个虚函数表,存放这个类的虚函数地址.若 ...

  2. php中的include()的使用技巧

    php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...

  3. 关于process

    http://docs.oracle.com/javase/1.5.0/docs/api/ The ProcessBuilder.start() and Runtime.exec methods cr ...

  4. vim的Tab设置为4个空格

    vim /etc/vimrc 1    set ts=42    set expandtab3    set autoindent 按tab键时产生的是4个空格,这种方式具有最好的兼容性.

  5. 使用httputils上传图片到服务器

    //创建httpUtils对象 HttpUtils mRegHttpUtils = new HttpUtils(); //图片路径 String path = "/sdcard/Downlo ...

  6. Storyboard的使用以及使用多个Storyboard的方法

    一, storyboard用起来很是方便.在开发中很有可能是多个人创建了多个storyboard.但最后,总是要把所有的storyboard“合并”在一起.总结一下自己的想法,不知道有没有其他的方式“ ...

  7. 20145210 《Java程序设计》第09周学习总结

    教材学习内容总结 第十六章 整合数据库 •JDBC(Java DataBase Connectivity) •JDBC是用于执行SQL的解决方案 •JDBC全名Java DataBase Connec ...

  8. Java可变参数/可变长参数

    Java可变参数/可变长参数 传递的参数不确定长度,是变长的参数,例如小例子: package demo; public class Demo { public static int sum(int ...

  9. 13、C#基础整理(枚举)

    枚举 1.概念和作用 (1)用于存放常量,只能在定义时赋值(防止编程过程中恶意篡改,并且防止对同一事物的不同赋值--统一化) (2)定义的枚举类型需要包含该类型的所有可能的值 (3)方法.类.内部都可 ...

  10. ZOJ 3811

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5343 网络赛这水题没写过太伤了,赛后写了下1A. 当时钻牛角尖一定要用k次bf ...