POJ 3164 Command Network minimum tree diagram

Source: Internet
Author: User

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:

    1. First determine if there is a minimum tree diagram, from the root node Dfs again.
    2. Root node, for all other vertex V, find a shortest edge with V as the end point.
    3. Determine whether the edge found in step 2 will form a ring, will be loop to step 4, will not go to step 5.
    4. Indents the ring to a point, updating the edge between the ring and other vertices.
    5. 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] &LT;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&LT;=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, &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.