2075:Tangled in Cables

  • 查看
  • 提交
  • 统计
  • 提示
  • 提问
总时间限制: 
1000ms 
内存限制: 
65536kB
描述
You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start your business properly and are relying on parts you have found in an old warehouse you bought. Among your finds is a single spool of cable and a lot of connectors. You want to figure out whether you have enough cable to connect every house in town. You have a map of town with the distances for all the paths you may use to run your cable between the houses. You want to calculate the shortest length of cable you must have to connect all of the houses together.
输入
Only one town will be given in an input.

  • The first line gives the length of cable on the spool as a real number.
  • The second line contains the number of houses, N
  • The next N lines give the name of each house's owner. Each name consists of up to 20 characters {a–z,A–Z,0–9} and contains no whitespace or punctuation.
  • Next line: M, number of paths between houses
  • next M lines in the form

< house name A > < house name B > < distance >
Where the two house names match two different names in the list above and the distance is a positive real number. There will not be two paths between the same pair of houses.

输出
The output will consist of a single line. If there is not enough cable to connect all of the houses in the town, output
Not enough cable
If there is enough cable, then output
Need < X > miles of cable
Print X to the nearest tenth of a mile (0.1).
样例输入
100.0
4
Jones
Smiths
Howards
Wangs
5
Jones Smiths 2.0
Jones Howards 4.2
Jones Wangs 6.7
Howards Wangs 4.0
Smiths Wangs 10.0
样例输出
Need 10.2 miles of cable
来源
Mid-Atlantic 2004
解析:题目大意是给定一些人和他们的家间需要的cable,问是否有足够的cable连接所有的house,把house当作点,需要的cable为边上的权值,求最小生成树即可。
代码:
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
map<string,int> gp;
struct Edge{int from,to;double cost;Edge(int f,int t,double c):from(f),to(t),cost(c){}bool operator < (const Edge & o)const{return cost < o.cost;}
};
int fa[1002];
int find(int x){if(fa[x] == -1) return x;return fa[x] = find(fa[x]);
}
vector<Edge> graph;int main(){double cable,co,total = 0; int n,m,eNum = 0; string str,str1;scanf("%lf",&cable);scanf("%d",&n);for(int i = 0; i < n; ++i){cin>>str;gp[str] = i;}cin>>m;for(int i = 0; i < m;i++){cin>>str>>str1>>co;graph.push_back(Edge(gp[str],gp[str1],co));}sort(graph.begin(),graph.end());memset(fa,-1,sizeof(fa));for(size_t i = 0; i < graph.size();i++){Edge e = graph[i];if(find(e.from) != find(e.to)){total += e.cost;eNum++;fa[find(e.from)] = find(e.to);if(eNum == n - 1) break;}}if(total > cable)puts("Not enough cable");else printf("Need %.01lf miles of cable\n",total);return 0;
}

百练2075:Tangled in Cables题解相关推荐

  1. 【最小生成树专题】POJ 2075 Tangled in Cables

    POJ 2075 Tangled in Cables 题目链接->http://poj.org/problem?id=2075 Time Limit: 1000MS   Memory Limit ...

  2. POJ 2075 Tangled in Cables (c++/java)

    http://poj.org/problem?id=2075 题目大意: 给你一些人名,然后给你n条连接这些人名所拥有的房子的路,求用最小的代价求连接这些房子的花费是否满足要求. 思路: 昨天20分钟 ...

  3. POJ 2075 Tangled in Cables

    也比较水:Hash+最小生成树,我用的是Prim:好的是我有复习了一下Hash View Code #include <stdio.h>#include <string.h># ...

  4. 百练/ 北京大学2016研究生推免上机考试(校外)G: Tangled in Cables(最小支撑树)

    题目来源:http://bailian.openjudge.cn/practice/2075/ 2075:Tangled in Cables 总时间限制:1000ms  内存限制: 65536kB 描 ...

  5. 程序设计入门经典题解(百练篇)

    参考链接:PKU百练题解(Bailian) Bailian1017 装箱问题[贪心] - 海岛Blog - CSDN博客 POJ1088 Bailian1088 滑雪[DFS+记忆化搜索]_海岛Blo ...

  6. 百练(十三~十六)题解

    百练(十三) Bailian2806 公共子序列[最长公共子序列+DP] - 海岛Blog - CSDN博客 Bailian3143 验证"歌德巴赫猜想"[筛选法]_海岛Blog- ...

  7. 百练(九~十二)题解

    百练(九) Bailian2801 填词 POJ1629 ZOJ1546 Fillword[排序] - 海岛Blog - CSDN博客 POJ1088 Bailian1088 滑雪[DFS+记忆化搜索 ...

  8. 钓鱼问题(百练1042)

    ★问题描述: 约翰正在钓鱼.他有h个小时可用(1 <= h <= 16),并且该区域有n个湖泊(2 <= n <= 25),沿着一条单向公路可达.约翰从1号湖开始,但他可以在任 ...

  9. 百练,4103,踩方格

    百练,4103,踩方格 普通做法:(也可以找规律) #include #include//要调用memset函数,头文件 using namespace std; int visited[50][50 ...

最新文章

  1. Leetcode: Sort List
  2. python处理流程-分析Python的Django框架的运行方式及处理流程
  3. 鸭子在Java中打字? 好吧,不完全是
  4. 规模比互联网大 30 倍的物联网,如何入门?
  5. Java学习资料汇总
  6. 天翼云搭建socks5和搭建http
  7. 大创人人有,我怎么才能拿到国家级?
  8. 一文读懂|什么是dToF激光雷达技术?
  9. 爬取东方求闻史记和东方求闻口授图片
  10. 第一代程序员作家--王小波
  11. android百度定位方式,Android 百度定位SDK
  12. HIVE:窗口函数,用sql语句查询MySQL安装路径和版本
  13. 什么是jdk,jre?
  14. 飞龙:蒙语“牵手”人工智能的拓荒者
  15. Chrome浏览器播放HTML5音频没声音的解决方案
  16. UE4蓝图 传送带效果
  17. Nexus基本配置与使用
  18. 【mindspore训练】modelzoo-resnet50模型训练后验证
  19. rest_framework基础
  20. 解决helix.toolkit obj模型贴图查找失败导致的异常

热门文章

  1. 网吧XP系统服务优化全面解析
  2. 微信公众号开发教程[012]-素材管理
  3. 计算机操作员评分标准,计算机操作员初级操作技能考核评分记录表
  4. python如何将表格数据转换成txt文件
  5. ajax获取后台图片数据流如何处理?
  6. 炫酷JavaScript转盘时钟动态js特效
  7. NS-3上下行链路丢包仿真
  8. R与RStudio安装与学习(一)
  9. golang 获取客户端真实IP地址
  10. 服务器接硬盘的那个叫什么名字,服务器硬盘接口和普通硬盘接口区别是什么?...