最小生成树 Problem D: 继续畅通工程

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

  • Prim:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110, INF = 0x3fffffff;
int d[maxn];
bool vis[maxn];
struct node
{
    int nex, len, sign;
};
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 + 1, 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 sign = G[now.id][i].sign;
            int cost = sign == 0 ? G[now.id][i].len : 0;
            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)
    {
        int ne = n * (n - 1) / 2;
        for(int i = 0; i < ne; ++i)
        {
            int c1, c2 , cost, sign;
            scanf("%d %d %d %d", &c1, &c2, &cost, &sign);
            G[c1].push_back(node{c2, cost, sign});
            G[c2].push_back(node{c1, cost, sign});
        }
        printf("%d\n", Prim(1, n));
        for(int i = 1; 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)
    {
        int ne = n * (n - 1) / 2;
        priority_queue<edge> pq;
        for(int i = 0; i < ne; ++i)
        {
            int c1, c2, cost, sign;
            scanf("%d %d %d %d", &c1, &c2, &cost, &sign);
            pq.push(edge{c1, c2, sign == 0 ? cost : 0});
        }
        printf("%d\n", Kruskal(n, pq));
    }
    return 0;
}

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

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/104864058
今日推荐