Uva - 10397 - Connect the Campus
2013-08-28 21:29
344 查看
题意:校园里有N个点,有些点之间已有M条电缆(0 <= M <= 1000)相连,问在现有的基础上把所有的点连通最少需要多长的电缆(1 <= N <= 750)。
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22163
——>>简单的小生成树。。。
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int maxn = 750 + 10;
int N, M, fa[maxn];
struct Point{
double x;
double y;
}p[maxn];
double Dis(Point A, Point B){
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
struct node{
int u;
int v;
double dis;
bool operator < (const node& e) const{
return dis > e.dis;
}
};
void init(){
for(int i = 1; i <= N; i++) fa[i] = i;
}
int Find(int x){
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
bool Union(int x, int y){
int newx = Find(x);
int newy = Find(y);
if(newx == newy) return 0;
fa[newy] = newx;
return 1;
}
void read(){
init();
for(int i = 1; i <= N; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
scanf("%d", &M);
int u, v;
for(int i = 0; i < M; i++){
scanf("%d%d", &u, &v);
Union(u, v);
}
}
void Kruscal(){
priority_queue<node> pq;
for(int i = 1; i <= N; i++)
for(int j = i+1; j <= N; j++)
pq.push((node){i, j, Dis(p[i], p[j])});
double ret = 0;
while(!pq.empty()){
node no = pq.top(); pq.pop();
if(Union(no.u, no.v)) ret += no.dis;
}
printf("%.2f\n", ret);
}
int main()
{
while(scanf("%d", &N) == 1){
read();
Kruscal();
}
return 0;
}
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22163
——>>简单的小生成树。。。
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int maxn = 750 + 10;
int N, M, fa[maxn];
struct Point{
double x;
double y;
}p[maxn];
double Dis(Point A, Point B){
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
struct node{
int u;
int v;
double dis;
bool operator < (const node& e) const{
return dis > e.dis;
}
};
void init(){
for(int i = 1; i <= N; i++) fa[i] = i;
}
int Find(int x){
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
bool Union(int x, int y){
int newx = Find(x);
int newy = Find(y);
if(newx == newy) return 0;
fa[newy] = newx;
return 1;
}
void read(){
init();
for(int i = 1; i <= N; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
scanf("%d", &M);
int u, v;
for(int i = 0; i < M; i++){
scanf("%d%d", &u, &v);
Union(u, v);
}
}
void Kruscal(){
priority_queue<node> pq;
for(int i = 1; i <= N; i++)
for(int j = i+1; j <= N; j++)
pq.push((node){i, j, Dis(p[i], p[j])});
double ret = 0;
while(!pq.empty()){
node no = pq.top(); pq.pop();
if(Union(no.u, no.v)) ret += no.dis;
}
printf("%.2f\n", ret);
}
int main()
{
while(scanf("%d", &N) == 1){
read();
Kruscal();
}
return 0;
}
相关文章推荐
- uva_10397 Connect the Campus
- uva 10397【Connect the Campus】
- UVa 10397 Connect the Campus (Kruskal+补全最小生成树)
- UVa 10397 - Connect the Campus
- uva 10397 Connect the Campus kruskal || prim
- Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)
- UVA 10397 Connect the Campus
- UVa 10397 Connect the Campus
- UVA 10397 Connect the Campus
- uva 10397 Connect the Campus(最小生成树)
- UVa 10397 - Connect the Campus
- UVA 10397 - Connect the Campus
- uva 10397 - Connect the Campus
- Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)
- UVA 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- UVA 10397 - Connect the Campus
- UVA 10397 - Connect the Campus
- Uva 10397 - Connect the Campus
- UVA 10397 - Connect the Campus 翻译