poj2395 Out of Hay , 求MST的最长边
来源:互联网 发布:淘宝金币能换现金 编辑:程序博客网 时间:2024/05/22 04:44
点击打开链接
求MST的最长边~
prim
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define Min(a,b) (a)<(b)?(a):(b)using namespace std;const int INF = 1000000000;const int maxn = 2000 + 5;struct pto{ int v, len;};vector<pto> G[maxn];bool vis[maxn];int dis[maxn];int n, m;int Prim(){ int i, j, p, ans, minc; memset(vis, 0, sizeof vis ); for(i=1; i<=n; ++i) dis[i] = INF; dis[1] = 0; ans = 0; int maxx = -100; for(i=1; i<=n; ++i) { minc = INF, p = -1; for(j=1; j<=n; ++j) if(!vis[j]) if(minc>dis[j]) minc = dis[p=j]; if(-1 == p) break; vis[p] = 1; if(maxx <minc) maxx = minc; for(j=0; j<G[p].size(); ++j) if(!vis[G[p][j].v]) { int& u = G[p][j].v; dis[u] = Min(dis[u], G[p][j].len); } } return maxx;}int main(){ int i, x, y, z; pto e; while(~scanf("%d%d",&n,&m)) { for(i=0; i<=n; ++i) G[i].clear(); for(i=0; i<m; ++i) { scanf("%d%d%d",&x,&y,&z); e.v = y, e.len = z; G[x].push_back(e); e.v = x; G[y].push_back(e); } printf("%d\n",Prim()); } return 0;}
kruskal
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int INF = 1000000000;const int maxn = 2000 + 5;const int maxm = 10000 + 5;struct Edge { int x, y, w; Edge(int x=0, int y=0, int w=0):x(x),y(y),w(w) {} bool operator < (const Edge& rhs) const { return w < rhs.w; }} e[maxm];int fa[maxn];int n, m;int getfather(int x){ while(x != fa[x]) x = fa[x]; return fa[x];}int kruskal(){ int i, t1, t2, ans = 0, num = 0; int maxx = -100; sort(e, e+m); for(i=1; i<=n; ++i) fa[i] = i; for(i=0; i<m; ++i) { t1 = getfather(e[i].x); t2 = getfather(e[i].y); if(t1 != t2) { fa[t1] = t2; ans += e[i].w; if(maxx<e[i].w) maxx = e[i].w; if(++num==n-1) break; } } return maxx;}int main(){ int i, x, y, z; while(~scanf("%d%d",&n,&m)) { for(i=0; i<m; ++i) { scanf("%d%d%d",&e[i].x, &e[i].y, &e[i].w); } printf("%d\n", kruskal()); } return 0;}
0 0
- poj2395 Out of Hay , 求MST的最长边
- POJ 2395 Out of Hay【求MST中最长边】
- POJ2395--Out of Hay
- POJ2395---Out of Hay
- POJ2395--Out of Hay
- POJ2395-Out of Hay
- POJ2395 Out of Hay
- POJ2395 Out of Hay
- POJ2395 Out of Hay 【Dijkstra】
- POJ2395 Out of Hay【Kruskal】
- POJ2395 Out of Hay 最小生成树
- POJ2395 Out of Hay 最小生成树
- POJ2395 Out of Hay(最小生成树)
- poj2395 Out of Hay (prim算法)
- poj2395 Out of Hay(最小生成树)
- poj 2395 Out of Hay、2485 Highways(求最小生成树的最长边)
- poj2395--Out of Hay(最小生成树)
- Out of Hay(poj2395)(并查集)
- 线程调度实验报告_Nachos Lab2
- 原码、反码与补码
- Cocos2d-x学习笔记(14)(更新函数scheduleUpdate、进度计时器CCProgressTo、滚动视图CCScrollView)
- Eclipse配色方案插件
- Oracle中的SCN与备份,恢复的关系。
- poj2395 Out of Hay , 求MST的最长边
- 黑马程序员_递归
- MyEclipse10.5的破解
- 最近在学软件体系结构看了下图之后有点不懂大家看看我的理解是
- 调用业务逻辑层数据访问层等等一系列函数获取到所需的数据
- OCP 1Z0 052 14
- Java RandomAccessFile用法 (转的,写得非常很好!!)
- @SuppressWarnings注解 java
- 日本北海道海域一艘汽艇翻船 三人死亡一人失踪