http://poj.org/problem?id=2075
Main topic:
Give you some names, and then give you a way to connect the houses owned by these names, asking for the minimum cost of connecting the houses to meet the requirements.
Ideas:
Yesterday 20 minutes of the question, input accidentally wrote wrong--| | | | | Look at the World Cup half-rest and find out .... T T
Map the subscript with a map and then ask for the MST to do so.
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 ED ge& 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);d ouble ans = 0;for (int i = 0; i<len; i++) {int from = E[i].fro M;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.Scanner;import Java.util.treemap;public class Main {//final equivalent to constpublic static final int maxn=500;//write not accustomed to public static int[] Fa=new INT[MAXN];p ublic static treemap<string, integer> m=new treemap<string, integer> ();p ublic static edge[] E= New EDGE[MAXN*MAXN];p ublic static int find (int cur) {//Can't write like this? 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 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 is. 1f}}class Edge implements comparable<edge> {int from,to;double val;public int compareTo (Edge x) { Double is the wrong one) BigDecimal data1 = new BigDecimal (this.val); BigDecimal data2 = new BigDecimal (x.val); Return Data1.compareto (DATA2); } }