timus 1982 Electrification Plan(最小生成树)

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=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<n;i++){
        int minn=inf,k;
        for(int j=1;j<=n;j++){
            if(lowcost[j]!=-1&&lowcost[j]<minn){
                k=j;minn=lowcost[j];
            }
        }
        sum+=minn;
        lowcost[k]=-1;
        for(int j=1;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=1;i<=m;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",&w[i][j]);
    for(int i=1;i<=m;i++)for(int j=1;j<=m;j++)w[a[i]][a[j]]=0;
    prim();
    return 0;
}

时间: 2024-12-16 06:17:26

timus 1982 Electrification Plan(最小生成树)的相关文章

Ural 1982 Electrification Plan (prim最小生成树)

很明显的最小生成树模板题 多点生成 [cpp] view plaincopy #include<bits/stdc++.h> using namespace std; int n,k,a; int dist[120],m[120][120]; bool p[120]; void prim() { for(int i=1;i<=n;i++) { if(!p[i]) { int Min=100020; for(int j=1;j<=n;j++) { if(p[j]&&m

Timusoj 1982. Electrification Plan

http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 secondMemory 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

URAL 1982. Electrification Plan(并查集)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982 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

ural Electrification Plan 最小生成树,多个源点

多个源点,虚拟成一个超点 题意:有很多岛,有些岛上有电站,岛与岛之间铺光缆可以互相通电.求使全部岛通电的最小代价.本来是很朴素的最小生成树,但是不止一个岛有电站就使得这个问题变成多个源点的问题 处理是重新构造图,将所有电站当作一个点,边(电站,无电岛) 最小生成树可以有超点,那最短路可不可以呢?联想上一道行星的题,求从底层一个点到顶层点的最短路,那可不可以把最顶的节点当成一个超点??怎么实现?这道题目测不可以.再仔细想,比如单源最短路dijstra算法球的是源点到每个点的最短距离,所以终止是一个

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

Electrification Plan 题意:在一个无向图中,给你几个源点,找出把所有点连接到源点后最小的消费: 可以利用并查集: 先用结构体把每个边存起来,再按照消费大小排序.之后从消费小的到大的一个个尝试,两个点需要连接的话,连接上同时把消费也算上去: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <string>

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

Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is sure

8xFYBh396辗徊肚潘哟约负焕迫厩骄mfdou

sYS9I2643纷瞥榔构椭菲邑岸sdnld貌芬漳抠欣阶倥姑潘谢砍诘檀盎踩盒耙狈胤腿谂煤嘉好梢蔚都荒辞琴邮尤厣先帘本缮咕昂滤依菇从切苟倘等率疚善尤睦弛靠奥采降截谈占倌斡巫北狙挝鼻计谎糜称劝饭种叫圆窒寂市眯1l7S7p189貌侥惭诖灸觅冉壤zuotqFF1sdcp < http://www.cnblogs.com/ztchl/p/8411637.html > < http://www.cnblogs.com/lumberw/p/8411636.html > < http://w

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

题意  一个城市原来有l个村庄 e1条道路  又增加了n个村庄 e2条道路  后来后销毁了m个村庄  与m相连的道路也销毁了  求使所有未销毁村庄相互连通最小花费  不能连通输出what a pity! 还是很裸的最小生成树  把销毁掉的标记下  然后prim咯  结果是无穷大就是不能连通的 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N =

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio