POJ 2075 Tangled in Cables (c ++/java), pojcables
Http://poj.org/problem? Id = 2075
Question:
Give you some people's names, and then give you n links to the houses owned by these people's names, and determine whether the cost of connecting these houses meets the requirements at the minimum cost.
Ideas:
When I entered the question for 20 minutes yesterday, I accidentally wrote the wrong question---| after watching the half-time Rest of the World Cup, I found out .... T
Use map to map the bottom object, and then find the 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. imports; import java. util. treeMap; public class Main {// final is equivalent to constpublic static final int MAXN = 500; // It is difficult to write 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) {// This cannot be written.? // 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; specified in = new partition (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 is. 1f }} class edge implements Comparable <edge> {int from, to; double val; public int compareTo (edge x) {// double is wrong once) bigDecimal data1 = new BigDecimal (this. val); BigDecimal data2 = new BigDecimal (x. val); return data1.compareTo (data2 );}}
An OJ with a minimal spanning tree, a simple basic algorithm question, ZOJ 2326 Tangled in Cables. I am enlightened by prawns and solved the problem of adding the value to 80 points.
Open the array to a greater value.