There is No Alternative(最小生成树+模拟)

问题 F: There is No Alternative

时间限制: 3.000 Sec  内存限制: 128 MB
提交 状态

题目描述

ICPC (Isles of Coral Park City) consist of several beautiful islands.
The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.
The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.
However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in figure F.1.

As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.
Write a program that advises the mayor which bridges are no alternative bridges for the given input.

输入

The input consists of a single test case.

N M
S1 D1 C1
.
.
.
SM DM CM
The first line contains two positive integers N and M. N represents the number of islands and each island is identified by an integer 1 through N. M represents the number of the pairs of islands between which a bridge may be built.
Each line of the next M lines contains three integers Si, Di and Ci (1 ≤ i ≤ M) which represent that it will cost Ci to build the bridge between islands Si and Di. You may assume 3 ≤ N ≤ 500,N − 1 ≤ M ≤ min(50000, N(N − 1)/2), 1 ≤ Si < Di ≤ N, and 1 ≤ Ci ≤ 10000. No two bridges connect the same pair of two islands, that is, if i ≠ j and Si = Sj, then Di ≠ Dj. If all the
candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.

输出

Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.

样例输入 Copy

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

样例输出 Copy

1 3

思路

先把完整的图的最小生成树找到,标记边,然后一条一条减边,在忽略那条边的情况下再跑一遍Kluskal,看得到的答案跟最初记录的答案是否相同。

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int n,m,f[510];
bool flag;  //设置只有第一次才会记录最小生成树的边
struct Edge
{
    int u,v,w;
    bool f;  //是否是最小生成树的边
}e[51000];
bool cmp(Edge a, Edge b)
{
    return a.w < b.w;
}
int find(int x)
{
    if(x==f[x]) return x;
    return f[x]=find(f[x]);
}
int Kluskal(int n,int num)
{
    for(int i=1;i<=n;i++) f[i]=i;
    int cnt=0,ans=0;
    for(int i=0; i<m; i++)
    {
        if(i==num) continue;  //忽略第num条边
        int u=e[i].u,v=e[i].v,w=e[i].w;
        int t1=find(u),t2=find(v);
        if(t1 != t2)
        {
            if(!flag) e[i].f=true;  //只有第一次才记录边
            ans += w;
            f[t1]=t2;
            cnt++;
        }
        if(cnt == n-1)break;
    }
    if(cnt < n-1) return -1;
    return ans;
}
inline int Found(int start)
{
    for(int i=start; i<m; i++)
    {
        if(e[i].f)
        {
            e[i].f=false;
            return i; //找到最小生成树的边就返回
        }
    }
    return -1;
}
int main()
{
    cin>>n>>m;
    for(int i=0; i<m; i++)
    {
        cin>>e[i].u>>e[i].v>>e[i].w;
        e[i].f=false;
    }
    sort(e,e+m,cmp);
    int ans = Kluskal(n,-1);
    flag=true;
    int sumnum=0;  //不可替代边的权值和
    int sum=0;     //不可替代边的个数
    int num=0;
    for(int i=1; i<=n-1; i++) //一个个的去掉n-1个
    {
        num = Found(num);
        int temp=Kluskal(n,num);
        if(temp != ans)   //去掉不是原来的值,说明不可替代
        {
            sum+= e[num].w;
            sumnum++;
        }
    }
    cout<<sumnum<<' '<<sum<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值