I - Kruskal+一点性质 UVALive - 6837There is No Alternative

该博客介绍了如何使用Kruskal算法寻找图中最小生成树,并通过比较不同最小生成树的构建来确定哪些边是所有连通所有岛屿的必要桥梁,从而最小化成本。文章提供了输入输出示例,解释了如何计算没有替代的必要桥梁的数量及其总成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I - Kruskal+一点性质

 UVALive - 6837

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. Figure F.1. Three sets of bridges connecting all the islands for Sample Input 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. Figure F.2. No alternative bridges for Sample Input 1, 2 and 3 Write a program that advises the mayor which bridges are no alternative bridges for the given input. Input The input file contains several test cases, each of them has the following format. 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 For each test case, output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space. Sample Input 4 4 1 2 3 1 3 3 2 3 3 2 4 3 4 4 1 2 3 1 3 5 2 3 3 2 4 3 4 4 1 2 3 1 3 1 2 3 3 2 4 3 3 3 1 2 1 2 3 1 1 3 1 Sample Output 1 3 3 9 2 4 0 0

 

题意:n个点,m条边,问有多少边是最小生成树必须的边,权值之和为多少

思路:先求一遍最小生成树,然后在这n-1条边里每次删去一条,再求一遍最小生成树,如果生成树权值变了,这条边就是必须的

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <malloc.h>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define clc2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f
#define BASE 131

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
using namespace std;
const int maxn=50000+11;
int N,M,ans;//点,边
struct edge
{
    int u,v;//点u到v
    int var;//权值
}ma[maxn];
int vis[maxn];
set<int>bian;
bool cmp(edge a,edge b)
{
    return a.var<b.var;
}
int fa[maxn];
void initKruskal()
{
    ans=0;
    fora2(i,1,N)
    {
        fa[i]=i;
    }
    sort(ma+1,ma+M+1,cmp);

}
int findx(int x)
{
    if(x==fa[x])return x;
    return fa[x]=findx(fa[x]);
}
bool unio(int x,int y)
{
    int fx=findx(x),fy=findx(y);
    if(fx==fy)return false;
    fa[fy]=fx;
    return true;
}
void kruskal()
{
    initKruskal();
    int m=0;
    fora2(i,1,M)
    {
        if(!vis[i])continue;
        if(unio(ma[i].u,ma[i].v))
        {
            m++;
            ans+=ma[i].var;
            bian.insert(i);
        }
        if(m==N-1)return;
    }
}
bool OK()
{
    fora2(i,1,N)fa[i]=i;
    int m=0;
    int sum=0;
    fora2(i,1,M)
    {
        if(!vis[i])continue;
        if(unio(ma[i].u,ma[i].v))
        {
            m++;
            sum+=ma[i].var;
            //bian.insert(i);
        }
        if(m==N-1)break;
    }
    if(sum!=ans)return true;
    return false;
}
void tryCut()
{
    int ans1=0,ans2=0;
    for(set<int>::iterator it=bian.begin();it!=bian.end();it++)
    {
        vis[*it]=0;
        if(OK())
        {
            ans1++;
            ans2+=ma[*it].var;
        }
        vis[*it]=1;
    }
    printf("%d %d\n",ans1,ans2);
}
int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        int k=1;
        while(k<=M)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            ma[k].u=x;
            ma[k].v=y;
            ma[k].var=z;
            k++;
        }

        if(M==1){printf("0 0\n");continue;}
        memset(vis,1,sizeof(vis));
        bian.clear();
        kruskal();
        tryCut();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值