http://poj.org/problem?id=2075

题目大意:

给你一些人名,然后给你n条连接这些人名所拥有的房子的路,求用最小的代价求连接这些房子的花费是否满足要求。

思路:

昨天20分钟的题,输入不小心写错了- -|||||看世界杯半场休息随便看了下发现了。。。。T T

用map进行下标的映射,然后求MST即可。

c++

#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAXN = 500;
int fa[MAXN];
struct edge
{int from, to;double val;bool operator < (const edge& x)const{return val < x.val;}
}e[MAXN*MAXN];
map<string, int> m;int find(int cur)
{return cur == fa[cur] ? cur : fa[cur] = find(fa[cur]);
}int main()
{int len = 0, n;double a;cin >> a >> n;while (n--){string temp;cin >> temp;m[temp] = len++;}cin >> n;for (len = 0; len<n; len++){string from, to;double value;cin >> from >> to >> value;e[len].from = m[from];e[len].to = m[to];e[len].val = value;}for (int i = 0; i<len; i++)fa[i] = i;sort(e, e + len);double ans = 0;for (int i = 0; i<len; i++){int from = e[i].from;int to = e[i].to;int root_x = find(from);int root_y = find(to);if (root_x == root_y) continue;fa[root_x] = root_y;ans += e[i].val;}if (ans >  a)printf("Not enough cable\n");elseprintf("Need %.1lf miles of cable\n", ans);return 0;
}

JAVA:

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;public class Main {//final 相当于constpublic static final int MAXN=500;//写起来好不习惯public static int[] fa=new int[MAXN];public static TreeMap<String, Integer> m=new TreeMap<String, Integer>();public static edge[] e=new edge[MAXN*MAXN];public static int find(int cur){//不能这么写?//return cur == fa[cur] ? cur : fa[cur] = find(fa[cur]);  if(cur==fa[cur])return cur;elsereturn fa[cur] = find(fa[cur]); }  public static void main(String[] args) {int len = 0, n;  double a;  Scanner in=new Scanner(System.in);a=in.nextDouble();n=in.nextInt();while((n--)!=0){String temp=in.next();m.put(temp, new Integer(len++));         }n=in.nextInt();double value;  for (len = 0; len<n; len++)  {  String from=in.next();String to=in.next();value=in.nextDouble();e[len]=new edge();e[len].from = m.get(from);  e[len].to = m.get(to);  e[len].val = value;  }  for (int i = 0; i<len; i++)  fa[i] = i;  //sortArrays.sort(e,0,len);   double ans=0;for(int i=0;i<len;i++){int from = e[i].from;  int to = e[i].to;  int root_x = find(from);  int root_y = find(to);  if (root_x == root_y) continue;fa[root_x] = root_y;  ans += e[i].val;  }if (ans >  a)  System.out.print("Not enough cable\n"); else   System.out.printf("Need %.1f miles of cable\n", ans);    //java 是.1f}}class edge implements  Comparable<edge>
{int from,to;double val;public int compareTo(edge x)  {          //double比较错了一次)BigDecimal data1 = new BigDecimal(this.val); BigDecimal data2 = new BigDecimal(x.val); return data1.compareTo(data2) ;      }
}

转载于:https://www.cnblogs.com/murmured/p/5004026.html

POJ 2075 Tangled in Cables (c++/java)相关推荐

  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

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

  3. 百练2075:Tangled in Cables题解

    2075:Tangled in Cables 查看 提交 统计 提示 提问 总时间限制:  1000ms  内存限制:  65536kB 描述 You are the owner of SmallCa ...

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

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

  5. POJ 1001 Exponentiation C++解题报告 JAVA解题报告

    求高精度幂 Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 126980   Accepted: 30980 Descripti ...

  6. POJ 1001 Exponentiation (记第一道Java水过的题)

    妹的!java些高精度相当与玩cs开外挂!电脑装上java很久了,一直只会写写a + b 玩,今天心血来潮找个高精度来玩玩.^ ^ import java.io.*; import java.util ...

  7. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

  8. POJ 超详细分类

    POJ 各题算法 1000    A+B Problem            送分题     49%    2005-5-7 1001    Exponentiation         高精度   ...

  9. POJ 图论分类 + DP(较全 自己又加了点)

    DP -----------动态规划 状态压缩DP 2411 (棋盘规模较大)状态压缩DP+DFS+滚动数组 2664 (棋盘规模较小)直接递推即可(DP) 2506 (棋盘规模较小)直接递推即可(D ...

最新文章

  1. jquery中$(document).ready(function(){//todo});window.onload时间线关系
  2. 30岁就退休,你也可以做到
  3. paper reading: roi 区域对人脸测试心率有用
  4. 多线程+链表模拟RR调度算法
  5. Allegro16.6导出位号图
  6. 解决办法:C代码中明明有,为什么编译时提示未定义的引用
  7. 红色警戒 direct错误
  8. vue使用a标签下载文件_vue+iview 通过a标签实现文件下载
  9. 数字科技陪伴企业成长|突破封锁,庚顿数据助力中国名牌全球瞩目
  10. 行业案例 | AR+工业互联网,智慧园区的全新服务模式
  11. 2021年数维杯数学建模分析和思路——C题
  12. VTK-医学三维图像四视图显示以及鼠标滑轮控制切片交互
  13. 研究生如何做好科研和发表文章
  14. MIPI介绍(CSI DSI接口)
  15. 基于Flexlive.CQP.Framework的C# 酷Q UDP实现
  16. 《嵌入式系统 - RT-Thread开发笔记》手把手教你使用RT-Thread制作GD32 RISC-V系列BSP
  17. 五彩斑斓的 Black —— Python代码格式化工具
  18. MYSQL补充SQL语句
  19. Kafka启动失败异常-InconsistentClusterIdException
  20. javascript可以直接比较时间字符串的大小

热门文章

  1. Oracle学习笔记:通过种子数据库设置dbid为指定值
  2. WIN2003设完C盘权限后终端连不上。
  3. 读《程序员的SQL金典》[2]--函数
  4. (翻译)Tricks of the windows game programming Gurus(Windows游戏大师之路) --- 前言(作者:ANDRE LAMOTHE 1999)...
  5. AutoCAD VBA天圆地方的放样展开图
  6. 手机吞吃蛇游戏的设计与开发
  7. IBM DS300 安装
  8. C语言中临时变量写在哪里,C语言中不允许创建临时变量,交换两个数的内容
  9. php高等数学,中国大学《高等数学(四)》期末答案高校邦《PHP语言程序设计》见面课答案...
  10. 【大数据】最新大数据学习路线(完整详细版,含整套教程)