Title Address: http://poj.org/problem?id=1679
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not unique!
Analysis: T group data, each group of n nodes M edge. Calculate, the minimum spanning tree is not unique, if it is the output of the minimum spanning tree weights and, otherwise the output is not unique! (not unique).
First calculate the minimum spanning tree, calculate the sub-niche into a tree, to determine whether the values are equal! The input data guarantees that no heavy edges exist.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <iostream> #include <string> #include <algorithm> #define N 110#define INF 0x3f3f3f3fusing namespace Std;int N, m;int map[n][n];bool used[n][n];int pre[n];bool vis[n];int dis[N];int Max[N][N];int Prim () {int ans=0; int I, J; Memset (Vis, false, sizeof (VIS)); Memset (used, false, sizeof (used)); memset (dis, 0, sizeof (DIS)); memset (max, 0, sizeof (max)); Pre[1]=-1; Vis[1]=true; for (i=2; i<=n; i++) {dis[i]=map[1][i]; Pre[i]=1; } for (int k=0; k<n-1; k++) {int mm=inf; int POS; for (I=1; i<=n; i++) {if (!vis[i]&&mm>dis[i]) {mm=dis[i]; Pos=i; }} ans+=mm; In this can add a judge if the mm==inf is found to indicate that there is no minimum spanning tree vis[pos]=true; Used[pos][pre[pos]]=true; The edge marker between Used[pre[pos]][pos]=true;//pos and Pre[pos] is used//update for (j=1; j<=n; J + +) {if (Vis[j]) Max[j][pos]=max[pos][j]=max (Max[j][pre[pos]], Dis[pos]); if (!vis[j]&&dis[j]>map[pos][j]) {dis[j]=map[pos][j]; Pre[j]=pos; }}} return ans;} int Mst;int sed_mst ()//calculate sub-niche into a tree {int sed=inf; int I, J; For (I=1, i<=n; i++) {for (j=i+1; j<=n; J + +) {if (Map[i][j]!=inf &&!used[i][j]) { Sed=min (SED, mst+map[i][j]-max[i][j]); }}} if (Sed==inf) return-1; return sed;} int main () {int TG; scanf ("%d", &TG); int I, J; while (tg--) {scanf ("%d%d", &n, &m); For (I=1, i<=n; i++) {for (j=1; j<=n; J + +) {if (i==j) map[i][j]=0; else Map[i][j]=inf; }}//the initialization of the map int u, V, W; for (i=0; i<m; i++) {scanf ("%d%d%d", &u, &v, &W); Map[u][v]=map[v][u]=w; } Mst=prim (); printf ("%d\n", MST); if (Mst==sed_mst ()) {printf ("not unique!\n"); }else{printf ("%d\n", MST); }} return 0;}
POJ 1679 the Unique MST "sub-niche into a tree +100 small amount of data"