POJ 1679 The Unique MST【MST是否唯一,Prime算法,最好的代码】

The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27389   Accepted: 9816

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

POJ Monthly--2004.06.27 [email protected]

原题链接:http://poj.org/problem?id=1679

题意:看最小生成树是否唯一,网上看了好多的代码,都用到了枚举,但是发现了一份不用枚举的代码,

原文链接:http://blog.csdn.net/cambridgeacm/article/details/7857252,但是有一点不明白,就是主循环为什么是n次,而不是n-1次。有大神知道吗?

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF=0x3f3f3f3f;
int a[105][105];
int dis[105];
bool vis[105];
int n,m;
void Prime()
{
    for(int i=1; i<=n; i++)
    {
        dis[i]=a[1][i];
        vis[i]=false;
    }
    int ans=0;
    bool flag=false;
    for(int i=1; i<=n; i++)
    {
        int minn=INF;
        int p=-1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<minn)
                minn=dis[p=j];
        }
        int k=0;
        for(int j=1; j<=n; j++)
        {
            if(vis[j]&&a[p][j]==minn)
                k++;
        }
        if(k>1)
        {
            flag=true;
            break;
        }
        vis[p]=1;
        ans+=minn;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]>a[p][j])
                dis[j]=a[p][j];
        }
    }
    if(flag)
        cout<<"Not Unique!"<<endl;
    else
        cout<<ans<<endl;
}
int main()
{
    int T;
    freopen("data/1679.txt","r",stdin);
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<=n; j++)
                if(i==j) a[i][j]=0;
                else a[i][j]=INF;
        }
        int x,y,z;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(z<a[x][y])
                a[x][y]=a[y][x]=z;
        }
        Prime();
    }
    return 0;
}
时间: 2024-10-06 20:58:59

POJ 1679 The Unique MST【MST是否唯一,Prime算法,最好的代码】的相关文章

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST (判断最小生成树是否唯一)

链接:poj 1679 题意:判断最小生成树是否唯一, 若唯一,输出最小权值和,否则,输出  Not Unique! 判断最小生成树是否唯一的思路: 1.对图中的每一条边,扫描其他边,如果存在相同权值的边,则对该边做标记 2.然后用Kruskal算法或Prim算法求MST 3.求得MST后,如果该MST中未包含做了标记的边,即可判断MST唯一: 如果包含做了标记的边,则依次去掉这些边的一条边,再求MST, 如果求得的MST权值和原来的MST的权值一样,即可判断MST不唯一. 个人思路是先求最小生

POJ - 1679 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 followin

poj 1679 The Unique MST (次小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST 【次小生成树】【模板】

题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后添加到最小生成树上,然后把原树上添加了之后形成环的最长的边删去,知道一个最小的.就是次小生成树. 这些需要的都可以在求解最小生成树的时候处理出来. AC代码: #include <cstdio> #include <cstring> #include <iostream> #i

poj 1679 The Unique MST 最小生成树

点击打开链接 http://poj.org/problem?id=1679 题意:给一个无向图,问最小生成树是否唯一,如果唯一就输出最小生成树的所有边的权值的和,如果不唯一,那么就输出Not Unique! 思路:在用prime算法求最小生成树的过程中,在找到权值最小的一个节点之后,先判断一下,这个节点的权值是否可以由好几条路径求得,并且权值都等于当前权值,如果是的话,那么最小生成树就不唯一.那么如何判断呢?可以用一个flag数组标记,0代表这个点得到当前权值的路径唯一,1代表不唯一,初始化全为

poj 1679 The Unique MST (判定最小生成树是否唯一)

题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29408   Accepted: 10520 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spannin

[2016-01-27][POJ][1679][The Unique MST]

C - The Unique MST Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1679 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree):