POJ 1287 Networking

來源:互聯網
上載者:User

標籤:

  題目連結:

poj.org/problem?id=1287

題目大意:

你被指派到去設計一個地區的連接點,給出你每個點對之間的路線,你需要算出串連所有點路線的總長度。

題目輸入:

一個數字n  代表有 n個頂點,  一個數字 m 有m條邊。

接下來m行是 m條邊的資訊, 起點   終點  權值

題目輸出 :

輸出最小產生樹的值

#include <iostream>#include <cstdlib>#include <cstdio>#include <algorithm>#include <vector>#include <queue>using namespace std;#define INF 0xfffffff#define maxn 22220struct Edge{    int s, e, w;    Edge(int s=0,int e=0,int w=0):s(s), e(e), w(w) {}    bool friend operator < (Edge A, Edge B)    {        return A.w < B.w;    }}P[maxn];int n, m;int Par[maxn];void Init();int Kruskal();int GetPar(int a);void Meger(int a,int b);int main(){    int s, e, w;    while(cin >> n, n)    {        cin >> m;        Init();        for(int i=0; i<m; i++)        {            cin >> s >> e >>w;            P[i] = Edge(s,e,w);        }        sort(P, P+m);        int ans = Kruskal();        cout << ans << endl;    }    return 0;}void Init(){    for(int i=0; i<=n; i++)    {        Par[i]=i;    }}int Kruskal(){    int sum = 0;    int Nnode = 1;    for(int i=0; i<m; i++)    {        if( GetPar(P[i].s) != GetPar(P[i].e) )        {            sum += P[i].w;            Meger(P[i].s,P[i].e);            Nnode ++;        }        if(Nnode == n)            return sum;    }    return 0;}int GetPar(int a){    if(Par[a] != a)        Par[a] = GetPar(Par[a]);    return Par[a];}void Meger(int a,int b){    Par[GetPar(a)] = Par[GetPar(b)];}

 

POJ 1287 Networking

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.