ACM 继续畅通工程
当N为0时输入结束。
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
1
0
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int a,b,v;
}loab[]; int parent[]; void init()
{
for(int i = ; i < ; i++)
parent[i] = i;
} int find(int n)
{
if(parent[n] == n)
return n;
else
return find(parent[n]);
} bool merge(int a,int b)
{
a = find(a);
b = find(b);
if(a != b)
{
parent[b] = a;
return true;
}else{
return false;
}
} bool cmp(node a,node b) //sort函数定义
{
return a.v < b.v;
}
int main()
{
int n,flag,t;
/*
~scanf("%d",&n)&&n
*/
while(cin>>n&&n)
{ init(); //parent数组初始化
t = n*(n-)/;
for(int i = ; i < t; i++)
{
//cin>>loab[i].a>>loab[i].b>>loab[i].v>>flag; //使用cin输入时 会TLE
scanf("%d%d%d%d",&loab[i].a,&loab[i].b,&loab[i].v,&flag);
if(flag) //当道路已经建成时把值重设为0,这样把问题给简单化
loab[i].v = ;
}
sort(loab,loab+t,cmp);
int sum = ;
for(int i = ; i < t; i++)
{
bool temp = merge(loab[i].a,loab[i].b); //构建最小生成树
if(temp) //只取道路成本较小的
sum += loab[i].v;
}
cout << sum <<endl;
}
return ;
}
ACM 继续畅通工程的更多相关文章
- ACM: 还是畅通工程-并查集-最小生成树-解题报
还是畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查乡村交通 ...
- ACM: 继续畅通工程-并查集-最小生成树-解题报告
继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...
- ACM 还是畅通工程
Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直 ...
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- ACM: 畅通工程-并查集-解题报告
畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查城镇交通状况 ...
- ACM题目————还是畅通工程
Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...
- ACM 畅通工程2
Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到 ...
- ACM 畅通工程
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...
- HDU ACM 1879 继续畅通工程
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- python基础(初识Python)
python基础(初识Python) 本章内容: Python 的种类 Python 的环境 Python 入门(解释器.编码.pyc文件.脚步传入参数.变量.输入.流程控制与缩进.while循环) ...
- [转]scrapy中的request.meta
作者:知乎用户链接:https://www.zhihu.com/question/54773510/answer/146971644 meta属性是字典,字典格式即{'key':'value'},字典 ...
- Django项目实战之用户上传与访问
1 将文件保存到服务器本地 upload.html <!DOCTYPE html> <html lang="en"> <head> <me ...
- Python将excel文件从xls转换为xlsx
本文使用场景:将一个xls格式Excel文件转换为xlsx文件格式.接下来将一步一步演示该操作.你也可以对代码进行修改使其适用于你所需的场景. 安装Python3 首先需要安装Python,我这里安装 ...
- 学习React系列(五)——使性能最优
提高性能可分为两方面: 一.配置层面 二.代码层面 本文只从代码层面考虑: 一.避免重复渲染 这里要说一句: 当shouldComponentUpdate返回false的时候不触发render函数也就 ...
- [LeetCode] Next Closest Time 下一个最近时间点
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- Jenkins + Gradle + pgyer + Android自动发布
Jenkins配置与必要的环境配置 一:Jenkins服务端(Linux系统为例说明): 1.jdk安装与配置 2.SDK安装与配置 3.安装配置对应的gradle版本(建议gradle版本在4.1版 ...
- bzoj 1272: [BeiJingWc2008]Gate Of Babylon
Description Solution 如果没有限制,答案就是 \(\sum_{i=0}^{m}C(n+i-1,i)\) 表示枚举每一次取的个数,且不超过 \(m\),方案数为可重组合 发现这个东西 ...
- 2015 多校联赛 ——HDU5350(huffman)
Problem Description MZL is a mysterious mathematician, and he proposed a mysterious function at his ...
- SPOJ 7258 Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...