ural 1982. Electrification Plan -最小生成树

1982. 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 ≤ kn ≤ 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 ij, c ii = 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
Problem Source: Open Ural FU Championship 2013
Tags: graph theory   ( hide tags for unsolved problems )


题意:
n个城市,其中k个城市有发电站,问,怎么才能让每个城市都有电,并且花费最小


解题思路
每个有发电站的城市都能做出一个最小生成树来,然后好几个最小生成树。但是这样不好写
我们设置一个超级发电站,一开始所有发电站都与之相连,让超级发电站当这课树的根,
这样,既达到了将好几棵树变成一棵树的目的,又使得发电站与发电站之间边的权值不会被加进结果去
然后用 kruskl 算法做最小生成树,
这里注意,因为是从边权值最小开始,权值最小的边的节点的父亲可能都不是超级发电站,
所以后期遇到一个以超级发电站为父亲节点的,要让不是超级发电站为父亲节点的连进去。(有点乱,见代码)


代码
#include 
#include 
#include 
#include 
#include 
#include 
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




你可能感兴趣的:(最小生成树)