#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
#define maxn 2600
#define INF 0xffffff
int father[maxn];
struct Map
{
int x, y;
}map[maxn];
struct node
{
int a, b, len;
friend bool operator < (node x, node y)
{
return x.len > y.len;
}
};
int find(int x);
int Union(int x, int y);
int Distance(Map a, Map b);
void kruskal(int n);
int main()
{
int m, n;
while(cin >> m >> n)
{
int i, j, k=0;
char ch;
for(i=1; i<=m; i++)
for(j=1; j<=n; j++)
{
cin >> ch;
if(ch == '#')
{
map[++k].x = i;
map[k].y = j;
}
}
for(i=1; i<=k; i++)
father[i] = i;
kruskal(k);
}
return 0;
}
int find(int x)
{
if(father[x] != x)
return father[x] = find(father[x]);
return x;
}
int Union(int x, int y)
{
x = find(x), y = find(y);
if(x != y)
{
father[x] = y;
return 1;
}
return 0;
}
int Distance(Map a, Map b)
{
int dx = abs(a.x - b.x), dy = abs(a.y - b.y);
if(dx <= 1 && dy <= 1)return 0;
if(dx <= 1)return dy-1;
if(dy <= 1)return dx-1;
return INF;
}
void kruskal(int n)
{
priority_queue<node> que;
int i, j, k, sum=0, ans=0;
node q;
for(i=1; i<n; i++)
for(j=i+1; j<=n; j++)
{
q.len = Distance(map[i], map[j]);
if(q.len == 0)
k = Union(i, j);
else if(q.len != INF)
{
q.a = i, q.b = j;
que.push(q);
}
}
for(i=1; i<=n; i++)
if(father[i] == i)
ans++;
k = 0;
while(que.size())
{
q = que.top(), que.pop();
if(Union(q.a, q.b))
{
sum += q.len;
k++;
}
}
cout << ans <<endl;
cout << k << " " << sum <<endl;
}
Wikioi 1002
最新推荐文章于 2014-10-20 22:20:57 发布