描述南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:
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 布线问题(并查集&&图论)的更多相关文章

  1. P1197 [JSOI2008]星球大战[并查集+图论]

    题目来源:洛谷 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球 ...

  2. HDU1878 欧拉回路---(并查集+图论性质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel

    有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...

  4. nyoj 711 枚举+并查集

     #include<stdio.h>//从大到小不断枚举边直到找到s-t的路径,判断从s可以到t可以用并查集来判断 #include<stdlib.h>//枚举最大的一条边肯定 ...

  5. nyoj 1022 合纵连横 (并查集<节点删除>)

    合纵连横 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...

  6. 并查集(图论) LA 3644 X-Plosives

    题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...

  7. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  8. nyoj 38 简单并查集的应用&最小生成树

    #include<stdio.h> #include<stdlib.h> #define inf 0x3fffffff #define N 600 struct node { ...

  9. 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)

    题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...

随机推荐

  1. 【原创】一个基于简单剪枝的DFS解数独程序

    问题来源:leetCode Sudoku Solver Write a program to solve aSudoku puzzle by filling the empty cells. Empt ...

  2. DM8168 CameraLink 视频噪点问题解决

    几天前写过FPGA採集LVDS视频(噪点去除),当时的情况是CameraLink视频出现了非常多噪点,不堪入目,硬件方面没有找到毛病,仅仅能是用硬件描写叙述语言对採集前端的FPGA又一次进行了处理,效 ...

  3. poj1163The Triangle(简单DP)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...

  4. c++爱问的面试问题

    1.static_cast,dynamic_cast,reinterpret_cast,const_cast四种转换. 2.const行为 3.malloc/free, new/delete差额 4. ...

  5. [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 55273   ...

  6. 基本介绍LINUX远程PC软件:PUTTY、SecureCRT、X-Manager

    ***********************************************声明************************************************ 原创 ...

  7. 升级到tomcat8时Artifact SpringMvcDemo:war exploded: Server is not connected. Deploy is not

    The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8 http://segmen ...

  8. 泛泰A860(高通公司8064 cpu 1080p) 拂4.4中国民营recovery TWRP2.7.1.2文本(通过刷第三版)

    专业第三方开发团队 VegaDevTeam  (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo crazyi(天下无雪 ...

  9. HDU 1541 Stars (树状数组)

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...

  10. RESTful架构详解(转)

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...