01分數規劃+prim POJ2728 Desert King

来源:http://www.cnblogs.com/sdfzxh/archive/2017/07/18/7203439.html
-Advertisement-
Play Games

Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26009 Accepted: 7219 Description David the Great has just become the king of a ...


Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 26009   Accepted: 7219

Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way.

After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital.

His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line.

As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.

Input

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

Sample Input

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

Sample Output

1.000

Source

Beijing 2005       題意:有n個村莊,村莊在不同坐標和海拔,現在要對所有村莊供水,只要兩個村莊之間有一條路即可,建造水管距離為坐標之間的歐幾里德距離,費用為海拔之差,現在要求方案使得費用與距離的比值最小,即求一棵最優比率生成樹。 prim求稠密圖的效率確實很好……  
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath> 
 6 using namespace std;
 7 const double acc=1e-7;
 8 const double inf=1e15;
 9 int n;
10 struct data{
11     double x,y,z;
12 }node[1010];
13 double l,r,mid;
14 double dis[1010][1010],h[1010][1010],w[1010];
15 bool check[1010];
16 double ds(double x1,double y1,double x2,double y2){
17     return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
18 }
19 double prim(double c){//coefficient-繫數 
20     double ret=0.0;
21     for(int i=1;i<=n;i++) w[i]=inf;//double 賦初值 
22     memset(check,0,sizeof(check));
23     w[1]=0.0;
24     for(int i=1;i<=n;i++){
25         double mn=inf;
26         int k;
27         for(int j = 1;j<=n;j++)
28             if(!check[j]&&w[j]<mn) mn=w[j],k=j;
29         check[k]=1;
30         ret+=mn;
31         for(int j=1;j<=n;j++)
32             if(!check[j]&&w[j]>h[k][j]-c*dis[k][j]) 
33                 w[j]=h[k][j]-c*dis[k][j];
34     }
35     return ret;
36 }
37 int main(){
38     while(scanf("%d",&n)){
39         if(!n) return 0;
40         for(int i=1;i<=n;i++){
41             scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z);
42             for(int j=1;j<=i;j++){
43                 dis[i][j]=dis[j][i]=ds(node[i].x,node[i].y,node[j].x,node[j].y);
44                 h[i][j]=h[j][i]=abs(node[i].z-node[j].z);
45             }
46         }
47         l=0.0;
48         r=10000.0;
49         while(r-l>acc){
50             mid=(l+r)*1.0/2;
51             if(prim(mid)>0) l=mid;
52             else r=mid;
53         }
54         printf("%.3f\n",mid);
55     }
56 }

 

                                         
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 輸入read用途:從標準輸入讀取一行,或者從文件描述符FD(file descriptor)中讀取一行,並且將其分割成欄位。   輸出(echo)用途 :在標準輸出上輸出傳遞過來的內容。 ...
  • 正題前的嘮叨 本人是才出來工作不久的小白菜一顆,技術很一般,總是會有遇到一些很簡單的問題卻不知道怎麼做,這些問題可能是之前解決過的。發現這個問題,想著提升一下自己的技術水平,將一些學的新的‘好’東西記錄下來,一是加深印象;二是以後可以作為參考;三是希望博友們可以提出不足和可以優化的地方,一起討論。 ...
  • 眾所周知,我們在寫程式的時候,好習慣是在重要的代碼打上日誌。以便監控程式運行的性能和記錄可能發生的錯誤。但是,如果日誌是基於同步IO文件操作,那麼就必須考慮到訪問總次數或併發數目。如果總次數或併發數目非常大,比如10W或者1K/s 或更多,那麼就要註意IO的同步性能對程式速度的拖慢效應了。 這個時候 ...
  • 本期同樣帶給大家分享的是阿笨在實際工作中遇到的真實業務場景,請跟隨阿笨的視角去如何採用基於開源組件SmartThreadPool線程池技術實現多任務批量處理。在工作中您是否遇到過如何快速高效的處理Job任務列表、如何通過多線程批量處理訂單、如何多線程群發簡訊、如何批量上傳圖片到遠程圖片伺服器或者雲存... ...
  • 最近單位開發一個項目,其中需要用到自動升級功能。因為自動升級是一個比較常用的功能,可能會在很多程式中用到,於是,我就想寫一個自動升級的組件,在應用程式中,只需要引用這個自動升級組件,並添加少量代碼,即可實現自動升級功能。因為我們的程式中可能包含多個類型的文件,比如exe、dll、 config、xm... ...
  • 本期同樣帶給大家分享的是阿笨在實際工作中遇到的真實業務場景,請跟隨阿笨的視角去如何實現採用微軟的ASP.NET OWIN技術承載WebAPI服務,如果您對本期的教程內容感興趣,那麼請允許讓阿笨帶著大家一起學習吧! 廢話不多說,直接上乾貨,我們不生產乾貨,我們只是乾貨的搬運工。 ...
  • 註解是jdk1.5新增的特性.大家都知道,jdk1.5在java的發展史上有著劃時代的意義.而註解的出現,在某種程度上顛覆了框架的設計.比如,spring在註解出現後,改善了原先五大組件的模式,增加了基於註解的實現方式.現在重點講講註解的使用. 元註解: jdk1.5定義了4個元註解,元註解的作用是 ...
  • 字元串轉換成其他基本類型,以及包裝類的自動裝箱、拆箱 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...