Nyoj 布线问题(并查集&&图论)
描述南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:
1、把所有的楼都供上电。
2、所用电线花费最少
- 输入
- 第一行是一个整数n表示有n组测试数据。(n<5)
每组测试数据的第一行是两个整数v,e.
v表示学校里楼的总个数(v<=500)
随后的e行里,每行有三个整数a,b,c表示a与b之间如果建铺设线路花费为c(c<=100)。(哪两栋楼间如果没有指明花费,则表示这两栋楼直接连通需要费用太大或者不可能连通)
随后的1行里,有v个整数,其中第i个数表示从第i号楼接线到外界供电设施所需要的费用。( 0<e<v*(v-1)/2 )
(楼的编号从1开始),由于安全问题,只能选择一个楼连接到外界供电设备。
数据保证至少存在一种方案满足要求。 - 输出
- 每组测试数据输出一个正整数,表示铺设满足校长要求的线路的最小花费。
- 样例输入
-
1
4 6
1 2 10
2 3 10
3 1 10
1 4 1
2 4 1
3 4 1
1 3 5 6 - 样例输出
-
4
- 来源
- [张云聪]原创
- 上传者
- 张云聪
自己定义的排序函数没有写在前边 Runtime Error多次......
排序之后,用并查集即可,,,Prim也可以处理....
并查集代码:
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 1000000 int v;
int dis[];
struct node{
int a,b,cost;
}c[]; bool cmp(node x,node y)
{
return x.cost<y.cost;
} void init()
{
for(int i=; i<=v; i++){
dis[i]=i;
}
} int find(int x)
{
if(dis[x]!=x)
dis[x]=find(dis[x]);
return dis[x];
} int main()
{
int n,e;
scanf("%d",&n);
while(n--){
int min=INF,m,sum=;
scanf("%d%d",&v,&e);
init();
for(int i=; i<e; i++){
scanf("%d%d%d",&c[i].a,&c[i].b,&c[i].cost);
}
for(int i=; i<v; i++){
scanf("%d",&m);
if(min>m)
min=m;
}
sort(c,c+e,cmp);
for(int i=; i<e; i++){
int x=find(c[i].a);
int y=find(c[i].b);
if(x!=y){
dis[x]=y;
sum+=c[i].cost;
}
}
printf("%d\n",sum+min);
}
return ;
}
Prim代码:
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 1000000 int dis[],vis[],mp[][];
int v; int Prim()
{
fill(dis,dis+v,INF);
memset(vis,,sizeof(vis));
int n=,ans=;
for(int i=; i<=v; i++){
if(i!=n)
dis[i]=mp[n][i];
}
vis[]=;
while(true){
int min=INF;
for(int j=; j<=v; j++){
if(!vis[j] && min>dis[j]){
min=dis[j];
n=j;
}
}
if(min==INF)
break;
ans+=min;
vis[n]=;
for(int i=; i<=v; i++){
if(!vis[i] && dis[i]>mp[n][i])
dis[i]=mp[n][i];
}
}
return ans;
} int main()
{
int n,e,i,x,y,cost;
scanf("%d",&n);
while(n--){
int sum=,min=INF,m;
memset(mp,INF,sizeof(mp));
scanf("%d%d",&v,&e);
for(i=; i<e; i++){
scanf("%d%d%d",&x,&y,&cost);
mp[x][y]=mp[y][x]=cost;
}
for(i=; i<v; i++){
scanf("%d",&m);
if(min>m)
min=m;
}
sum=min+Prim();
printf("%d\n",sum);
}
}
Nyoj 布线问题(并查集&&图论)的更多相关文章
- P1197 [JSOI2008]星球大战[并查集+图论]
题目来源:洛谷 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球 ...
- HDU1878 欧拉回路---(并查集+图论性质)
http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel
有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...
- nyoj 711 枚举+并查集
#include<stdio.h>//从大到小不断枚举边直到找到s-t的路径,判断从s可以到t可以用并查集来判断 #include<stdlib.h>//枚举最大的一条边肯定 ...
- nyoj 1022 合纵连横 (并查集<节点删除>)
合纵连横 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...
- 并查集(图论) LA 3644 X-Plosives
题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...
- HDU 3974 Assign the task 并查集/图论/线段树
Assign the task Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- nyoj 38 简单并查集的应用&最小生成树
#include<stdio.h> #include<stdlib.h> #define inf 0x3fffffff #define N 600 struct node { ...
- 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)
题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...
随机推荐
- ubuntu12.04硬盘安装
ubuntu12.04发布了 , 安装又是一个话题.安装系统有很多方法,比如livecd,和u盘,但这些都需借用外部设备,所以硬盘安装是最好不过的方法了.u盘,cd安装都非常的简 单,对于那些讨厌用光 ...
- 聊聊并发(六)——ConcurrentLinkedQueue的实现原理分析
1. 引言 在并发编程中我们有时候需要使用线程安全的队列.如果我们要实现一个线程安全的队列有两种实现方式:一种是使用阻塞算法,另一种是使用非阻塞算法.使用阻塞算法的队列可以用一个锁(入队和出队用同一把 ...
- hdu 4661 Message Passing(木DP&组合数学)
Message Passing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- [LeetCode179]Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- Eclipse热键
Eclipse编辑功能很强大.掌握Eclipse快捷功能.高开发效率.Eclipse中有例如以下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编辑的好帮手.能为用户提供 ...
- RH133读书笔记(6) - Lab 6 Adding New Filesystems to the Filesystem Tree
Lab 6 Adding New Filesystems to the Filesystem Tree Goal: Develop skills and knowlege related to par ...
- 移动端 transition动画函数的封装(仿Zepto)以及 requestAnimationFrame动画函数封装(仿jQuery)
移动端 css3 transition 动画 ,requestAnimationFrame 动画 对于性能的要求,h5优先考虑: 移动端 单页有时候 制作只用到简单的css3动画即可,我们封装一下, ...
- Nginx + IIS 配置,实现负载均衡
当你的Web应用程序访问量大的时候,一台服务器可能会因为压力过大而无法处理所有的请求.此时,可以增加服务器,采用负载均衡来分担所有的请求.关于Nginx的作用,自行百度了解.总之,在Windows平台 ...
- ASP.NET MVC常见问题解决方法
1.页面报错: The following errors occurred while attempting to load the app. - No assembly found containi ...
- Coco2d-x android win7 Python 游戏开发环境的搭建
1:我用的电脑配置 win7 3 核 内存8G 桌面.一直想学习Coco2d 游戏开发,所以,一个星期后,需要找到,最终建立了一个良好的环境 2:我使用的版本号版本号,至于建筑android开发环境略 ...