省**「暢通工程」的目標是使全省任何兩個村莊間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可)。經過調查評估,得到的統計表中列出了有可能建設公路的若干條道路的成本。現請你編寫程式,計算出全省暢通需要的最低成本。
input測試輸入包含若干測試用例。每個測試用例的第1行給出評估的道路條數 n、村莊數目m ( < 100 );隨後的 n
行對應村莊間道路的成本,每行給出一對正整數,分別是兩個村莊的編號,以及此兩村莊間道路的成本(也是正整數)。為簡單起見,村莊從1到m編號。當n為0時,全部輸入結束,相應的結果不要輸出。
output對每個測試用例,在1行裡輸出全省暢通需要的最低成本。若統計資料不足以保證暢通,則輸出「?」。
sample input
3 3sample output1 2 1
1 3 2
2 3 4
1 32 3 2
0 100
3?題解:
很水的一道最小生成樹的題,需要用到並查集,最後判斷是否成了一顆最小生成樹。
#include#includeusing
namespace
std;
const
int maxn=105
;int
par[maxn];
intn,m,res,cnt;
struct
edge
es[maxn];
bool cmp(const edge& a,const edge&b)
void init(int
n)int find(int
x)bool same(int x,int
y)void unite(int x,int
y)bool
kruskal()
}if(cnt==m-1
)
return
true
;
return
false;}
intmain()
return0;
}
HDU1863 暢通工程 最小生成樹
最小生成樹 題目大意 給出n個村莊直接的道路權值,讓求出連線所有村莊的權值的最小值。注意判斷一下村莊不連通的情況就行了。include include include define max 105 define inf 999999999 using namespace std bool s max...
HDU 1863 暢通工程 最小生成樹
省 暢通工程 的目標是使全省任何兩個村莊間都可以實現公路交通 但不一定有直接的公路相連,只要能間接通過公路可達即可 經過調查評估,得到的統計表中列出了有可能建設公路的若干條道路的成本。現請你編寫程式,計算出全省暢通需要的最低成本。input 測試輸入包含若干測試用例。每個測試用例的第1行給出評估的道...
HDU 1863 暢通工程
include include include using namespace std const int maxn 5000 5 int n,m,ans,cnt bool flag struct streat edge maxn int root 105 int find int x void k...