您当前的位置: 首页 > 

MangataTS

暂无认证

  • 0浏览

    0关注

    423博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

畅通工程(并查集模版题)

MangataTS 发布时间:2020-02-02 18:23:00 ,浏览量:0

题意:

多组输入N,M,当N为0退出人输入,N是道路数目,M是村庄总数,随后N行,每行输入三个数两个村庄的编号,以及连接这两个村庄的费用。对每一组数据输出畅通工程的最低费用,如果不能畅通就输出“?”(不包括双引号)

这道题有两道链接:

一道是fjut的链接,另外一道是hdu的

http://www.fjutacm.com/Problem.jsp?pid=1214

http://acm.hdu.edu.cn/showproblem.php?pid=1863

思路:其实这道题就是一道排序+并查集题,将每条路的费用进行排序然后依次合并,然后费用增加,当N中情况都试过了,再来一个循环判断是否全部连接。

好啦,我们来看AC代码吧:

#include
#include
#include
#include
using namespace std;
int fa[105];
struct node//定义这个结构体,a代表的是一个村庄的编号,b代表的是另一个村庄的编号,cost顾名思义 
{
    int a;
    int b;
    int cost;    
};
int find(int x)
{
    int r=x,temp;
    while(r!=fa[r])
    r=fa[r];
    while(x!=fa[x])
    {
        temp=fa[x];
        fa[x]=r;
        x=temp;
    }
    return x;
}
bool cmp(node x,node y)//cmp排序 
{
    return x.cost>n>>m&&n)
    {
        int sum=0;
        for(int i=1;icp[i].a>>cp[i].b>>cp[i].cost;
        }
        sort(cp+1,cp+n+1,cmp);//排序 
        for(int i=1;i            
关注
打赏
1665836431
查看更多评论
0.0359s