uva 10034(最小生成树)
2013-11-24 23:11
369 查看
记得在哪里做过一遍,最简单的最小生成树,直接用kruskal秒掉了。
代码如下:
View Code
代码如下:
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <algorithm> #define LEN 110 using namespace std; typedef struct { double x, y; }POINT; POINT Point[LEN]; typedef struct { int a, b; double w; }ARC; ARC Arc[LEN*LEN]; int top, n; inline double dis(POINT a, POINT b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } //UFSet int parent[LEN]; void init(){ for(int i=0; i<LEN; i++)parent[i] = i; } int Find(int x){ return parent[x]==x?x:parent[x]=Find(parent[x]); } void Union(int a, int b){ int pa = Find(a), pb = Find(b); if(pa!=pb) parent[pa] = pb; } //kurskal bool cmp(ARC a, ARC b){ return a.w<b.w; } double kurskal() { sort(Arc, Arc+top, cmp); init(); double ret = 0; int cnt = 0; for(int i=0; i<top; i++){ if(cnt == n-1) break; if(Find(Arc[i].a)!=Find(Arc[i].b)){ cnt++; Union(Arc[i].a, Arc[i].b); ret+=Arc[i].w; } } return ret; } int main() { // freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while(T--){ scanf("%d", &n); for(int i=1; i<=n; i++){ scanf("%lf%lf", &Point[i].x, &Point[i].y); } top = 0; for(int i=1; i<=n; i++){ for(int j=i+1; j<=n; j++){ Arc[top].a = i; Arc[top].b = j; Arc[top].w = dis(Point[i],Point[j]); top ++; } } double ans = kurskal(); printf("%.2lf\n", ans); if(T) printf("\n"); } return 0; }
View Code
相关文章推荐
- uva 10034 Freckles 最小生成树
- UVA 10034 - Freckles(最小生成树)
- uva 10034 Freckles(最小生成树Kruskal)
- UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)
- uva10034 - Freckles(最小生成树)
- UVa 10034 - Freckles (最小生成树模板题)
- UVa 10034 Freckles (最小生成树+kruskal)
- Freckles - UVa 10034 最小生成树
- uva 10034 Freckles 最小生成树
- UVA10034 - Freckles(最小生成树)
- 【UVA 10034 Freckles】& Kruskal & 最小生成树
- uva 10034 Freckles(最小生成树Kruskal)
- UVA 10034 Freckles 【最小生成树】
- 最小生成树-Uva10034
- uva 10034(最小生成树)
- UVA 10034 Freckles 最小生成树
- uva10034Freckles - prime最小生成树
- UVa 1395 Slim Span【最小生成树】
- hdu 1102 uva 10397(最小生成树prim)
- uva 1395 - Slim Span(最小瓶颈生成树)