最小生成樹hdu 1863暢通工程

Problem Description
省政府“暢通工程”的目標是使全省任何兩個村莊間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可)。經過調查評估,得到的統計表中列出了有可能建設公路的若干條道路的成本。現請你編寫程序,計算出全省暢通需要的最低成本。

Input
測試輸入包含若干測試用例。每個測試用例的第1行給出評估的道路條數 N、村莊數目M ( < 100 );隨後的 N
行對應村莊間道路的成本,每行給出一對正整數,分別是兩個村莊的編號,以及此兩村莊間道路的成本(也是正整數)。爲簡單起見,村莊從1到M編號。當N爲0時,全部輸入結束,相應的結果不要輸出。

Output
對每個測試用例,在1行裏輸出全省暢通需要的最低成本。若統計數據不足以保證暢通,則輸出“?”。

Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100

Sample Output
3
?

http://acm.hdu.edu.cn/showproblem.php?pid=1863
kruskal求最小生成樹 並查集加排序 ,因爲是路徑壓縮的並查集所以均攤的複雜度是常數,所以複雜度主要來自於排序。對於結構體排序寫個重載運算符或者cmp函數即可。

```
#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<cstdio>

using namespace std;

typedef long long LL;
const int maxn = 100 + 10;
const int INF = 1E9;

struct edge
{
    int from,to;
    LL cost;
    bool operator < (const edge &x) const
    {
        return cost <x.cost;
    }
};
edge e[maxn*maxn];

int n,m;
int father[maxn];
void init()
{
    for(int i =0;i<n;i++) father[i] = i;
}
int get_fa(int x)
{
    if(father[x] == x) return x;
    return father[x] = get_fa(father[x]);
}
void hebing(int x,int y)
{
    int xx = get_fa(x);
    int yy = get_fa(y);
    if(xx==yy) return;
    father[xx] = yy;
}
bool same(int x,int y)
{
    return get_fa(x) == get_fa(y);
}
LL kruskal()
{
    LL res = 0;
    sort(e,e+m);
    for(int i = 0;i<m;i++)
    {
        if(same(e[i].from,e[i].to)) continue;
        hebing(e[i].from,e[i].to);
        res += e[i].cost;
    }
    return res;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(m==0) break;
        init();
        for(int i = 0;i<m;i++)
            scanf("%d%d%lld",&e[i].from,&e[i].to,&e[i].cost);
        LL res = kruskal();
        for(int i =0;i<n;i++)
        {
            if(!same(i,0)) res = -1;
        }
        if(res == -1) printf("?\n");
        else printf("%lld\n",res);
    }
    return 0;
}

prim解法,priority_queue優先隊列(堆)加速的prim算法

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<map>
#include<set>
#include<vector>


using namespace std;

typedef long long LL;
const int maxn = 100 + 10;
const int INF  = 1E9;

int n,m;
struct edge{
    int to;
    LL cost;
    edge(int tt,int cc):to(tt),cost(cc){}
    edge(){}
    bool operator <(const edge &s)const
    {
        return s.cost<cost;
    }
};
vector<edge> g[maxn];
priority_queue<edge> q;
bool vis[maxn];
void init()
{
    for(int i = 1;i<=m;i++) g[i].clear();
    while(q.size()) q.pop();
    memset(vis,false,sizeof(vis));
}
LL prim()
{
    LL res = 0;
    vis[1] = true;
    for(int i=0;i<g[1].size();i++)
        q.push(g[1][i]);
    while(q.size())
    {
        edge s = q.top();
        q.pop();
        if(vis[s.to]) continue;
        vis[s.to] = true;
        res += s.cost;
        for(int i=0;i<g[s.to].size();i++)
            q.push(g[s.to][i]);
    }
    return res;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(m==0) break;
       init();
       for(int i =1;i<=m;i++)
       {
           int u,v;
           LL w;
           scanf("%d%d%lld",&u,&v,&w);
           g[u].push_back(edge(v,w));
           g[v].push_back(edge(u,w));
       }
       LL res = prim();
       for(int i = 1;i<=n;i++)
       {
           if(!vis[i]) res = -1;
       }
       if(res == -1) printf("?\n");
       else printf("%lld\n",res);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章