URAL 1982. Electrification Plan (and collection)

Source: Internet
Author: User

Title Link: http://acm.timus.ru/problem.aspx?space=1&num=1982



Some Country has NCities. The government have decided to electrify all these cities. At first, power stations in kDifferent cities were built. The other cities should is connected with the power stations via power lines. For any cities I, JIt's possible to build a power line between them in C IJRoubles. The country is in crisis after a civil war and so the government decided to build has only a few power lines. Of course from every city there must is a path along the lines to some city with a power station. Find the minimum possible cost-to-build all necessary power lines. Inputthe First line contains integers Nand k(1≤ kN≤100). The second line contains kDifferent integers that is the numbers of the cities with power stations. The next NLines contain an NX NTable of integers { C IJ} (0≤ C IJ≤105). It's Guaranteed that c ij = c ji , C IJ> 0 for IJ, C II= 0.OutputOutput 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



Test instructions

Give some cities built with power stations, and ask for the minimum cost of electricity for each city;

The code is as follows:

#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", &                Amp;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;}


URAL 1982. Electrification Plan (and collection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.