标签:UI size ant cost lan isis output vertica can
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982
| 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;
}URAL 1982. Electrification Plan(并查集)
标签:UI size ant cost lan isis output vertica can
原文地址:http://www.cnblogs.com/jzdwajue/p/6955806.html