首页 > 代码库 > 次小生成树 [POJ 1679] The Unique MST
次小生成树 [POJ 1679] The Unique MST
The Unique MST
Description
Given a connected undirected graph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V‘, E‘), with the following properties:
1. V‘ = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E‘) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E‘.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V‘, E‘), with the following properties:
1. V‘ = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E‘) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E‘.
Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!‘.
Sample Input
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not Unique!
Source
POJ Monthly--2004.06.27 srbga@POJ
模板、、、
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define INF 0x3f3f3f3f#define N 110int n,m;int closest[N];int lowcost[N];int mpt[N][N];int mxd[N][N];bool vis[N];bool connect[N][N];int ans1,ans2;void prim(int s){ int fk,k,i,j,minc; for(i=1;i<=n;i++) { closest[i]=s; lowcost[i]=mpt[s][i]; } vis[s]=1; for(i=1;i<n;i++) { minc=INF; for(j=1;j<=n;j++) { if(!vis[j] && lowcost[j]<minc) { k=j; minc=lowcost[j]; } } if(minc==INF) break; vis[k]=1; ans1+=minc; fk=closest[k]; connect[fk][k]=1; connect[k][fk]=1; for(j=1;j<=n;j++) { if(vis[j]) mxd[k][j]=mxd[j][k]=max(mxd[j][fk],lowcost[k]); if(!vis[j] && mpt[k][j]<lowcost[j]) { lowcost[j]=mpt[k][j]; closest[j]=k; } } }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); ans1=0; ans2=INF; memset(mxd,0,sizeof(mxd)); memset(mpt,INF,sizeof(mpt)); memset(vis,0,sizeof(vis)); memset(connect,0,sizeof(connect)); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); mpt[a][b]=c; mpt[b][a]=c; } prim(1); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(!connect[i][j]) { ans2=min(ans2,ans1-mxd[i][j]+mpt[i][j]); } } } if(ans1!=ans2) printf("%d\n",ans1); else printf("Not Unique!\n"); } return 0;}
次小生成树 [POJ 1679] The Unique MST
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。