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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

洛谷 P1550 浇水

發布時間:2025/7/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷 P1550 浇水 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

洛谷 1550 澆水

題目背景

John的農場缺水了!!!

題目描述

Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conveniently numbered 1..N. He may bring water to a pasture either by building a well in that pasture or connecting the pasture via a pipe to another pasture which already has water.

Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).

Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).

Determine the minimum amount Farmer John will have to pay to water all of his pastures.

POINTS: 400

農民John 決定將水引入到他的邊長為n(1<=n<=300)的牧場。他準備通過挖若

干井,并在各塊田中修筑水道來連通各塊田地以供水。在第i 號田中挖一口井需要花費W_i(1<=W_i<=100,000)元。連接i 號田與j 號田需要P_ij (1 <= P_ij <= 100,000 , P_ji=P_ij)元。

請求出農民John 需要為連通整個牧場的每一塊田地所需要的錢數。

輸入輸出格式

輸入格式:
第1 行為一個整數n。

第2 到n+1 行每行一個整數,從上到下分別為W_1 到W_n。

第n+2 到2n+1 行為一個矩陣,表示需要的經費(P_ij)。

輸出格式:
只有一行,為一個整數,表示所需要的錢數。

輸入輸出樣例

輸入樣例#1:
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
輸出樣例#1:
9
說明

John等著用水,你只有1s時間!!!

思路:看到題面很容易想到最小生成樹,但這題難在有一個水井系統。

考慮到無論如何最少也需要打一口井,所以我們可以將水井看做一個點,連接田地和水

井的費用即打井的費用,之后就可以求最小生成樹了。

題解:

#include<iostream> #include<cstdio> #include<algorithm> using namespace std; struct cc{int from,to,cost; }es[100000]; int val[505],f[505]; int tot=0; void build(int ff,int tt,int pp) {tot++;es[tot].from=ff;es[tot].to=tt;es[tot].cost=pp; } int cmp(cc a,cc b) {return a.cost<b.cost; } int find(int w) {if(w!=f[w]){f[w]=find(f[w]);}return f[w]; } int main() {int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&val[i]);}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int fee;scanf("%d",&fee);build(i,j,fee);}}for(int i=1;i<=n;i++){build(0,i,val[i]);}for(int i=1;i<=n;i++){f[i]=i;}sort(es+1,es+n*n+n+1,cmp);int ans=0;for(int i=1;i<=n*n+n+1;i++){if(find(es[i].from)!=find(es[i].to)){f[find(es[i].from)]=find(es[i].to);ans+=es[i].cost;}}printf("%d",ans);return 0; }

轉載于:https://www.cnblogs.com/-feather/p/7779934.html

總結

以上是生活随笔為你收集整理的洛谷 P1550 浇水的全部內容,希望文章能夠幫你解決所遇到的問題。

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