int choose(int distance[], int n, bool found[]) {
    int min = INT_MAX, minpos = -1;
    for(int i=0; i<n; i++)
        if(distance[i]<min && !found[i])
            min = distance[i], minpos = i;
    return minpos;
}

void shortestpath(int v, int *cost[], int distance[], int n, bool found[]) {
    for(int i=0; i<n; i++) {
        found[i] = false;
        distance[i] = cost[v][i];
    }
    found[v] = true;
    distance[v] = 0;
    for(int i=0; i<n-2; i++) {
        int u = choose(distance, n, found);
        found[u] = true;
        for(int w=0; w<n; w++)
            if(!found[w] && distance[u]+cost[u][w] < distance[w])
                distance[w] = distance[u]+cost[u][w];
    }
}

void allcosts(int *cost[], int *distance[], int n) {
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            distance[i][j] = cost[i][j];
    for(int k=0; k<n; k++)
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                if(distance[i][k]+distance[k][j] < distance[i][j])
                    distance[i][j] = distance[i][k]+distance[k][j];
}
struct node {
    int vertex;
    node *link;
};

struct hdnodes {
    int count;
    node *link;
};

void topsort(hdnodes graph[], int n) {
    int top = -1;
    for(int i=0; i<n; i++) {
        if(!graph[i].count) {
            graph[i].count = top;
            top = i;
        }
    }
    for(int i=0; i<n; i++) {
        if(top == -1) {
            fprintf(stderr, "\nNetwork has a cycle. Sort terminated.\n");
            exit(1);
        }
        int j = top;
        top = graph[top].count;
        printf("v%d, ", j);
        for(node *ptr=graph[j].link; ptr; ptr=ptr->link) {
            int k = ptr->vertex;
            graph[k].count--;
            if(!graph[k].count) {
                graph[k].count = top;
                top = k;
            }
        }
    }
}

最小支撑树(minimum spanning tree)

#include <iostream>
using namespace std;

class UnionFindSets {
    int *father, *rank;
public:
    UnionFindSets(int N) {
        father = new int[N], rank = new int[N];
        for(int x=0; x<N; x++) father[x] = x, rank[x] = 1;
    }
    int FindSets(int x) {
        if(x != father[x]) father[x] = FindSets(father[x]);
        return father[x];
    }
    bool SameSets(int x, int y) {
        return FindSets(x) == FindSets(y);
    }
    void UnionSets(int x, int y) {
        if((x = FindSets(x)) == (y = FindSets(y))) return;
        if(rank[x] <= rank[y]) father[x] = y, rank[y] += rank[x];
        else                   father[y] = x, rank[x] += rank[y];
    }
    ~UnionFindSets() {
        delete[] father, delete[] rank;
    }
};

struct edge {
    int u, v, w;
};

bool cmp(const edge &l, const edge &r) {
    return l.w<r.w;
}

int kruskal(edge e[], int m, int n) {
    UnionFindSets ufs(n);
    int ans=0;
    for(int i=0; i<m; i++)
        if(!ufs.SameSets(e[i].u, e[i].v)) {
            ufs.UnionSets(e[i].u, e[i].v);
            ans += e[i].w;
        }
    return ans;
}
 
int main() {
    int m, n;
    scanf("%d%d", &m, &n);

    edge e[m];
    for(int i=0; i<m; i++) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
    sort(e, e+m, cmp);

    printf("%d\n", kruskal(e, m, n));
    return 0;
}

一͛世͛珍͛藏͛
86 声望6 粉丝

« 上一篇
gdb (lldb)
下一篇 »
小顶堆