首页 > 代码库 > 最小生成树模板(poj3625)
最小生成树模板(poj3625)
Building Roads
Description Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms. Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms. Input * Line 1: Two space-separated integers: N and M Output * Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers. Sample Input 4 1 1 1 3 1 2 3 4 3 1 4 Sample Output 4.00 Source USACO 2007 December Silver |
prim算法:
Memory: 8072K | Time: 188MS | |
Language: C++ | Result: Accepted |
#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #define INF 0x3f3f3f3f using namespace std; const int N = 1001; double graph[N][N]; bool visit[N]; int n,M; typedef struct { double x; double y; }dian; dian m[N]; double prim() { memset(visit,0,sizeof(visit)); double low[1001]; int pos = 1; visit[1] = 1; double result = 0; for(int i = 2; i <= n; i++) { low[i] = graph[pos][i]; } for(int i = 0; i < n-1; i++) { double Min = INF; for(int j = 1; j <= n; j++) { if(!visit[j] && Min > low[j]) { Min = low[j]; pos = j; } } visit[pos] = 1; result += Min; for(int i = 1; i <= n; i++) { if(!visit[i] && low[i] > graph[pos][i]) { low[i] = graph[pos][i]; } } } return result; } double dis(dian a,dian b) { return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); } int main() { // freopen("in.txt","r",stdin); while(cin>>n>>M) { memset(graph,INF,sizeof(graph)); for(int i = 1; i <=n;i++) { cin >> m[i].x>>m[i].y; } for(int i = 1; i <= n; i++) { for(int j = i + 1; j <= n; j++) { graph[i][j] = graph[j][i] = dis(m[i],m[j]); } } for(int i = 0; i < M ; i++) { int a,b; cin>>a>>b; graph[a][b] = graph[b][a] = 0; } printf("%.2lf\n",prim()); } return 0; }
Memory: 8604K | Time: 735MS | |
Language: G++ | Result: Accepted |
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; const int N = 1001; const int E = 1000000; int n, M; int cent; int a[N]; int Count = 0; typedef struct { int x; int y; double vaule; }dian; dian m[E]; typedef struct { double x, y; }situation; situation p[N]; double dis(situation a, situation b) { return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } bool cmp(dian a, dian b) { return a.vaule < b.vaule; } void init() { // cent 这里应该初始化到n for (int i = 1; i <= n; i++) { a[i] = i; } } int Find(int x) { while (x != a[x]) { x = a[x]; } return x; } void Union(int x, int y) { // 建议做路径压缩 int fx = Find(x); int fy = Find(y); if (fx != fy) { a[fx] = fy; } } double Kruskal() { // init(); 不应该在这里init sort(m, m + cent, cmp); double result = 0; for (int i = 0; i < cent&&Count != n - 1; i++) { if (Find(m[i].x) != Find(m[i].y)) { Union(m[i].x, m[i].y); result += m[i].vaule; Count++; } } return result; } int main() { while (cin >> n >> M) { for (int i = 1; i <= n; i++) { cin >> p[i].x >> p[i].y; } cent = 0; Count = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { m[cent].x = i; m[cent].y = j; m[cent++].vaule = dis(p[i], p[j]); } } // init不应该放在Kruskal里面 init(); for (int i = 1; i <= M; i++) { int a, b; cin >> a >> b; // 这里还是要检查Find a 和 Find b是不是一样,不然Count会错 if (Find(a) != Find(b)) { Union(a, b); Count++; } } printf("%.2f\n", Kruskal()); } return 0; }
最小生成树模板(poj3625)