POJ1679 The Unique MST
来源:互联网 发布:免费顶级域名2016年 编辑:程序博客网 时间:2024/05/21 10:21
一.原题链接:http://poj.org/problem?id=1679
二.题目大意:给你一些边,让你判断最小生成树是不是唯一。
三.思路:
一个图最小生成树要不唯一的条件:
1.首先按常规求最小生成树,其他没有使用过的边一定有一些边和已经被使用的边相等。这是必要条件。
因此,我们只要枚举以下情况:
所有原来最小生成树的边(那些有其他未使用的边的权和它相等的),一一先删一次,再求最小生成树,求完后记得补回去。如果有相同最小生成树,那么就会出现和一开始按常规求的最小生成树权值相等。否则,输出最小权值。
四.代码:
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int MAX_SIZE = 10000, INF = 0x3f3f3f3f;struct Edge{ int u, v, weight; bool isDel, isUsed, isEqual;};Edge edges[MAX_SIZE];int nodeNum, edgeNum, pre[MAX_SIZE];//判断是否第一次求最小生成树bool first;void FUset(){ memset(pre, -1, sizeof(pre));}int Find(int x){ int root = x, save; while(pre[root] >= 0){ root = pre[root]; } while(x != root){ save = pre[x]; pre[x] = root; x = save; } return root;}void Union(int x, int y){ int xRoot = Find(x), yRoot = Find(y);//此时temp必为负数,负多少表示这棵树有几个节点 int temp = pre[xRoot] + pre[yRoot];//把节点少的树合并在节点多的树下面。 if(pre[xRoot] > pre[yRoot]){ pre[xRoot] = yRoot; pre[yRoot] = temp; } else{ pre[yRoot] = xRoot; pre[xRoot] = temp; }}bool cmp(Edge a, Edge b){ return a.weight < b.weight;}int Kruskal(){ int i, j, minWeight, u, v, usedNum = 1; FUset(); minWeight = 0; sort(edges, edges + edgeNum, cmp); for(i = 0; i < edgeNum; i++){ if(edges[i].isDel) continue; u = Find(edges[i].u); v = Find(edges[i].v); if(u != v){ Union(v, u); minWeight += edges[i].weight; usedNum++; if(first) edges[i].isUsed = true; } if(usedNum >= nodeNum) break; } return minWeight;}void enumEdges(){ int minWeight, minorWeight, i; first = true; minWeight = Kruskal(); first = false; for(i = 0; i < edgeNum; i++){ if(edges[i].isUsed && edges[i].isEqual){ edges[i].isDel = true; minorWeight = Kruskal(); if(minorWeight == minWeight){ cout<<"Not Unique!\n"; return; } edges[i].isDel = false; } } cout<<minWeight<<endl;}int main(){ //freopen("in.txt", "r", stdin); int i, j, test; cin>>test; while(test--){ cin>>nodeNum>>edgeNum; for(i = 0; i < edgeNum; i++){ edges[i].isEqual = edges[i].isUsed = edges[i].isDel = false; cin>>edges[i].u>>edges[i].v>>edges[i].weight; } for(i = 0; i < edgeNum; i++) for(j = 0; j < edgeNum; j++) if(i != j && edges[i].weight == edges[j].weight) edges[i].isEqual = edges[j].isEqual = true; enumEdges(); }}
0 0
- POJ1679:The Unique MST
- POJ1679 The Unique MST
- poj1679 - The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- poj1679 The Unique MST
- POJ1679------The Unique MST
- POJ1679-The Unique MST
- poj1679 The Unique MST
- POJ1679 The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- POJ1679-The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- poj1679(The Unique MST)Kruskal
- POJ1679 The Unique MST(kru)
- poj1679——The Unique MST
- Scala集合类型详解
- NYOJ139我排第几个
- http://blog.csdn.net/barebear/article/details/7704852
- hdu 2830(矩形dp)
- JS中三种主要方法(函数定义)类别理解 —(JS面向对象&原型)
- POJ1679 The Unique MST
- 小米笔试题 懂二进制
- leetcode: Majority Element
- yii小物件详解
- 出栈顺序(栈和队列)B
- iOS中本地图片库封装、相机调用封装库
- 最牛最暴力的开源协议:WTFPL
- 京东笔试题 小东复习,规划时间
- 开发没有直径,但也要避免走弯路