码迷,mamicode.com
首页 > 其他好文 > 详细

(次小生成树) poj 1679

时间:2015-01-16 16:18:13      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21358   Accepted: 7560

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‘. 

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

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF 1<<30
int n,m,a[110][110],low[110],vis[110],pre[110];
int maxx[110][110],connect[110][110],mst;
int prime()
{
      int minn,pos,fa,ans=0;
      vis[1]=1,pos=1;
      for(int i=1;i<=n;i++)
            if(i!=pos)
            {
                  low[i]=a[pos][i];
                  pre[i]=pos;
            }
      for(int i=1;i<n;i++)
      {
            minn=INF;
            for(int j=1;j<=n;j++)
            {
                  if(!vis[j]&&minn>low[j])
                  {
                        pos=j;
                        minn=low[j];
                  }
            }
            fa=pre[pos];
            ans+=minn;
            vis[pos]=1;
            connect[fa][pos]=connect[pos][fa]=1;
            maxx[fa][pos]=maxx[pos][fa]=minn;
            for(int j=1;j<=n;j++)
                  maxx[pos][j]=maxx[j][pos]=max(maxx[pos][fa],maxx[fa][j]);
            for(int j=1;j<=n;j++)
            {
                  if(!vis[j]&&low[j]>a[pos][j])
                  {
                        low[j]=a[pos][j];
                        pre[j]=pos;
                  }
            }
      }
      return ans;
}
int main()
{
      int tt;
      scanf("%d",&tt);
      while(tt--)
      {
            bool flag=false;
            memset(vis,0,sizeof(vis));
            memset(connect,0,sizeof(connect));
            memset(maxx,0,sizeof(maxx));
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++)
                  for(int j=1;j<=n;j++)
                        a[i][j]=INF;
            for(int i=1;i<=m;i++)
            {
                  int x,y,z;
                  scanf("%d%d%d",&x,&y,&z);
                  a[x][y]=z,a[y][x]=z;
            }
            mst=prime();
            for(int i=1;i<=n;i++)
                  for(int j=1;j<=n;j++)
                  {
                        if(connect[i][j]||a[i][j]==INF)
                              continue;
                        if(a[i][j]==maxx[i][j])
                        {
                              flag=true;
                              break;
                        }
                  }
            if(flag)
                  printf("Not Unique!\n");
            else
                  printf("%d\n",mst);
      }
      return 0;
}

  

(次小生成树) poj 1679

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4228918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!