最短生成树 Problem E: Jungle Roads

>>>>>>题目地址<<<<<<

  • Prim
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110, INF = 0x3fffffff;
int d[maxn];
bool vis[maxn];
struct node
{
    int nex, cost;
};
vector<node> G[maxn];
struct tnode
{
    int id, d;
    bool operator < (const tnode & tmp) const { return d > tmp.d; }
};
int Prim(int start, int n)
{
    fill(d, d + n, INF);
    memset(vis, false, sizeof(vis));
    d[start] = 0;
    int ans = 0;
    priority_queue<tnode> pq;
    pq.push(tnode{start, 0});
    while(!pq.empty())
    {
        tnode now = pq.top();
        pq.pop();
        if(vis[now.id]) continue;
        vis[now.id] = true;
        ans += d[now.id];
        for(int i = 0; i < G[now.id].size(); ++i)
        {
            int nex = G[now.id][i].nex;
            int cost = G[now.id][i].cost;
            if(vis[nex] == false)
            {
                if(cost < d[nex])
                {
                    d[nex] = cost;
                    pq.push(tnode{nex, d[nex]});
                }
            }
        }
    }
    return ans;
}
int main()
{
    int n;
    while(scanf("%d", &n) != EOF && n != 0)
    {
        for(int i = 0; i < n-1; ++i)
        {
            getchar();
            char v1, v2;
            int nch, cost;
            scanf("%c %d", &v1, &nch);
            for(int j = 0; j < nch; ++j)
            {
                scanf(" %c %d", &v2, &cost);
                G[v1-'A'].push_back(node{v2-'A', cost});
                G[v2-'A'].push_back(node{v1-'A', cost});
            }
        }
        printf("%d\n", Prim(1, n));
        for(int i = 0; i < n; ++i) G[i].clear();
    }
    return 0;
}

  • Kruskal
#include <bits/stdc++.h>
using namespace std;
struct edge
{
    int v1, v2, cost;
    bool operator < (const edge & tmp) const { return cost > tmp.cost; }
};
int FindRoot(int x, vector<int> & f)
{
    int tmp = x;
    while(f[x] != x)
    {
        x = f[x];
    }
    while(f[tmp] != tmp)
    {
        int tmp2 = tmp;
        tmp = f[tmp];
        f[tmp2] = x;
    }
    return x;
}
int Kruskal(int nv, priority_queue<edge> & pq)
{
    vector<int> father(nv+1);
    for(int i = 1; i <= nv; ++i) father[i] = i;
    int ans = 0, numE = 0;
    while(!pq.empty())
    {
        edge now = pq.top();
        pq.pop();
        int ra = FindRoot(now.v1, father), rb = FindRoot(now.v2, father);
        if(ra != rb)
        {
            father[ra] = rb;
            ans += now.cost;
            numE++;
            if(numE >= nv-1) break;
        }
    }
    if(numE != nv-1) return -1;
    else return ans;
}
int main()
{
    int n;
    while(scanf("%d", &n) != EOF && n != 0)
    {
        priority_queue<edge> ans;
        for(int i = 0; i < n-1; ++i)
        {
            char v1, v2; int nch, cost;
            getchar();
            scanf("%c %d", &v1, &nch);
            for(int j = 0; j < nch; ++j)
            {
                scanf(" %c %d", &v2, &cost);
                ans.push(edge{v1-'A', v2-'A', cost});
            }
        }
        printf("%d\n", Kruskal(n, ans));
    }
    return 0;
}

发布了316 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/104864895