[BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

1626: [Usaco2007 Dec]Building Roads 修建道路

Time Limit: 5 Sec??Memory Limit: 64 MB Submit: 1730??Solved: 727 [Submit][Status][Discuss]

Description

Farmer John最近得到了一些新的農場,他想新修一些道路使得他的所有農場可以經過原有的或是新修的道路互達(也就是說,從任一個農場都可以經過一些首尾相連道路到達剩下的所有農場)。有些農場之間原本就有道路相連。 所有N(1 <= N <= 1,000)個農場(用1..N順次編號)在地圖上都表示為坐標為(X_i, Y_i)的點(0 <= X_i <= 1,000,000;0 <= Y_i <= 1,000,000),兩個農場間道路的長度自然就是代表它們的點之間的距離。現在Farmer John也告訴了你農場間原有的M(1 <= M <= 1,000)條路分別連接了哪兩個農場,他希望你計算一下,為了使得所有農場連通,他所需建造道路的最小總長是多少。

Input

* 第1行: 2個用空格隔開的整數:N 和 M

?* 第2..N+1行: 第i+1行為2個用空格隔開的整數:X_i、Y_i * 第N+2..N+M+2行: 每行用2個以空格隔開的整數i、j描述了一條已有的道路, 這條道路連接了農場i和農場j

Output

* 第1行: 輸出使所有農場連通所需建設道路的最小總長,保留2位小數,不必做 任何額外的取整操作。為了避免精度誤差,計算農場間距離及答案時 請使用64位實型變量

Sample Input

4 1
1 1
3 1
2 3
4 3
1 4

輸入說明:

??? FJ一共有4個坐標分別為(1,1),(3,1),(2,3),(4,3)的農場。農場1和農場
4之間原本就有道路相連。


Sample Output

4.00

輸出說明:

??? FJ選擇在農場1和農場2間建一條長度為2.00的道路,在農場3和農場4間建一
條長度為2.00的道路。這樣,所建道路的總長為4.00,并且這是所有方案中道路
總長最小的一種。
已經有的邊權值賦為0,做最小生成樹
#include <cmath> 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char buf[10000000], *ptr = buf - 1;
inline int readint(){int n = 0;char ch = *++ptr;while(ch < '0' || ch > '9') ch = *++ptr;while(ch <= '9' && ch >='0'){n = (n << 1) + (n << 3) + ch - '0';ch = *++ptr;}return n;
}
typedef long long ll;
const int maxn = 1000 + 10, maxm = 500000 + 1000 + 10;
struct Edge{int u, v;double w;Edge(){}Edge(int _u, int _v, double _w): u(_u), v(_v), w(_w){}bool operator < (const Edge &x) const {return w < x.w;}
}e[maxm];
int cnt = 0;
inline void add(int u, int v, double w){e[++cnt] = Edge(u, v, w);
}
inline ll sqr(int x){return (ll)x * x;
}
int x[maxn], y[maxn];
inline double dis(int a, int b){return sqrt(sqr(x[a] - x[b]) + sqr(y[a] - y[b]));
}
int fa[maxn];
int Find(int a){return a == fa[a] ? a : fa[a] = Find(fa[a]);
}
int main(){fread(buf, sizeof(char), sizeof(buf), stdin);int n, m;n = readint();m = readint();for(int i = 1; i <= n; i++){x[i] = readint();y[i] = readint();}for(int i = 1; i <= n; i++)for(int j = 1; j < i; j++)add(i, j, dis(i, j));for(int u, v, i = 1; i <= m; i++){u = readint();v = readint();add(u, v, 0);}sort(e + 1, e + cnt + 1);for(int i = 1; i <= n; i++) fa[i] = i; double ans = 0.0;for(int i = 1, k = 0; i <= cnt && k < n - 1; i++){if(Find(e[i].v) == Find(e[i].u)) continue;fa[Find(e[i].v)] = Find(e[i].u);k++;ans += e[i].w;}printf("%.2lf\n", ans);return 0;
}

?

轉載于:https://www.cnblogs.com/ruoruoruo/p/7481884.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391434.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391434.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391434.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

雙城記s001_雙城記! (使用數據講故事)

雙城記s001Keywords: Data science, Machine learning, Python, Web scraping, Foursquare關鍵字&#xff1a;數據科學&#xff0c;機器學習&#xff0c;Python&#xff0c;Web抓取&#xff0c;Foursquare https://br.pinterest.com/pin/92816442292506979/https://br.pintere…

python:linux中升級python版本

https://www.cnblogs.com/gne-hwz/p/8586430.html 轉載于:https://www.cnblogs.com/gcgc/p/11446403.html

web前端面試總結

2019獨角獸企業重金招聘Python工程師標準>>> 摘要&#xff1a;前端的東西特別多&#xff0c;面試的時候我們如何從容應對&#xff0c;作為一個老兵&#xff0c;我在這里分享幾點我的經驗。 一、javascript 基礎(es5) 1、原型&#xff1a;這里可以談很多&#xff0c;…

783. 二叉搜索樹節點最小距離(dfs)

給你一個二叉搜索樹的根節點 root &#xff0c;返回 樹中任意兩不同節點值之間的最小差值 。 注意&#xff1a;本題與 530&#xff1a;https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/ 相同 示例 1&#xff1a; 輸入&#xff1a;root [4,2,6,1,3] 輸…

linux epoll機制對TCP 客戶端和服務端的監聽C代碼通用框架實現

1 TCP簡介 tcp是一種基于流的應用層協議&#xff0c;其“可靠的數據傳輸”實現的原理就是&#xff0c;“擁塞控制”的滑動窗口機制&#xff0c;該機制包含的算法主要有“慢啟動”&#xff0c;“擁塞避免”&#xff0c;“快速重傳”。 2 TCP socket建立和epoll監聽實現 數據結構…

linux中安裝robot環境

https://www.cnblogs.com/lgqboke/p/8252488.html&#xff08;文中驗證robotframework命令應該為 robot --version&#xff09; 可能遇到的問題&#xff1a; 1、python版本太低 解決&#xff1a;升級python https://www.cnblogs.com/huaxingtianxia/p/7986734.html 2、pip安裝報…

angular 模塊構建_我如何在Angular 4和Magento上構建人力資源門戶

angular 模塊構建Sometimes trying a new technology mashup works wonders. Both Magento 2 Angular 4 are very commonly talked about, and many consider them to be the future of the development industry. 有時嘗試新技術的mashup會產生奇跡。 Magento 2 Angular 4都…

tableau破解方法_使用Tableau瀏覽Netflix內容的簡單方法

tableau破解方法Are you struggling to perform EDA with R and Python?? Here is an easy way to do exploratory data analysis using Tableau.您是否正在努力使用R和Python執行EDA&#xff1f; 這是使用Tableau進行探索性數據分析的簡單方法。 Lets Dive in to know the …

六周第三次課

2019獨角獸企業重金招聘Python工程師標準>>> 六周第三次課 9.6/9.7 awk awk也是流式編輯器&#xff0c;針對文檔中的行來操作&#xff0c;一行一行地執行。 awk比sed更強大的功能是它支持了分段。 -F選項的作用是指定分隔符&#xff0c;如果不加-F選項&#xff0c;…

面試題字符集和編碼區別_您和理想工作之間的一件事-編碼面試!

面試題字符集和編碼區別A recruiter calls you for a position with your dream company. You get extremely excited and ask about their recruiting process. He replies saying “Its nothing big, you will have 5 coding rounds with our senior tech team, just the sta…

初探Golang(1)-變量

要學習golang&#xff0c;當然要先配置好相關環境啦。 1. Go 安裝包下載 https://studygolang.com/dl 在Windows下&#xff0c;直接下載msi文件&#xff0c;在安裝界面選擇安裝路徑&#xff0c;然后一直下一步就行了。 在cmd下輸入 go version即可看到go安裝成功 2. Golan…

macaca web(4)

米西米西滴&#xff0c;吃過中午飯來一篇&#xff0c;話說&#xff0c;上回書說道macaca 測試web&#xff08;3&#xff09;&#xff0c;參數驅動來搞&#xff0c;那么有小伙本又來給雷子來需求&#xff0c; 登錄模塊能不能給我給重新封裝一下嗎&#xff0c; 我說干嘛封裝&…

linux中安裝cx_Oracle

https://blog.csdn.net/w657395940/article/details/41144225 各種嘗試都&#xff0c;最后 pip install cx-Oracle 成功導入 轉載于:https://www.cnblogs.com/gcgc/p/11447583.html

rfm模型分析與客戶細分_如何使用基于RFM的細分來確定最佳客戶

rfm模型分析與客戶細分With some free time at hand in the midst of COVID-19 pandemic, I decided to do pro bono consulting work. I was helping a few e-commerce companies with analyzing their customer data. A common theme I encountered during this work was tha…

leetcode 208. 實現 Trie (前綴樹)

Trie&#xff08;發音類似 “try”&#xff09;或者說 前綴樹 是一種樹形數據結構&#xff0c;用于高效地存儲和檢索字符串數據集中的鍵。這一數據結構有相當多的應用情景&#xff0c;例如自動補完和拼寫檢查。 請你實現 Trie 類&#xff1a; Trie() 初始化前綴樹對象。 void…

那些年收藏的技術文章(一) CSDN篇

#Android ##Android基礎及相關機制 Android Context 上下文 你必須知道的一切 Android中子線程真的不能更新UI嗎&#xff1f; Android基礎和運行機制 Android任務和返回棧完全解析&#xff0c;細數那些你所不知道的細節 【凱子哥帶你學Framework】Activity啟動過程全解析 【凱子…

chrome json插件_如何使用此免費的Chrome擴展程序(或Firefox插件)獲取易于閱讀的JSON樹

chrome json插件JSON is a very popular file format. Sometimes we may have a JSON object inside a browser tab that we need to read and this can be difficult.JSON是一種非常流行的文件格式。 有時我們可能需要在瀏覽器選項卡中包含一個JSON對象&#xff0c;這很困難。…

test10

test10 轉載于:https://www.cnblogs.com/Forever77/p/11447638.html

數據倉庫項目分析_數據分析項目:倉庫庫存

數據倉庫項目分析The code for this project can be found at my GitHub.該項目的代碼可以在我的GitHub上找到 。 介紹 (Introduction) The goal of this project was to analyse historic stock/inventory data to decide how much stock of each item a retailer should hol…

leetcode 213. 打家劫舍 II(dp)

你是一個專業的小偷&#xff0c;計劃偷竊沿街的房屋&#xff0c;每間房內都藏有一定的現金。這個地方所有的房屋都 圍成一圈 &#xff0c;這意味著第一個房屋和最后一個房屋是緊挨著的。同時&#xff0c;相鄰的房屋裝有相互連通的防盜系統&#xff0c;如果兩間相鄰的房屋在同一…