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. epoll的ET和LT两种模式对比及注意事项

    ET模式: 因为ET模式只有从unavailable到available才会触发,所以 1.读事件:需要使用while循环读取完,一般是读到EAGAIN,也可以读到返回值小于缓冲区大小: 如果应用层读 ...

  2. css之outline实现圆角效果

    对,你没有看错,outline也可以实现圆角,不过请先读完自然会知道结果: ============================================================== ...

  3. Divisors poj2992

    Divisors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9940   Accepted: 2924 Descript ...

  4. h5 meta学习

    定义针对搜索引擎的关键词:<meta name="keywords" content="meta,red" /> 定义对页面的描述:<meta ...

  5. CSS中新属性calc()

    CSS3的calc()使用 原文: http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html © w3cplus.com calc( ...

  6. Windows下MySQL5.6.21安装步骤

    01.把 mysql-advanced-5.6.17-winx64.zip 解压到自定义 D:\mysql-5.6.17-W64 或 D:\mysql-advanced-5.6.17-winx64 目 ...

  7. Win CE 6.0 获取手持机GPS定位1----基础知识 (C#)

    一.GPS全球定位系统的组成 (1)GPS卫星(空间部分) 由沿接近环形的地球轨道运行的24颗卫星组成,位于距地表20200千米的高空,均匀分布在6个轨道面上(每个轨道面4颗),轨道倾角55度.此外, ...

  8. Layer弹层组件 二次扩展,项目中直接使用。

    /************************ Layer扩展 ****************************/ /* * Layer弹出Alert提示框 * @param messag ...

  9. AlexNet 网络详解及Tensorflow实现源码

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 图片数据处理 2. 卷积神经网络 2.1. 卷积层 2.2. 池化层 2.3. 全链层 3. AlexNet 4. 用Tensorflow搭 ...

  10. java 集合之实现类ArrayList 和 LinkedList

    List 的方法列表 方法名 功能说明 ArrayList() 构造方法,用于创建一个空的数组列表 add(E e) 将指定的元素添加到此列表的尾部 get(int index) 返回此列表中指定位置 ...