poj1679次小生成树入门题

次小生成树求法:例如求最小生成树用到了 1、2、4这三条边,总共5条边,那循环3次的时候,每次分别不用1、2、4求得最小生成树的MST,最小的MST即为次小生成树

如下代码maxx即每次删去1,2,4边之后求得的最大边

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=100+10,maxn=111117,inf=0x3f3f3f;

int c[N][N],d[N],n;
bool used[N][N];//the MST i to j max power
int maxx[N][N];//the MST max power
int pre[N];//before edge
int prim()
{
    bool vis[N];
    memset(vis,0,sizeof vis);
    memset(used,0,sizeof used);
    memset(maxx,0,sizeof maxx);
    for(int i=1;i<=n;i++)//the first time used 1
    {
        d[i]=c[1][i];
        pre[i]=1;
    }
    d[1]=0;
    pre[1]=0;
    vis[1]=1;
    int ans=0;
    for(int i=2;i<=n;i++)
    {
        int mind=inf,k;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&mind>d[j])
            {
                mind=d[j];//find the next point that have min value with now
                k=j;
            }
        }
        if(mind==inf)return -1;//not found exit
        ans+=mind;
        vis[k]=1;
        used[k][pre[k]]=used[pre[k]][k]=1;
        for(int j=1;j<=n;j++)
        {
            if(vis[j])maxx[j][k]=maxx[k][j]=max(maxx[j][pre[k]],d[k]);
            if(!vis[j]&&d[j]>c[j][k])
            {
                d[j]=c[j][k];
                pre[j]=k;
            }
        }
    }
    return ans;
}
int smst(int mst)
{
    int ans=inf;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(c[i][j]!=inf&&!used[i][j])
                ans=min(ans,mst+c[i][j]-maxx[i][j]);
        }
    }
    if(ans==inf)return -1;
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t,m;
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j)c[i][j]=0;
                else c[i][j]=inf;
            }
        }
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            cin>>x>>y>>z;
            c[x][y]=c[y][x]=z;
        }
        int ans=prim();
        if(ans==-1)cout<<"Not Unique!"<<endl;
        else
        {
            if(smst(ans)!=ans)cout<<ans<<endl;
            else cout<<"Not Unique!"<<endl;
        }
    }
    return 0;
}

SMST

时间: 2024-10-08 23:47:43

poj1679次小生成树入门题的相关文章

poj1679+次小生成树

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

hdu 4463 Outlets Prim 次小生成树 简单题

Outlets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2200    Accepted Submission(s): 1028 Problem Description In China, foreign brand commodities are often much more expensive than abroad. T

hdu 1679 The Unique MST 次小生成树 简单题

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

POJ1679(次小生成树)

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

uva10600次小生成树模板题

裸题,上模板就行,注意j  !  =  k #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include&

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 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(次小生成树&amp;amp;&amp;amp;Kruskal)

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

poj 2831(次小生成树)

题意:给你一幅图,再给你Q个询问,每个询问为id cost,即如果将id这条边的边权改为cost的话,这条边是否可能是最小生成树中的一条边 解题思路:将第i条边(u,v)的权值修改的话,要判断是否是最小生成树中的一条边,首先要把它加入进去,此时必定会引起原来的生成树成环,所以必定要擦去一条边,擦去的是哪一条边,这就利用到了次小生成树的原理了. 之前写过一个次小生成树的题,现在回过头看,感觉又有点不对了. 这里再总结一次: 首先肯定是构造一颗最小生成树,接下来就是枚举不在生成树里的边,假定为(u,

【总结】 (严格)次小生成树

前言 首先需要了解什么是最小生成树,还要知道什么是倍增(求Lca). 上面的东西如果了解了,就可以开始进入学习的路途了!! 1 算法框架 1.1 整体思路 用不是最小生成树上的边去更新答案. 1.2 具体维护 对于每一个倍增跳上去的,要维护两个东西: 路径的边权最大值. 路径的边权次大值 2 具体实现 我们考虑一下对于每一条边(不在最小生成树上),如果要把它加入答案,如何更新? MST-路径最大值+边权. 然后这个东西就可以很愉快地解决了... 其实不是的,如果题目求的是次小生成树,这题就没了,