ural 1982. Electrification Plan 【最小生成树】
来源:互联网 发布:linux解压缩zip 编辑:程序博客网 时间:2024/06/06 15:51
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 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 incij 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 × ntable of integers {cij} (0 ≤ cij ≤ 105). It is guaranteed that cij = cji , cij > 0 for i ≠ 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
/* 题意:给你n个城市和k个发电站的位置,以及每个城市之间拉电线的费用, 求使所有城市都能得到供电的最小花费 类型:最小生成树 分析:把有发电站的城市之间的费用全部改成0,再跑一次最小生成树即可*/#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<iostream>using namespace std;#define int_inf ((1<<31)-1)#define ll_inf ((1ll<<63)-1)int lowcost[100000+100];int vis[100000+100];int Map[300][300];bool dian[100000+100];int n;int prime(){ for(int i=1;i<=n;i++){ lowcost[i]=Map[1][i]; } int Min; int ans=0; lowcost[1]=0; vis[1]=1; for(int i=1;i<n;i++){ Min=int_inf; int k; for(int j=1;j<=n;j++){ if(!vis[j]&&Min>lowcost[j]){ Min=lowcost[j]; k=j; } } vis[k]=true; ans+=Min; for(int j=1;j<=n;j++){ if(!vis[j]&&lowcost[j]>Map[k][j]) lowcost[j]=Map[k][j]; } } return ans;}int main(){ int m; while(scanf("%d%d",&n,&m)!=EOF){ memset(dian,0,sizeof(dian)); memset(vis,0,sizeof(vis)); for(int i=1;i<=m;i++){ int a; scanf("%d",&a); dian[a]=1; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int c; scanf("%d",&c); Map[i][j]=c; if(dian[i]==1&&dian[j]==1)Map[i][j]=0; } } int result=prime(); printf("%d\n",result); }}
0 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
- JS中对集合与数组的操作
- 台州学院ACM:1748: Dungeon Master
- 5. Longest Palindromic Substring
- Android里WebView详解(二)
- 机器学习中用到的数学知识
- ural 1982. Electrification Plan 【最小生成树】
- TimePicker、TimePickerDialog的使用
- Java 学习笔记(0x08) super关键字
- linux 学习笔记-01
- CCF-201503-3-节日
- 最多约数问题
- 事件(3)——事件对象
- 2016年湖南省第十二届大学生计算机程序设计竞赛 解题报告
- XML简介与创建