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. Angularjs –– Expressions(表达式)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Angular的表达式 Angular的表达式和JavaScript代码很像,不过通常A ...

  2. taobao_api项目开坑,自主完成淘宝主要接口的开发-版本:卖家版(非淘宝api)

    项目名称:taobao_api 项目目的:独立实现各个淘宝操作的相关api,不依赖淘宝提供的api,而是自己实现接口 前期实现接口:已付款订单查询(自动更新), 订单发货 , 订单备注 应用场景:中小 ...

  3. unset与unlink

    unset() -- 释放给定的变量 详见->http://www.kuqin.com/php5_doc/function.unset.html unlink() --删除文件    常用于用户 ...

  4. httpd2.2配置文件详解

    httpd2.2官方配置手册:http://httpd.apache.org/docs/2.2/ 注意:关闭防火墙,iptables规则 vim /etc/sysconfig/selinux SELI ...

  5. clipboard.js 介绍

    这是著名开源项目 clipboard.js 的 README.md,我把它翻译成中文.发出来,方便自己和他人阅读. 项目地址:https://github.com/zenorocha/clipboar ...

  6. JavaWeb(三)JSP概述

    一.JSP概述 1.1.JSP简介 一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结束.JSP是一种Java servlet,主要用于实现 ...

  7. 项目管理软件之争,禅道和JIRA大对比

    本文摘要: 一. 产品介绍 二. 界面设计 1. 界面颜色设计 2. 布局结构 三. 功能区别 四. 价格对比 五. 后期服务 六. 优缺点 七. 总结 说到项目管理软件,不得不提的是禅道和JIRA. ...

  8. bootstrap 的可编辑下拉框 jquery.editable-select

    搜了半天发现在某处下载jquery.editable-select需要积分,于是整理出来方便 其他人. 先上下载链接:http://pan.baidu.com/s/1kUXvwlL      pass ...

  9. 【转】linux IO子系统和文件系统读写流程

    原文地址:linux IO子系统和文件系统读写流程 我们含有分析的,是基于2.6.32及其后的内核. 我们在linux上总是要保存数据,数据要么保存在文件系统里(如ext3),要么就保存在裸设备里.我 ...

  10. HiveQL简单操作DDL

    hive-2.1.1 DDL操作 Create/Drop/Alter/Use Database 创建数据库 //官方指导 CREATE (DATABASE|SCHEMA) [IF NOT EXISTS ...