Test instructions
To a directed graph, ask him for the minimum spanning tree.
Analysis:
Directed graph of the fixed-point minimum spanning tree, also known as the minimum map, with the solution of the algorithm, the algorithm step-by detailed explanation:
- First determine if there is a minimum tree diagram, from the root node Dfs again.
- Root node, for all other vertex V, find a shortest edge with V as the end point.
- Determine whether the edge found in step 2 will form a ring, will be loop to step 4, will not go to step 5.
- Indents the ring to a point, updating the edge between the ring and other vertices.
- sum+ all edge weights after the indent point are the minimum tree-diagram answer.
Code:
POJ 3164//sep9#include <iostream> #include <cmath> #define INF 0x7fffffff using namespace Std;const int MAXN =128;int n,m;double X[MAXN],Y[MAXN];d ouble g[maxn][maxn];int vis[maxn];int Used[maxn],pass[maxn],eg[maxn],more, Queue[maxn];int map[maxn][maxn];void Combine (int id,double &sum) {int tot=0,from,i,j,k;for (; id!=0&&!pass [Id];id=eg[id]) {queue[tot++]=id;pass[id]=1;} for (From=0;from<tot&&queue[from]!=id;++from), if (From==tot) return, More=1;for (i=from;i<tot;++i) { Sum+=g[eg[queue[i]]][queue[i]];if (I!=from) {used[queue[i]]=1;for (j=1;j<=n;++j) if (!used[j]) {if (G[queue[i]][j] <G[ID][J]) g[id][j]=g[queue[i]][j];}} for (I=1;i<=n;++i) if (!used[i]&&i!=id) {for (j=from;j<tot;++j) {k=queue[j];if (g[i][id]>g[i][k]-g[ EG[K]][K]) g[i][id]=g[i][k]-g[eg[k]][k];}} Double mdst (int root) {int i,j,k;double sum=0;memset (used,0,sizeof (used)), for (More=1;more;) {More=0;memset (eg,0, sizeof (eg)); for (i=1;i<=n;++i) if (!used[i]&&i!=root) {for (J=1,K=0;J<=N;++J) if (!used[j]&&i!=j) {if (k==0| | G[j][i]<g[k][i]) K=j;} Eg[i]=k;} memset (pass,0,sizeof (pass)); for (i=1;i<=n;++i) if (!used[i]&&!pass[i]&&i!=root) combine (i,sum);} for (I=1;i<=n;++i) if (!used[i]&&i!=root) Sum+=g[eg[i]][i];return sum;} void dfs (int root) {vis[root]=1;for (int i=1;i<=n;++i) if (map[root][i]==1&&vis[i]==0) DFS (i);} int main () {while (scanf ("%d%d", &n,&m) ==2) {int i,j;for (i=1;i<=n;++i) scanf ("%lf%lf", &x[i],&y[i]) , for (I=1;i<=n;++i) for (j=1;j<=n;++j) G[i][j]=inf;memset (map,0,sizeof (map)), and while (m--) {scanf ("%d%d", &i, &J), if (i==j) continue;g[i][j]=sqrt ((X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j])); map[i][j]=1;} memset (vis,0,sizeof (VIS));d FS (1), for (I=1;i<=n;++i) if (vis[i]==0) break;if (i<=n) printf ("Poor snoopy\n"); elseprintf ("%.2lf\n", MDST (1));}}
POJ 3164 Command Network minimum tree diagram