POJ 2075 Tangled in Cables (c++/java)
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)的更多相关文章
- POJ 2075 Tangled in Cables 最小生成树
简单的最小生成树,不过中间却弄了很久,究其原因,主要是第一次做生成树,很多细节不够熟练,find()函数的循环for判断条件是 pre[i]>=0,也就是遇到pre[i]==-1时停止,i就是并 ...
- [POJ 1001] Exponentiation C++解题报告 JAVA解题报告
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 126980 Accepted: 30 ...
- POJ 2075
#include<iostream> #include<stdio.h> #include<string> #include<map> #include ...
- Tangled in Cables(Kruskal+map容器处理字符串)
/** 题意: 给你两个城市之间的道路(无向图),求出需要的 电缆.如果大于所提供的,就输出Not enough ... 否则输出所需要的电缆长度. 输入:N (给 ...
- TOJ 2119 Tangled in Cables
描述 You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfor ...
- 图论常用算法之一 POJ图论题集【转载】
POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...
- POJ 1503 Integer Inquiry 大数 难度:0
题目链接:http://poj.org/problem?id=1503 import java.io.*; import java.math.BigInteger; import java.util. ...
- poj 1129 Channel Allocation
http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...
- ACM java写法入门
打2017icpc沈阳站的时候遇到了大数的运算,发现java与c++比起来真的很赖皮,竟然还有大数运算的函数,为了以后打比赛更快的写出大数的算法并且保证不错,特意在此写一篇博客, 记录java的大数运 ...
随机推荐
- SGU 186.The Chain
看懂题就是水题... code #include <iostream> #include <algorithm> using namespace std; int a[110] ...
- modifytime是一个神奇的column name----这边文章是错的totally,因为我的实验不彻底。timestamp属性很神奇,头一个timestamp,会自动的成DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
在mysql里边modifytime是一个神奇的column name,试一下. 请执行sql语句 CREATE TABLE `test_time` ( `modifytime` timestamp ...
- [翻译]jQuery十周年-John Resig
10th Anniversary of jQuery Today marks the 10th anniversary of the release of jQuery...[原文] 今天是jQuer ...
- myisam和innodb区别
InnoDB MyIsam 事务 支持 不支持 锁 行锁 表锁 索引 B+树,数据和索引在一个文件中,必须有主键,如果不指定,会自动生成一个隐藏字段作 ...
- div浮动框居于浏览器窗口中间
代码先贴这里,随后再改 <script language="JavaScript"> document.getElementById('divCenter').styl ...
- jQuery简单的轮播特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Http和Socket连接的区别
Http和Socket连接区别 相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过自己的浅显理解能对初学者有所帮助. 1.TCP连接 要想明白Socket连接,先 ...
- 造成session丢失的原因和解决方法
win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程, 造成保存在该进程中的session丢失. 因为Session,Appl ...
- Java中异常处理和设计
在程序设计中,进行异常处理是非常关键和重要的一部分.一个程序的异常处理框架的好坏直接影响到整个项目的代码质量以及后期维护成本和难度.试想一下,如果一个项目从头到尾没有考虑过异常处理,当程序出错从哪里寻 ...
- www.nt-kaisheng.com
线号机耗材网站开发架构,是基于丽标线号机_凯标线号机_耗材|色带|号码管批发|电缆标牌_南通凯胜电器有限公司,进行的服务需求的网站. 南通凯胜电器有限公司网站与手工编码比起来,HTML5框架在准确性和 ...