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");
else
printf("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 相当于const
public 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;
else
return 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; //sort
Arrays.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) ;
}
}

POJ 2075 Tangled in Cables (c++/java)的更多相关文章

  1. POJ 2075 Tangled in Cables 最小生成树

    简单的最小生成树,不过中间却弄了很久,究其原因,主要是第一次做生成树,很多细节不够熟练,find()函数的循环for判断条件是 pre[i]>=0,也就是遇到pre[i]==-1时停止,i就是并 ...

  2. [POJ 1001] Exponentiation C++解题报告 JAVA解题报告

        Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 126980   Accepted: 30 ...

  3. POJ 2075

    #include<iostream> #include<stdio.h> #include<string> #include<map> #include ...

  4. Tangled in Cables(Kruskal+map容器处理字符串)

    /** 题意:     给你两个城市之间的道路(无向图),求出需要的     电缆.如果大于所提供的,就输出Not enough ...     否则输出所需要的电缆长度.       输入:N (给 ...

  5. TOJ 2119 Tangled in Cables

    描述 You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfor ...

  6. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

  7. POJ 1503 Integer Inquiry 大数 难度:0

    题目链接:http://poj.org/problem?id=1503 import java.io.*; import java.math.BigInteger; import java.util. ...

  8. poj 1129 Channel Allocation

    http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...

  9. ACM java写法入门

    打2017icpc沈阳站的时候遇到了大数的运算,发现java与c++比起来真的很赖皮,竟然还有大数运算的函数,为了以后打比赛更快的写出大数的算法并且保证不错,特意在此写一篇博客, 记录java的大数运 ...

随机推荐

  1. Linux嘚瑟一时的Shared Object

    场景概述 近来接触node程序以及负责实现node扩展来对象本地SDK的调用,旨在借node及其第三方库来快速实现RESTful API以及给浏览器端使用.当然这中间研究工作耗了不少时间. 在实现目标 ...

  2. 读书笔记之 - javascript 设计模式 - 组合模式

    组合模式是一种专为创建Web上的动态用户界面而量身定制的模式,使用这种模式,可以用一条命令在对各对象上激发复杂的或递归的行为. 在组合对象的层次体系中有俩种类型对象:叶对象和组合对象.这是一个递归定义 ...

  3. URPF 简单流程

    主要功能是防止基于源地址欺骗的网络攻击. 路由器接口一旦使能URPF功能,当该接口收到数据报文时,首先会对数据报文的源地址进行合法性检查,对于源地址合法性检查通过的报文,才会进一步查找去往目的地址的转 ...

  4. js实现placeholder效果

    <form name="testForm" action="" method=""> <input type=" ...

  5. dedecms 文章排列方式

    orderby='sortrank' 文档排序方式orderby='hot' 或 orderby='click' 表示按点击数排列orderby='sortrank' 或 orderby='pubda ...

  6. Android扫描二维码 实现 登录网页

    工程代码:ScanQRcode.zip ------------------------------------------------------------------ 1. 扫描二维码登录的实现 ...

  7. 用python选择及显示三级目录,可返回上层目录以及随时跳出。

    # -*- coding: utf-8 -*-"""Created on Fri Jul 29 09:43:38 2016 @author: yinggang" ...

  8. C语言陷阱——类型转换

    以下例子取自<深入理解计算机系统>. 考虑如下的C语言代码: #include<stdio.h> typedef unsigned char* byte_pointer; vo ...

  9. MySQL存储过程实例

    一.创建MySQL数据库函数 TCC:无参数,查询fruit表中的所有数据 : TAA:两个参数,查询fruit总共有多少行:查询ids为某个值时水果表的数据 TDD:两个参数,查询ids不等于某个值 ...

  10. Node.js回调概念

    什么是回调? 回调是一个异步等效的功能.在完成特定任务回调函数被调用. Node大量使用了回调.Node的所有的API都支持回调这样的一种方式. 例如,一个函数读取一个文件可能开始读取文件,并使得下一 ...