洛谷P1550 [USACO08OCT]打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole
題目背景
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<cstring> #include<algorithm> #define maxn 310 using namespace std; int n,fa[maxn],num; struct node{int from,to,v;bool operator < (const node &a)const{return v<a.v;} }e[maxn*maxn]; int find(int x){if(x==fa[x])return x;return fa[x]=find(fa[x]); } int main(){scanf("%d",&n);int x;for(int i=0;i<=n;i++)fa[i]=i;for(int i=1;i<=n;i++){scanf("%d",&x);e[++num].from=0;e[num].to=i;e[num].v=x;}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){scanf("%d",&x);if(i>j)e[++num].from=i,e[num].to=j,e[num].v=x;}}sort(e+1,e+num+1);int ans=0;for(int i=1;i<=num;i++){int f1=find(e[i].from),f2=find(e[i].to);if(f1!=f2){ans+=e[i].v;fa[f1]=f2;}}printf("%d",ans);return 0; }
?
轉載于:https://www.cnblogs.com/thmyl/p/8977312.html
總結
以上是生活随笔為你收集整理的洛谷P1550 [USACO08OCT]打井Watering Hole的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 凉开头的成语有哪些啊?
- 下一篇: 市电电压大于100V才来电