[最小生成树小结]hdu1875/hdu1879/poj2395/poj2421/poj2485/poj1258/poj2349

找到了

hdu1875

hdu1879

poj2395

poj2421

poj2485

poj1258

poj2349(toj1411)几个题,都是简单的最小生成树,可以用于练习达到熟练程度。一起干掉。

代码:

hdu1875

#include <cstdio>
#include <cstring>
#include <cfloat>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;

const double eps = 1e-8;
const double INF = DBL_MAX * 0.5;
const int MAX = 107;
double G[MAX][MAX];
bool vis[MAX];
double mincost[MAX];
struct Node {
	int x, y;
	double dis(const Node& B)const {
		return sqrt((double)(x - B.x) * (x - B.x) + (double)(y - B.y) * (y - B.y));
	}
} Point[MAX];

bool BFS(int u, int n) {
	memset(vis, false, sizeof(vis));
	vis[u] = true;
	queue<int> Q;
	int p = u;
	Q.push(p);
	while (!Q.empty()) {
		p = Q.front();
		Q.pop();
		
		for (int i = 0; i < n; ++i) {
			if (G[p][i] + eps < INF && !vis[i]) {
				vis[i] = true;
				Q.push(i);
			}
		}
	}
	
	for (int i = 0; i < n; ++i) {
		if (!vis[i]) return false;
	}
	return true;
}

double prim(int u, int n) {
	fill(mincost, mincost + MAX, INF);
	memset(vis, false, sizeof(vis));
	mincost[u] = 0.0;
	double sum = 0.0;
	while (true) {
		int v = -1;
		for (int u = 0; u < n; ++u) {
			if (!vis[u] && (v == -1 || mincost[u] < mincost[v])) {
				v = u;
			}
		}
		if (v == -1) break;
		vis[v] = true;
		sum += mincost[v];
		for (int i = 0; i < n; ++i) {
			if (mincost[i] > G[v][i]) mincost[i] = G[v][i];
		}
	}
	return sum * 100.0;
}

inline int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int T, n;
	T = read();
	while (T--) {
		n = read();
		//scanf(" %d", &C);
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				G[i][j] = INF;
			}
		}
		for (int i = 0; i < n; ++i) {
			Point[i].x = read();
			Point[i].y = read();
			//scanf(" %d %d", &Point[i].x, &Point[i].y);
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (j <= i) G[i][j] = G[j][i];
				else {
					G[i][j] = Point[i].dis(Point[j]);
					if (G[i][j] + eps < 10.0 || G[i][j] > 1000.0 + eps) {
						G[i][j] = INF;
					}
				}
			}
		}
		if (!BFS(0, n)) puts("oh!");
		else printf("%.1f\n", prim(0, n));
	}
	return 0;
}

hdu1879:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 1024;
struct edge {
	int from, to;
	int cost;
	edge(){}
	edge(int _from, int _to, int _cost):from(_from), to(_to), cost(_cost){}
	bool operator<(const edge& B)const {
		return cost < B.cost;
	}
};
vector<edge> G;
int father[MAX];

int find(int x) {
	return x == father[x] ? x : father[x] = find(father[x]);
}

void Union(int x, int y) {
	int fx = find(x);
	int fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
	}
}

int kruskal(int n) {
	for (int i = 0; i < MAX; ++i) {
		father[i] = i;
	}
	sort(G.begin(), G.end());
	
	int sum = 0;
	for (int i = 0; i < G.size(); ++i) {
		edge &e = G[i];
		if (find(e.from) == find(e.to)) {
			continue;
		}
		sum += e.cost;
		Union(e.from, e.to);
	}
	return sum;
}

inline int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}
int main() {
	int n;
	while (~scanf(" %d", &n) && n) {
		int u, v, c, d;
		G.clear();
		int es = n * (n - 1) >> 1;
		for (int i = 0; i < es; ++i) {
			u = read();
			v = read();
			c = read();
			d = read();
			//scanf(" %d %d %d %d", &u, &v, &c, &d);
			G.push_back(edge(u, v, (d ? 0 : c)));
		}
		printf("%d\n", kruskal(n));
	}
	
	return 0;
}

poj1258:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 105;
int G[MAX][MAX];
int mincost[MAX];
bool vis[MAX];

int prim(int node) {
	memset(vis, false, sizeof(vis));
	fill(mincost, mincost + node + 1, 0xffffff);
	int sum = 0;
	mincost[1] = 0;
	for (int i = 1; i <= node; ++i) {
		int v = 0;
		for (int u = 1; u <= node; ++u) {
			if (!vis[u] && (v == 0 || mincost[v] > mincost[u])) {
				v = u;
			}
		}
		if (!v) break;
		sum += mincost[v];
		vis[v] = true;
		for (int i = 1; i <= node; ++i) {
			if (mincost[i] > G[v][i]) mincost[i] = G[v][i];
		}
	}
	return sum;
}
int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int n;
	while (~scanf(" %d", &n)) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				G[i][j] = read(); //scanf(" %d", &G[i][j]);
			}
		}
		printf("%d\n", prim(n));
	}
	return 0;
}

poj2349:

