日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

POJ 2075 Tangled in Cables (c++/java)

發布時間:2023/12/20 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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");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 相當于constpublic 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;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; //sortArrays.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) ; } }


轉載于:https://www.cnblogs.com/murmured/p/5004026.html

總結

以上是生活随笔為你收集整理的POJ 2075 Tangled in Cables (c++/java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。