ACM Contest and Blackout -次小生成树

  • ACM Contest and Blackout

  •  UVA - 10600 
  • 确实是跑完最小生成树之后 无脑枚举所有边去找出一条次小生成树
  • #include<bits/stdc++.h>
    using namespace std;
    #define maxn 107
    const int INF = 0x3f3f3f3f;
    int t,n,m,ans,ans2,pre[maxn],fa[maxn],x,y,cnt,cur;
    struct node
    {
        int a, b, c;
    } a[maxn*maxn];
    bool cmp(node x,node y)
    {
        return x.c<y.c;
    }
    int fond(int x, int pre[])
    {
        return pre[x] == x ? x : pre[x] = fond(pre[x], pre);
    }
    void solve()
    {
        vector<node> used;
        sort(a, a+m,cmp);
        cnt=ans= 0;
        for(int i=1; i<=n; i++)
            pre[i] = i;
        for(int i=0; i<m; i++)
        {
            x = fond(a[i].a, pre);
            y = fond(a[i].b, pre);
            if(x == y)continue;
            pre[x] = y;
            used.push_back(a[i]);
            ++cnt;
            ans += a[i].c;
            if(cnt == n-1) break;
        }
        ans2 = INF;
        int len =used.size();
        for(int i=0; i<len; i++)
        {
            node v = used[i];
            cur =cnt = 0;
            for(int j=1; j<=n; j++)fa[j]=j;
            for(int j=0; j<m; j++)
            {
                if(v.a == a[j].a && v.b == a[j].b && v.c == a[j].c) continue;
                int x = fond(a[j].a, fa), y = fond(a[j].b, fa);
                if(x == y)continue;
                fa[x] = y;
                ++cnt;
                cur += a[j].c;
                if(cnt == n-1) break;
            }
            if(cnt == n-1) ans2 = min(ans2, cur);
        }
    }
    int main()
    {
        cin>>t;
        while(t--)
        {
            cin>>n>>m;
            for(int i=0; i<m; i++) cin>>a[i].a>>a[i].b>>a[i].c;
            solve();
            cout<<ans<<" "<<ans2<<endl;
        }
        return 0;
    }
    
  •  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值