/*
	初始化每个点一个连通分量 
	每次只需要选最小边即可,然后在剩下s个连通分量时答案就更新完毕鸟
*/ 
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const double INF = 1e10;
const int MAX = 512;
struct Point {
	double x, y;
	double dis(const Point& B) {
		return sqrt((x - B.x) * (x - B.x) + (y - B.y ) * (y - B.y));
	}
} a[MAX];
struct edge {
	int from, to;
	double cost;
};
struct cmp {
	bool operator()(edge& e1, edge& e2) {
		return e1.cost > e2.cost;
	}
};

int father[MAX];

int find(int x) {
	return x == father[x] ? x : father[x] = find(father[x]);
}
void Union(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
	}
}

double gao(int s, int p) {
	priority_queue<edge, vector<edge>, cmp> Q;
	for (int i = 0; i < p; ++i) {
		for (int j = i + 1; j < p; ++j) {
			Q.push((struct edge){i, j, a[i].dis(a[j])});
		}
		father[i] = i;
	}
	double ans = 0.0;
	for (int i = s; i < p; ++i) {
		edge e;
		e.from = e.to = e.cost = 0.0;
		while (!Q.empty()) {
			e = Q.top();
			Q.pop();
			if (find(e.from) != find(e.to)) break;
		}
		
		Union(e.from, e.to);
		if (ans < e.cost) ans = e.cost;
	}
	return ans;
}

int main() {
	int T;
	scanf(" %d", &T);
	while (T--) {
		int s, p;
		scanf(" %d %d", &s, &p);
		for (int i = 0; i < p; ++i) {
			scanf(" %lf %lf", &a[i].x, &a[i].y);
		}
		printf("%.2f\n", gao(s, p));
	}
	return 0;
}

poj2395:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 2048;
struct edge {
	int from, to;
	int cost;
	bool operator<(const edge& e)const {
		return cost < e.cost;
	}
};
vector<edge> G;
int father[MAX];

int find(int x) {
	return x == father[x] ? x : father[x] = find(father[x]);
}
bool Union(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
		return true;
	}
	return false;
}

int kruskal(int n) {
	for (int i = 1; i <= n; ++i) father[i] = i;
	sort(G.begin(), G.end());
	int ans = 0;
	for (vector<edge>::iterator e = G.begin(); e != G.end(); ++e) {
		if (Union(e->from, e->to) && ans < e->cost) {
			ans = e->cost;
		}
	}
	return ans;
}

inline int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int n, m;
	while (~scanf(" %d %d", &n, &m)) {
		G.clear();
		int u, v, c;
		while (m--) {
			u = read();
			v = read();
			c = read();
			G.push_back((struct edge){u, v, c});
		}
		printf("%d\n", kruskal(n));
	}
	return 0;
}	

poj2421:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 128;
struct edge {
	int from, to;
	int cost;
	bool operator<(const edge& e)const {
		return cost < e.cost;
	}
};
vector<edge> G;
int father[MAX];

int find(int x) {
	return x == father[x] ? x : father[x] = find(father[x]);
}
bool Union(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
		return true;
	}
	return false;
}

int kruskal(int n) {
	for (int i = 1; i <= n; ++i) father[i] = i;
	sort(G.begin(), G.end());
	int sum = 0;
	for (vector<edge>::iterator e = G.begin(); e != G.end(); ++e) {
		if (Union(e->from, e->to)) {
			sum += e->cost;
		}
	}
	return sum;
}

inline int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int n, c;
	while (~scanf(" %d", &n)) {
		G.clear();
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				c = read();
				if (j > i) {
					G.push_back((struct edge){i, j, c});
				}
			}
		}
		int u, v, m = read();
		while (m--) {
			u = read();
			v = read();
			G.push_back((struct edge){u, v, 0});
		}
		printf("%d\n", kruskal(n));
	}
	return 0;
}

poj2485:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 512;
struct edge {
	int from, to;
	int cost;
	bool operator<(const edge& e)const {
		return cost < e.cost;
	}
};
vector<edge> G;
int father[MAX];

int find(int x) {
	return x == father[x] ? x : father[x] = find(father[x]);
}
bool Union(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
		return true;
	}
	return false;
}

int kruskal(int n) {
	for (int i = 1; i <= n; ++i) father[i] = i;
	sort(G.begin(), G.end());
	int ans = 0;
	for (vector<edge>::iterator e = G.begin(); e != G.end(); ++e) {
		if (Union(e->from, e->to) && ans < e->cost) {
			ans = e->cost;
		}
	}
	return ans;
}

inline int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int T, n, c;
	T = read();
	while (T--) {
		n = read();
		G.clear();
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				c = read(); //scanf(" %d", &c);
				if (j > i) {
					G.push_back((struct edge){i, j, c});
				}
			}
		}
		printf("%d\n", kruskal(n));
	}
	return 0;
}	

小结:这类问题还是可以有一些变形和应用的,上述中,有些题可能需要有诸如增加权为0的边,将边删除等等小技巧。prim的复杂度略高一些,但是应对一般的小规模数据还是没有问题的,而且图论部分的问题规模都不会特别大。具体来说,prim和kruskal的实现部分还是智者见智,让自己习惯一些就好︿( ̄︶ ̄)︿

虽然暑假集训时学习过,但是那时对这块的东西好像还只是很模糊的,因为理解得并不是很深刻,所以代码实现起来也不见得行云流水。还望自己多多努力吧……之后再做一些诸如次小生成树之类的问题,加深理解︿( ̄︶ ̄)︿

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值