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 stations via power lines. For any cities ij 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 = cjicij >
0 for i ≠ jcii = 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

题意:

给出一些建有电站的城市,求每个城市都通电的最小花费;

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 10017;
int father[maxn];
int n;
int flag;
struct node
{
    int x, y;
    int c;
} cc[maxn];
int find(int x)
{
    return x==father[x] ? x:father[x]=find(father[x]);
}
void init()
{
    for(int i = 1; i <= n; i++)
    {
        father[i] = i;
    }
}
void Union(int x, int y)
{
    flag = 0;
    int f1 = find(x);
    int f2 = find(y);
    if(f1 != f2)
    {
        flag = 1;
        father[f2] = f1;
    }
}
bool cmp(node a, node b)
{
    return a.c < b.c;
}
int main()
{
    int k;
    while(~scanf("%d%d",&n,&k))
    {
        init();
        int m;
        for(int i = 1; i <= k; i++)
        {
            scanf("%d",&m);
            father[m] = -1;
        }
        int l = 0;
        int cost;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                scanf("%d",&cost);
                cc[l].x = i, cc[l].y = j;
                cc[l].c = cost;
                l++;
            }
        }
        int num = n*n;
        sort(cc,cc+num,cmp);
        int ans = 0;
        for(int i = 1; i <= num; i++)
        {
            Union(cc[i].x, cc[i].y);
            if(flag)
                ans+=cc[i].c;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-12-29 03:22:01

URAL 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

timus 1982 Electrification Plan(最小生成树)

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 were built. The other cities should be connected with t

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

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

URAL(timus)1709 Penguin-Avia(并查集)

Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other Antarctic airlines, experiences financial difficulties because of the world's economic crisis. People of Antarctica economize on flights and use trains

URAL 1671 Anansi&#39;s Cobweb (并查集)

题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<iostream> #define inf -100000000 #define LL long long #define maxn 100005 using namespace

Pilot Work Experience (URAL 1888 并查集+floyd)

Pilot Work Experience Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Leonid had n Oceanic Airlines flights during his business trip. He studied the latest issue of the monthly on-board magazine o

CSU 1601 War (并查集)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 202  Solved: 58 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar

Lightoj1009 Back to Underworld(带权并查集)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Back to Underworld Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description The Vampires and Lykans are fighting each other to death. The war has become so fierc