kruskal_poj 1251Jungle Roads
来源:互联网 发布:兰州宏点网络 编辑:程序博客网 时间:2024/05/20 07:59
poj1251
http://poj.org/problem?id=1251
此题一看就知是最小生成树,由于数据量很小,输入的数据形式也没有那一种比较方便,所以prim和kruskal都可以
下面给出kruskal代码
#include<cstdio>#include<vector>#include<cstring>#include<algorithm>#include<iostream>#define MAXEN 200#define MAXVN 60using namespace std;typedef struct{ int u, v, value;}Edge;Edge eg[MAXEN];int p[MAXVN], rank[MAXVN];vector<Edge> usedEdge;void MAKE_SET(int x){ p[x] = x; rank[x] = 0;}int FIND_SET(int x){ if(x != p[x]) p[x] = FIND_SET(p[x]); return p[x];}void LINK(int x, int y){ if(rank[x] > rank[y]) p[y] = x; else{ p[x] = y; if(rank[x] == rank[y]) rank[y] += 1; }}void UNION_SET(int x, int y){ LINK(FIND_SET(x), FIND_SET(y));}int cmp(const void *a, const void *b){ return (*(Edge *)a).value - (*(Edge *)b).value;}bool cp(Edge a,Edge b){ return a.value < b.value;}int kruskal(int en, int vn){ usedEdge.clear();//printf("sfdds"); for(int i = 1; i <= vn; i++) MAKE_SET(i); qsort(eg,en,sizeof(eg[0]), cmp); for(int i = 0; i < en; i++){ if(FIND_SET(eg[i].u) != FIND_SET(eg[i].v)){ UNION_SET(eg[i].u,eg[i].v); usedEdge.push_back(eg[i]); } } int sum = 0; for(int i = 0; i < usedEdge.size(); i++) sum+=usedEdge[i].value; return sum;}int main(){ int vn,n, b; char a; //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(cin>>vn && vn){ //getchar(); int count = 0; for(int i = 1; i < vn; i++){ cin>>a>>n; //printf("n: %d\n", n); //getchar(); for(int j = 0; j < n ; j++){ cin>>a>>b; getchar(); eg[count].u = i; eg[count].v = a - 'A' +1; eg[count++].value = b; } } int sum = kruskal(count, vn); cout<<sum<<endl; }}
- kruskal_poj 1251Jungle Roads
- 1251 Jungle Roads
- POJ 1251 Jungle Roads
- pku 1251 Jungle Roads
- poj 1251 Jungle Roads
- poj 1251 Jungle Roads
- POJ-1251 jungle roads
- poj 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- Poj 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ:1251 Jungle Roads
- POJ 1251 Jungle Roads
- zoj 1090 The Circumference of the Circle(海伦公式+正弦定理)
- SQL SERVER 2012 文件表(FILETABLE)新体验之一
- hdu 1175 其实真的可以不用搜索!!
- FileNet PE操作入口的几个基本点
- 进程的虚拟地址空间
- kruskal_poj 1251Jungle Roads
- SQL SERVER 2012 文件表(FILETABLE)新体验之二
- 一之日
- 快速幂取模
- javascript画图
- 如果你想知道cin,cout究竟和scanf,printf速度上有什么差别~~
- ACM入门之杭电1045:Fire Net C++解法
- Android横竖屏总结
- Android数据存储