prim 樸素 最小生成樹

瑪珈山bug王發表於2020-11-04
#include <bits/stdc++.h>
using namespace std;

const int N = 510;

int grid[N][N];
int st[N];
int dis[N];
int n, m;

int prim()
{
    memset(st, 0, sizeof st);
    memset(dis, 0x3f, sizeof dis);

    int res = 0;

    for (int i = 0; i < n; ++i) {
        int t = -1;     //記錄當前未加入集合的點中離集合最近的點
        for (int j = 1; j <= n; ++j) {
            if (st[j] == 0 and (t == -1 or dis[t] > dis[j]))
                //先判斷是否在集合內部
                t = j;
        }

        //i = 0 時,集合為空,所有dis都為無窮大,此時會選取1節點加入集合中,再用它更新自己和相鄰節點的dis,
        //所以i = 0 時不能判斷dis[t] 和 累加 res;

        if (i and dis[t] == 0x3f3f3f3f)
            return 0;
        
        if (i)  res += dis[t];
        st[t] = true;

        for (int j = 1; j <= n; ++j) {
            dis[j] = min(dis[j], grid[t][j]);
        }
    }
    
    return res;
}

int main() 
{
#ifndef ONLINE_JUDGE
    freopen("D:/VS CODE/C++/in.txt", "r", stdin);
    freopen("D:/VS CODE/C++/out.txt", "w", stdout);
#endif
    cin >> n >> m;

    memset(grid, 0x3f, sizeof grid);

    for (int i = 0; i < m; ++i) {
        int a, b, w;
        cin >> a >> b >> w;
        grid[a][b] = grid[b][a] = min(grid[a][b], w);   //可能存在重邊
        if (a == b)
            grid[a][b] = 0; //避免錄入自環
    }
    
    int res = prim();

    if (res) {
        cout << res;
    }
    else {
        cout << "impossible";
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}

相關文章