博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1879 hdoj 1879
阅读量:4122 次
发布时间:2019-05-25

本文共 1318 字,大约阅读时间需要 4 分钟。

继续畅通工程

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5908    Accepted Submission(s): 2444
Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。
当N为0时输入结束。
 
Output
每个测试用例的输出占一行,输出全省畅通需要的最低成本。
 
Sample Input
31 2 1 01 3 2 02 3 4 031 2 1 01 3 2 02 3 4 131 2 1 01 3 2 12 3 4 10
 
Sample Output
310

#include<stdio.h>

int father[104];
int n,i;
typedef struct country{
    int x;
    int y;
    int cost;
    int build;
}country;
void init(){
    for(i=1;i<=n;i++){
        father[i]=i;
    }
}
int compare(const void *a,const void *b){
    return (*(country*)a).cost-(*(country*)b).cost;
}
int find(int x){
    if(x==father[x]) return x;
    find(father[x]);
}
int main(){
    int cost,count,m,x,y;
    country cou[5055];
    while(scanf("%d",&n),n!=0){
        cost=0,count=0;
        init();
        m=n*(n-1)/2;
        for(i=0;i<m;i++){
            scanf("%d%d%d%d",&cou[i].x,&cou[i].y,&cou[i].cost,&cou[i].build);
            if(cou[i].build==1){
                cou[i].cost=0;
            }
        }
        qsort(cou,m,sizeof(country),compare);
        for(i=0;i<m;i++){
            x=find(cou[i].x),y=find(cou[i].y);
            if(x!=y){
                father[x]=y;
                cost+=cou[i].cost;
                count++;
            }
        }
        printf("%d\n",cost);
    }
    return 0;
}

转载地址:http://bxtpi.baihongyu.com/

你可能感兴趣的文章
一篇搞懂Java反射机制
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】树
查看>>
MySQL主从复制不一致的原因以及解决方法
查看>>
RedisTemplate的key默认序列化器问题
查看>>
序列化与自定义序列化
查看>>
ThreadLocal
查看>>
从Executor接口设计看设计模式之最少知识法则
查看>>
OKhttp之Call接口
查看>>
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
关于Content-Length
查看>>
WebRequest post读取源码
查看>>
使用TcpClient可避免HttpWebRequest的常见错误
查看>>
EntityFramework 学习之一 —— 模型概述与环境搭建 .
查看>>
C# 发HTTP请求
查看>>
启动 LocalDB 和连接到 LocalDB
查看>>
Palindrome Number --回文整数
查看>>
Reverse Integer--反转整数
查看>>
Container With Most Water --装最多水的容器(重)
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>