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. 每天学点SpringMVC-拦截器

    1. 先写个Hello World 1.1 写一个Interceptor class并实现HandlerInterceptor接口 public class FirstInterceptor impl ...

  2. 入坑IT十年(二)技术以外

    上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...

  3. C-一行或多行文章垂直居中

    1 样式效果 2 table布局 li span 

  4. vim 环境设定(通用)

    有没有发现,如果我们以 vim 软件来搜寻一个档案内部的某个字符串时,这个字符串会被反白,而下次我们再次以 vim 编辑这个档案时,该搜寻的字符串反白情况还是存在呢!甚至于在编辑其他档案时,如果其他档 ...

  5. jsp 四大域范围

     JSP中四种作用域的不同 作用域规定的是变量的有效期限. 1.如果把变量放到pageContext里,就说明它的作用域是page,它的有效范围只在当前jsp页面里. 从把变量放到pageCont ...

  6. this到底指向哪里

    this指向调用它的对象 首先要明确,this指向调用方,谁调用,this指向谁. 直接调用 举个栗子: var test = 'window' ; function testThis () { va ...

  7. python concurrent.futures

    python因为其全局解释器锁GIL而无法通过线程实现真正的平行计算.这个论断我们不展开,但是有个概念我们要说明,IO密集型 vs. 计算密集型. IO密集型:读取文件,读取网络套接字频繁. 计算密集 ...

  8. WebApi系列~HttpClient的性能隐患

    回到目录 最近在进行开发过程中,基于都是接口开发,A站接口访问B接口接口来请求数据,而在这个过程中我们使用的是HttpClient这个框架,当然也是微软自己的框架,性能当前没有问题,但如果你直接使用官 ...

  9. BeginInvoke()使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  10. win10 uwp 存放网络图片到本地

    有时候我们的网络很垃圾,我的的UWP要在第一次打开网络图片,就把图片存放到本地,下次可以从本地打开. 有时候用户使用的是流量网络,不能每次都联网下载. 我们不得在应用存放用户打开的图片. 这就是先把图 ...