The Unique MST POJ
来源:互联网 发布:淘宝退的运费险在哪里 编辑:程序博客网 时间:2024/05/22 01:35
这是一个次小生成树的基础题,这个题的想法,就是先写一个最小生成树,建立最小生成树的时候,把每个点的边还有结点用vector保存无向图,然后遍历每一个结点,找出这条边上的最大值,如果这个点没有遍历过那就更新最大值。
#include <iostream>#include <algorithm>#include <queue>#include <cstring>#include <cstdio>using namespace std;const int INF = 0x7fffffff;const int MAX = 10001;int n,m;int num;int p[MAX];int max1[101][101];int dist[105][105];vector<int> g[MAX];struct Edge{ int from; int to; int w; bool flag;} e[MAX];struct Node{ int seq; int max;};bool cmp(const Edge &a, const Edge &b){ return a.w < b.w;}void makeSet(){ for(int i = 0; i <= n; i++) { p[i] = i; }}int findSet(int x){ if(x != p[x]) p[x] = findSet(p[x]); return p[x];}void addEdge(int from, int to, int w){ g[from].push_back(to); g[to].push_back(from); dist[from][to] = w; dist[to][from] = w;}int kruscal(){ int i,j; int x, y; int edgeNum = 0; int result = 0; makeSet(); std::sort(e,e+m,cmp); for(i = 0; i < m; i++) { x = findSet(e[i].from); y = findSet(e[i].to); if(x != y) { edgeNum++; addEdge(e[i].from,e[i].to,e[i].w); addEdge(e[i].to,e[i].from,e[i].w); e[i].flag = true; p[x] = y; result += e[i].w; } } return edgeNum == n-1 ? result : -1;}void bfs(int p){ int i,j; bool used[101]; memset(used,0,sizeof(used)); std::queue<Node> que; Node now,adj; now.max = 0; now.seq = p; que.push(now); used[p] = true; while(!que.empty()) { Node q = que.front(); que.pop(); for(i = 0; i < g[q.seq].size(); i++) { adj.seq = g[q.seq][i]; adj.max = dist[q.seq][adj.seq]; if(!used[adj.seq]) { if(q.max > adj.max) adj.max = q.max; max1[p][adj.seq] = adj.max; used[adj.seq] = true; que.push(adj); } } }}void second_MST(){ int i,j; int mst = kruscal(); for(i = 1; i <= n; i++) bfs(i); int smst = INF; for(i = 0; i < m; i++) { if(!e[i].flag) { if(mst + e[i].w - max1[e[i].from][e[i].to] < smst) smst = mst + e[i].w - max1[e[i].from][e[i].to]; } } if(smst == mst) printf("Not Unique!\n"); else printf("%d\n",mst);}int main(){ int i,j; int cases; int a,b,w; scanf("%d",&cases); while(cases--) { memset(dist,0,sizeof(dist)); scanf("%d %d",&n,&m); for(i = 0; i < m; i++) { scanf("%d %d %d",&e[i].from,&e[i].to,&e[i].w); e[i].flag = false; } num = 0; second_MST(); } return 0;}
阅读全文
0 0
- poj The Unique MST
- The Unique MST POJ
- The Unique MST POJ
- The Unique MST POJ
- The Unique MST POJ
- The Unique MST POJ
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679 The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- Poj 1679 The Unique MST
- poj 1679 The Unique MST
- CentOS7 64位下MySQL5.7安装与配置(YUM)
- [Linux] 向网卡设备上添加 ip 别名
- idea Unable to open debugger port (127.0.0.1:58233): java.net.SocketException "socket closed"
- HDU-5935 Car 逆推 贪心 精度
- ES6(let 、const)
- The Unique MST POJ
- BZOJ 4033 [HAOI 2015] 树DP 解题报告
- 类似于百度的下拉菜单
- ospf 的cost 与Metric
- mysql Aborted connects值很大且增长很快,采用tcpdump+wireshark分析
- BZOJ 1821 [JSOI2010]Group部落划分
- Java语言中进行异常处理的关键字具体含义:throws、throw、try、catch、finally
- Android下载时Execution failed for task ':app:compileDebugNdk'.异常的处理
- SpringMVC处理Ajax01