K - The Unique MST - poj 1679

题目的意思已经说明了一切,次小生成树。。。

************************************************************************************

#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

const int maxn = 105;
const int oo = 0x7fffffff;

int G[maxn][maxn], path[maxn][maxn];//path记录两点之间的最大边值
bool use[maxn][maxn];//记录是否是最小生成树上面的边

int Prim(int N)
{
    int dist[maxn], vis[maxn]={0,1}, pre[maxn];
    int i, ans = 0, T = N-1;

for(i=1; i<=N; i++)
    {
        pre[i] = 1;
        dist[i] = G[1][i];
    }
    while(T--)
    {
        int k = 1, mini = oo;

for(i=1; i<=N; i++)
        {
            if(!vis[i] && mini > dist[i])
                mini = dist[i], k = i;
        }
        use[ pre[k] ][k] = use[k][ pre[k] ] = true;
        ans += mini; vis[k] = true;

for(i=1; i<=N; i++)
        {
            if(vis[i] && k != i)
                path[k][i]=path[i][k] = max(path[pre[i]][i], mini);
            if(!vis[i] && dist[i] > G[k][i])
                dist[i] = G[k][i], pre[i] = k;
        }
    }

return ans;
}
int OK(int N)//判断是否有次小生成树
{
    for(int i=1; i<=N; i++)
    for(int j=i+1; j<=N; j++)
    {
        //如果有边的长度与最小树上的边相等,就说明不唯一了
        if(!use[i][j] && path[i][j]==G[i][j])
            return 0;
    }

return 1;
}

int main()
{
    int T;

scanf("%d", &T);

while(T--)
    {
        int i, j, N, M, u, v, w;

scanf("%d%d", &N, &M);

for(i=1; i<=N; i++)
        for(j=1; j<=N; j++)
        {
            G[i][j] = (i == j ? 0 : oo);
            path[i][j] = 0;
            use[i][j] = false;
        }

while(M--)
        {
            scanf("%d%d%d", &u, &v, &w);
            G[u][v] = G[v][u] = w;
        }

int ans = Prim(N);

if(OK(N) == 0)
            printf("Not Unique!\n");
        else
            printf("%d\n", ans);
    }

}

时间: 2024-08-30 09:44:19

K - The Unique MST - poj 1679的相关文章

The Unique MST POJ - 1679 (次小生成树)

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

The Unique MST POJ - 1679 次小生成树prim

求次小生成树思路: 先把最小生成树求出来  用一个Max[i][j] 数组把  i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过   把没有使用过的一条边加入最小生成树必然回形成一条回路   在这条回路中减去 除加入的边的权值最大的一条边  原图必然保持连通  (如果此时 权值最大的边和新加入的边权值相同  则存在 不同的最小生成树) 把每一条边加入再删除后 即可得出次小生成树 参考了: https://blog.csdn.net/qq_3395144

K - The Unique MST

K - The Unique MST #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct nond{ int x,y,z; }edge[101*101]; int T,N,M,x,y,z,fa[1000],num,ans[1000]; int tot,bns,k,answer=9999999; int cmp

[kuangbin带你飞]专题六 最小生成树 K - The Unique MST (判断最小生成树是否唯一)

K - The Unique MST 题目链接:https://vjudge.net/contest/66965#problem/K 题目: 给定连接的无向图,告诉它的最小生成树是否唯一. 定义1(生成树):考虑连通的无向图G =(V,E). G的生成树是G的子图,比如T =(V',E'),具有以下属性:    1. V'= V.    2.T是连接的和非循环的. 定义2(最小生成树):考虑边加权,连通,无向图G =(V,E). G的最小生成树T =(V,E')是总成本最小的生成树. T的总成本

K度限制MST poj 1639

/* k度限制MST:有一个点的度<=k的MST poj 1639 要求1号点的度不超过k 求MST 我们先把1号点扔掉 跑MST 假设有sum个连通分支 然后把这sum个分支连到1上 就得到了一个sum度的MST 这里往上连的时候 我们连这个分支里 距离1最近的 然后我们在1上加一条边 (即加一个度)得到sum+1度的MST 这里加边的时候 1连出去的每一条边都试一遍 取最小 假设当前1连到了 i 因为原来是个树 这样一搞就形成一个环 我们现在要删去环里面最长边 来得到更小的ans 我么维护d

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【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, 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不唯一. 个人思路是先求最小生