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. CSS3中动画transform必须要了解的Skew变化原理

    transform是CSS3中比较安全的动画(对于性能来说),其中有一些动画属性,来执行不同的变化.今天我们来了解skew的变化原理. skew,其实使用的频率不是很高,当然也没有最低.但是往往,一直 ...

  2. 详解函数声明VS函数表达式

    函数声明 比方如下:1.我们以一个完整的语句以function开头,不加任何东西. 2.有一个函数名(add) 3.参数可带可不带(x,y) 4.有一个数体 满足以上要求的我们统称为函数声明! 附加小 ...

  3. AspNet MVC : 操作/控制器过滤器(action filter)

    1.Action Filter Action Filter提供了在执行action/controller前后对请求/响应修改的能力,可以应用于action和控制器上,作用在控制器上将自动被应用到该控制 ...

  4. sql2000

    sql 2000简体中文企业版下载(含SP3 SP4 下载地址)安装图解及sp4安装教程图解 2012-07-17 16:24:37|  分类: mssql数据库|字号 订阅     sql 2000 ...

  5. python模块之paramiko

              46.python模块之paramiko   SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  6. IOS设计模式之三:MVC模式

    IOS设计模式之三:MVC模式   模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...

  7. ConnectifyHotspotInstaller3.7注册码

    ConnectifyHotspotInstaller3.7注册码 邮箱: wcxu21@126.com 密 钥:QEX4HR-9VLS2N-AXFA3A-K8CD8M-1DQ7Q1-CCDDTB-KL ...

  8. iOS手写2048--基于Xcode7.1

    闲着没事自己想了下,半天写出来了,没有美化,只是实现了基本的2048,被我改成了A.B.C.D.E: 没有游戏开发经验,完全基于uiview 和 一大堆逻辑计算,如果你有指针.链表的使用经验,应该会很 ...

  9. Spring: DispacherServlet和ContextLoaderListener中的WebApplicationContext的关系

    在Web容器(比如Tomcat)中配置Spring时,你可能已经司空见惯于web.xml文件中的以下配置代码: <context-param> <param-name>cont ...

  10. [转]Android DPAD not enabled in AVD

     转自:http://blog.csdn.net/flyhigh200703/article/details/8955484 问题描述:打开Android的仿真器,右侧的按键部分对于上下左右键出现以下 ...