Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2
行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。

当N为0时输入结束。

Output
每个测试用例的输出占一行,输出全省畅通需要的最低成本。
Sample Input
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
Sample Output
3 1 0
题意:都是汉语;
解题思路:想先将已经建好的公路建成一棵树,再kruskal;
感悟:下次不能看总决赛写代码了,函数里忘了初始化,检查了两个小时没看出来,(骑士主场哨太严重了!!!)
代码:
#include

using namespace std;

#define N 110

struct node

{

    int
i,j,w,f;//从i到j需要w,f表示是否连通

};

node fr[N*N];

int bin[N];

bool comp(node a,node b)

{

    return
a.w

}

int find1(int x)

{

    int
r=x;

   
while(bin[r]!=r)

       
r=bin[r];

    return
r;

}

int kruskal(int n,int m)

{

   
sort(fr,fr+m,comp);

    int
s=0;

    for(int
i=0;i

    {

       
int fx=find1(fr[i].i);

       
int fy=find1(fr[i].j);

       
if(fx!=fy)

       
{

           
//cout<<"p="<<fr[i].f<<endl;

           
bin[fx]=fy;

           
//cout<<bin[fy]<<" "<<fy<<endl;

           
s+=fr[i].w;

       
}

    }

    return
s;

}

int main()

{

   
//freopen("in.txt", "r", stdin);

    int
n,t;

   
while(scanf("%d",&n)!=EOF&&n)

    {

       
t=n*(n-1)/2;

       
for(int i=0;i<=n;i++)

           
bin[i]=i;

       
for(int i=0;i

       
{

           
scanf("%d%d%d%d",&fr[i].i,&fr[i].j,&fr[i].w,&fr[i].f);

//printf("%d %d %d %d\n",fr[i].i,fr[i].j,fr[i].w,fr[i].f);

           
if(fr[i].f)

           
{

               
//cout<<"这是通的"<<fr[i].i<<"
"<<fr[i].j<<endl;

               
int x=find1(fr[i].i);

               
int y=find1(fr[i].j);

               
//cout<<x<<" "<<y<<endl;

               
if(x!=y)

                   
bin[x]=y;

           
}

       
}

       
int ans=kruskal(n,t);

       
printf("%d\n",ans);

    }

    return
0;

}

Problem E 的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. 关于ios::sync_with_stdio(false)

    作用就是取消同步,这样的话使用cin就和使用scanf效率相似. 但是今天在做题的时候碰到一点小问题,就是在关闭同步的时候使用scanf是交了一发代码,然后RE了(经检查scanf没有写错),而把关同 ...

  2. KMP算法的来龙去脉

    1. 引言 字符串匹配是极为常见的一种模式匹配.简单地说,就是判断主串TT中是否出现该模式串PP,即PP为TT的子串.特别地,定义主串为T[0-n−1]T[0-n−1],模式串为P[0-p−1]P[0 ...

  3. 翻译 | 使用A-Frame打造WebVR版《我的世界》

    原文地址:Minecraft in WebVR with HTML Using A-Frame 原文作者:Kevin Ngo 译者:Felix 校对:阿希 我是 Kevin Ngo,一名就职于 Moz ...

  4. shell脚本获取文件中key/value的小方法

    方法有N种,awk.sad.grep.cut... 以上几种方式不写了,就写两个不太常用到的. 废话少说,直接上代码: cat a.txt aa.gif=aaaa.gif bb.gif=bbbb.gi ...

  5. Java数据结构和算法总结-冒泡排序、选择排序、插入排序算法分析

    前言:排序在算法中的地位自然不必多说,在许多工作中都用到了排序,就像学生成绩统计名次.商城商品销量排名.新闻的搜索热度排名等等.也正因为排序的应用范围如此之广,引起了许多人深入研究它的兴趣,直至今天, ...

  6. 数据的分类-JavaScript数据类型

    JavaScript数据类型 1.数据类型是什么? 我们接触的绝大多数程序语言来说,把数据都进行了分类,包括数字.字符.逻辑真假:int,long,string,boolean....等等:我们都知道 ...

  7. 非对称加密RSA的C#实现

    1.对称加密算法 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥(secret key). 对称加密有很多种算法,由于它效率很高,所 ...

  8. Yii 2.0 数据库操作总结

    1. 概述 操作数据库有2种方式: DAO(data access object),不安全 ORM(onject relational mapping) 2. DAO Yii::app()->d ...

  9. c# 【MVC】WebApi设置返回Json

    public static HttpResponseMessage toJson(Object obj) { String str; if (obj is String || obj is Char) ...

  10. Java策略模式以及来自lambda的优化

    前言    设计模式是软件工程中一些问题的统一解决方案的模型,它的出现是为了解决一些普遍存在的,却不能被语言特性直接解决的问题,随着软件工程的发展,设计模式也会不断的进行更新,本文介绍的是经典设计模式 ...