[kuangbin带你飞]之'最小生成树 '专题(未完成)
Posted pupil-xj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[kuangbin带你飞]之'最小生成树 '专题(未完成)相关的知识,希望对你有一定的参考价值。
带飞网址: https://vjudge.net/article/187
专题六:
POJ 1251 Jungle Roads
POJ 1287 Networking
POJ 2031 Building a Space Station
POJ 2421 Constructing Roads
ZOJ 1586 QS Network
POJ 1789 Truck History
POJ 2349 Arctic Network
POJ 1751 Highways
POJ 1258 Agri-Net
POJ 3026 Borg Maze
POJ 1679 The Unique MST
√ HDU 1233 还是畅通工程
√ HDU 1301 Jungle Roads
√ HDU 1875 畅通工程再续
// hdu 1301
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 6 int n; 7 int p[300]; 8 9 struct node { 10 char x, y; 11 int len; 12 } edge[100]; 13 14 int cmp(node a, node b) { return a.len < b.len; } 15 int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } 16 17 int main() { 18 while(scanf("%d", &n) == 1 && n) { 19 char s, e; 20 int m, v, k, ans; 21 k = 0; 22 ans = 0; 23 for(int i = 0; i != 300; ++i) { p[i] = i; } 24 25 for(int i = 0; i != n-1; ++i) { 26 getchar(); 27 scanf("%c%d", &s, &m); 28 for(int j = 0; j != m; ++j) { 29 getchar(); 30 scanf("%c%d", &e, &v); 31 edge[k].x = s; 32 edge[k].y = e; 33 edge[k].len = v; 34 k++; 35 } 36 } 37 sort(edge, edge+k, cmp); 38 for(int i = 0; i != k; ++i) { 39 int x = find(edge[i].x); 40 int y = find(edge[i].y); 41 if(x != y) { 42 ans += edge[i].len; 43 p[x] = y; 44 } 45 } 46 printf("%d ", ans); 47 } 48 return 0; 49 }
// hdu 1233
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 #define MAX 110 6 struct p{ 7 int start, end; 8 int weight; 9 }s[5000]; 10 11 int f[MAX]; 12 13 int cmp(const p & a, const p & b){ 14 return a.weight < b.weight; 15 } 16 17 int find(int x){ 18 int r = x, tmp; 19 if(x == f[x]){ 20 return x; 21 } 22 while(r != f[r]){ 23 r = f[r]; 24 } 25 while(x != r){ 26 tmp = f[x]; 27 f[x] = r; 28 x = tmp; 29 } 30 return r; 31 } 32 33 int main() 34 { 35 int n, m; 36 while(cin >> n){ 37 if(n == 0)break; 38 m = n*(n-1)/2; 39 for(int i = 0; i != m; ++i){ 40 scanf("%d%d%d", &s[i].start, &s[i].end, &s[i].weight); 41 } 42 for(int i = 1; i <= n; ++i){ 43 f[i] = i; 44 } 45 sort(s, s+m, cmp); 46 int sum = 0; 47 int k = 1; 48 int ans = 0; 49 while(k < n){ 50 int i = find(s[ans].start); 51 int j = find(s[ans].end); 52 if(i != j){ 53 f[j] = i; 54 sum += s[ans].weight; 55 k++; 56 } 57 ++ans; 58 } 59 printf("%d ", sum); 60 } 61 return 0; 62 }
// hdu 1875
1 #include<cstdio> 2 #include<cmath> 3 4 const double INF = 100005.0; 5 6 struct island { 7 int x, y; 8 } p[105]; 9 10 int n; 11 12 double getdis(int a, int b) { 13 int x = p[a].x - p[b].x; 14 int y = p[a].y - p[b].y; 15 return sqrt((double)(x*x + y*y)); 16 } 17 18 double dis[105]; 19 int vis[105]; 20 void prim() { 21 double sum = 0.0; 22 int cnt = 1; 23 for(int i = 0; i != n; ++i) { 24 dis[i] = getdis(0, i); 25 if(dis[i] < 10.0 || dis[i] > 1000.0) dis[i] = INF; 26 vis[i] = 0; 27 } 28 vis[0] = 1; 29 for(int i = 0; i != n-1; ++i) { 30 double minn = INF; 31 int t = -1; 32 for(int j = 0; j != n; ++j) { 33 if(!vis[j] && dis[j] < minn) { 34 minn = dis[j]; 35 t = j; 36 } 37 } 38 if(t == -1) break; 39 vis[t] = 1; 40 cnt++; 41 sum += minn; 42 for(int j = 0; j != n; ++j) { 43 if(!vis[j] && dis[j] > getdis(t, j) && getdis(t, j) >= 10.0 && getdis(t, j) <= 1000.0) { 44 dis[j] = getdis(t, j); 45 } 46 } 47 } 48 if(cnt != n) printf("oh! "); 49 else printf("%.1lf ", sum*100.0); 50 } 51 52 int main() { 53 int T; 54 scanf("%d", &T); 55 while(T--) { 56 scanf("%d", &n); 57 for(int i = 0; i != n; ++i) { 58 scanf("%d%d", &p[i].x, &p[i].y); 59 } 60 prim(); 61 } 62 return 0; 63 }
以上是关于[kuangbin带你飞]之'最小生成树 '专题(未完成)的主要内容,如果未能解决你的问题,请参考以下文章
[kuangbin带你飞]专题六 最小生成树 K - The Unique MST (判断最小生成树是否唯一)
[kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
[kuangbin带你飞]专题六 最小生成树 J - Borg Maze