博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
timus 1982 Electrification Plan(最小生成树)
阅读量:7165 次
发布时间:2019-06-29

本文共 1879 字,大约阅读时间需要 6 分钟。

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
c
ij 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 {
c
ij} (0 ≤
c
ij ≤ 10
5). It is guaranteed that
c
ij =
c
ji,
c
ij > 0 for
i
j,
c
ii = 0.

Output

Output the minimum cost to electrify all the cities.

Sample

input output
4 21 40 2 4 32 0 5 24 5 0 13 2 1 0
3
Problem Author: Mikhail Rubinchik
【分析】将任意两个带有发电站的城市之间的距离赋为0,然后prim求最小生成树。(这么简单的思路我居然没有想到,还是学长教我的)。
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 10000000#define mod 10000typedef long long ll;using namespace std;const int N=6005;const int M=50000;int power(int a,int b,int c){ int ans=1;while(b){ if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;}int a[N],w[N][N],vis[N],lowcost[N];int n,m,k;void prim(){ int sum=0;lowcost[1]=-1; for(int i=2;i<=n;i++){ lowcost[i]=w[1][i]; } for(int i=1;i
View Code

 

转载于:https://www.cnblogs.com/jianrenfang/p/5850950.html

你可能感兴趣的文章