继续畅通工程

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 92   Accepted Submission(s) : 43

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Author

ZJU

Source

浙大计算机研究生复试上机考试-2008年

————————————————————————————————————

kruskal求最小生成树

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std; int m,n,pa[105];
struct node
{
int u,v,c;
} s[100005]; bool cmp(node a,node b)
{
return a.c<b.c;
} void init()
{
for(int i=0; i<105; i++)
pa[i]=i;
} int fin(int x)
{
if(pa[x]!=x)
{
pa[x]=fin(pa[x]);
}
return pa[x];
} void un(int a,int b)
{
int r1=fin(a);
int r2=fin(b);
if(r1!=r2)
{
pa[r1]=pa[r2];
}
} void kruskal()
{ int sum=0;
int num=0;
int flag=0;
for(int i=0; i<m; i++)
{
if(fin(s[i].u)!=fin(s[i].v))
{
un(s[i].u,s[i].v);
num++;
sum+=s[i].c;
if(num>=n-1)
{
flag=1;
break;
}
}
}
if(flag)
printf("%d\n",sum);
else
printf("?\n");
} int main()
{
int u,v,c,f;
while(scanf("%d",&n)&&n)
{
init();
m=n*(n-1)/2;
for(int i=0; i<m; i++)
{
scanf("%d%d%d%d",&u,&v,&c,&f);
s[i].u=u;
s[i].v=v;
s[i].c=c;
if(f==1)
s[i].c=0;
}
sort(s,s+m,cmp);
kruskal();
}
return 0;
}

HDU1879 继续畅通工程 2017-04-12 19:12 50人阅读 评论(0) 收藏的更多相关文章

  1. POJ1269 Intersecting Lines 2017-04-16 19:43 50人阅读 评论(0) 收藏

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15478   Accepted: 67 ...

  2. HDU1875 畅通工程再续 2017-04-12 19:52 48人阅读 评论(0) 收藏

    畅通工程再续 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  3. HDU1233 还是畅通工程 2017-04-12 19:49 64人阅读 评论(0) 收藏

    还是畅通工程 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. HDU1863 畅通工程 2017-04-12 19:25 59人阅读 评论(0) 收藏

    畅通工程 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  5. HDU1232 畅通工程 2017-04-12 19:20 53人阅读 评论(0) 收藏

    畅通工程 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  6. 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 83276 Accepted: 31159 Description Mich ...

  7. HDU6029 Happy Necklace 2017-05-07 19:11 45人阅读 评论(0) 收藏

    Happy Necklace                                                                           Time Limit: ...

  8. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  9. Python获取当前时间 分类: python 2014-11-08 19:02 132人阅读 评论(0) 收藏

    Python有专门的time模块可以供调用. <span style="font-size:14px;">import time print time.time()&l ...

随机推荐

  1. GNU Radio: Multiple USRP configurations 配置多个USRP设备

    Introduction 引言 Some USRP devices are capable of being grouped to form a single, virtual device. A s ...

  2. SpringCloud初体验:四、API GateWay 服务网关

    网关服务很多,比如:Zuul.Kong.spring cloud gateway ……, 这里不纠结哪种性能好,本次体验是用的 spring cloud gateway 更多网关比较可以了解这篇文章: ...

  3. Ubuntu 16.04 natural scrolling

    http://ubuntuhandbook.org/index.php/2016/05/install-ubuntu-tweak-in-ubuntu-16-04/ download ubuntu-tw ...

  4. Java中return的语句

    1.return语句的作用:a.返回一个值,这个值可以是任意类型.b.使程序返回到操作系统(即终止程序)2.java中对于一个函数,不论有没有返回值类型,都可以带有return 语句.但是区别在于,r ...

  5. GCC参数详解 二

    1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...

  6. Quick guide for converting from JAGS or BUGS to NIMBLE

    Converting to NIMBLE from JAGS, OpenBUGS or WinBUGS NIMBLE is a hierarchical modeling package that u ...

  7. js将UTC时间转化为当地时区时间(UTC转GMT)

    我们在进行网站开发的时候有可能会涉及到国外的用户或者用户身在国外,这时就会存在时差问题,比如说我们在中国的时间是08:00,但是此时韩国的时间是09:00,如果在网页上需要进行相关显示的话就会出现问题 ...

  8. Blueprint 编译概述

    转自:http://blog.csdn.net/cartzhang/article/details/39637269 一.术语 Blueprint,像C++语言一下的,在游戏中使用前需要编译.当你在B ...

  9. tensorflow-base_operations

    # -*- coding: utf-8 -*-import tensorflow as tf# 基本的常量操作,通过构造函数返回值 定义值的操作operationsa = tf.constant(2) ...

  10. 「小程序JAVA实战」小程序视频组件与api介绍(51)

    转自:https://idig8.com/2018/09/22/xiaochengxujavashizhanxiaochengxushipinzujianyuapijieshao50/ 这次说下,小程 ...