【并查集】牛客 继续畅通工程、畅通工程、畅通工程

题目描述

    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。

输入描述:

    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。

    当N为0时输入结束。

输出描述:

    每个测试用例的输出占一行,输出全省畅通需要的最低成本。

示例1

输入

复制

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

输出

复制

3
1
0

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int tree[101];
int findRoot(int x)
{
    if(tree[x]==-1)
        return x;
    else //找根节点,且路径压缩
    {
        int tmp=findRoot(tree[x]);
        tree[x]=tmp;
        return tmp;
    }
}
typedef struct Edge
{
    int a,b,cost,state;
}Edge;
bool cmp(struct Edge e1,struct Edge e2)
{
    if(e1.state!=e2.state)
    {
        return e1.state>e2.state;
    }
    return e1.cost<e2.cost;
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        int t=n*(n-1)/2;
        struct Edge e[5001];
        for(int i=0;i<n;i++)
        {
            tree[i]=-1;
        }
        for(int i=0;i<t;i++)
        {
            scanf("%d %d %d %d",&e[i].a,&e[i].b,&e[i].cost,&e[i].state);
        }
        sort(e,e+t,cmp);
        int ans=0;
        for(int i=0;i<t;i++)
        {
            int a=findRoot(e[i].a);
            //printf("%d\n",a);
            int b=findRoot(e[i].b);
            //printf("%d\n",b);
            if(a!=b)
            {
                tree[a]=b;
                if(e[i].state!=1)
                {
                    ans+=e[i].cost;
                }
            }
        }
        printf("%d\n",ans);
    }
}

 

题目描述

    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。

输入描述:

    测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M (N, M < =100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。

输出描述:

    对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。

示例1

输入

复制

3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100

输出

复制

3
?

 

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int tree[101];
typedef struct Edge
{
    int a,b,cost;
}Edge;
bool cmp(struct Edge e1,struct Edge e2)
{
    return e1.cost<e2.cost;
}
int findTree(int x)
{
    if(tree[x]==-1)
        return x;
    else
    {
        int tmp=findTree(tree[x]);
        tree[x]=tmp;
        return tmp;
    }
}
int main(){
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF&&n!=0)
    {
        struct Edge e[101];
        for(int i=0;i<n;i++)
            tree[i]=-1;
        for(int i=0;i<n;i++)
        {
            scanf("%d %d %d",&e[i].a,&e[i].b,&e[i].cost);
        }
        sort(e,e+n,cmp);
        int ans=0;int cnt=1;
        for(int i=0;i<n;i++)
        {
            int a=findTree(e[i].a);
           // printf("%d\n",a);
            int b=findTree(e[i].b);
             //printf("%d\n",b);
            if(a!=b)
            {
                tree[a]=b;
                ans+=e[i].cost;
                cnt++;
            }
        }
        if(cnt<m)
            printf("?\n");
        else
        printf("%d\n",ans);
        
    }
}

 

题目描述

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

输入描述:

    测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 
    注意:两个城市之间可以有多条道路相通,也就是说
    3 3
    1 2
    1 2
    2 1
    这种输入也是合法的
    当N为0时,输入结束,该用例不被处理。

输出描述:

    对每个测试用例,在1行里输出最少还需要建设的道路数目。

示例1

输入

复制

4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0

输出

复制

1
0
2
998

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int tree[1001];
typedef struct Edge
{
    int a,b;
}Edge;
int findTree(int x)
{
    if(tree[x]==-1)
        return x;
    else
    {
        int tmp=findTree(tree[x]);
        tree[x]=tmp;
        return tmp;
    }
}

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&n!=0)
    {
        struct Edge e[1001];
        for(int i=0;i<n;i++)
        {
            tree[i]=-1;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&e[i].a,&e[i].b);
        }
        int cnt=0;
        for(int i=0;i<m;i++)
        {
            int a=findTree(e[i].a);
            int b=findTree(e[i].b);
            if(a!=b)
            {
                tree[a]=b;
                cnt++;
            }
        }
        printf("%d\n",n-cnt-1);
    }
}

 

题目描述

    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。

输入描述:

    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
    当N为0时,输入结束,该用例不被处理。

输出描述:

    对每个测试用例,在1行里输出最小的公路总长度。

示例1

输入

复制

3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0

输出

复制

3
5

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int tree[5000];
typedef struct Edge
{
  int a,b,cost;
}Edge;
int findTree(int x)
{
    if(tree[x]==-1)
        return x;
    else
    {
        int tmp=findTree(tree[x]);
        tree[x]=tmp;
        return tmp;
    }
}
bool cmp(struct Edge a,struct Edge b)
{
    return a.cost<b.cost;
}
int main(){
   int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        int t=n*(n-1)/2;
        struct Edge e[5000];
        for(int i=0;i<t;i++)
        {
            scanf("%d %d %d",&e[i].a,&e[i].b,&e[i].cost);
            tree[i]=-1;
        }
        sort(e,e+t,cmp);
        int ans=0;
        for(int i=0;i<t;i++)
        {
            int a=findTree(e[i].a);
            int b=findTree(e[i].b);
            if(a!=b)
            {
                tree[a]=b;
                ans+=e[i].cost;
            }
        }
        printf("%d\n",ans);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值