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),布布扣,bubuko.com

时间: 2024-12-14 04:47:50

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

POJ 2075 Tangled in Cables (kruskal算法 MST + map)

Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6039   Accepted: 2386 Description You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start

poj 2075 Tangled in Cables

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 6 Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for

POJ 2075:Tangled in Cables 【Prim】

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 1 Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for

poj 2757 : 最长上升子序列(JAVA)

总时间限制:  2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N.比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等.这

POJ 1001 Exponentiation(大数幂,还是Java大发好!需调用多个方法)

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156303   Accepted: 38063 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the n

poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted: 1843 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a a

POJ 2413 How many Fibs? (java大数)

How many Fibs? Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := f n-1 + f n-2 (n>=3) Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b]. Input The input contains several test cases.

POJ 2075

#include<iostream> #include<stdio.h> #include<string> #include<map> #include<iomanip> #define MAXN 500 #define inf 1000000000 typedef double elem_t; using namespace std; double _m[MAXN][MAXN]; int pre[MAXN]; map<string,int

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

/** 题意: 给你两个城市之间的道路(无向图),求出需要的 电缆.如果大于所提供的,就输出Not enough ... 否则输出所需要的电缆长度. 输入:N (给定的电缆总长度) m1 (有多少个城市—) str1 str2 str3 str4 : :(城市的名字) m2(相当于给出m2条边) a  b  c (a城市到b城市的距离为c) : : : : 输出: 所需的最短长度   若大于给定的长度  输出 Not enough cable 分析: 简单的最小生成树+字符串处理 用map容器映