soj1169: Networking_最小生成树

soj1169: Networking

http://acm.scu.edu.cn/soj/problem.action?id=1169

简介题意:求最小生成树的权值之和。prim和kruskal都可以,我都写了,不过kruskal数组开小了re了好几次,简直太愚蠢,大家要注意看题,本道题非常简单。

首先prim:

#include
#include
#include
using namespace std;
#define MAX 55
#define INF 0x3f3f3f3f
int map[MAX][MAX];
int dist[MAX];
bool visit[MAX];

void prim(int n)
{
    memset(dist,INF,sizeof(dist));
    memset(visit,0,sizeof(visit));
    for(int i = 1;i <= n; i++)
        if(map[1][i] != 0)
            dist[i] = map[1][i];
    visit[1] = 1;
    int price = 0;
    for(int i = 1;i <= n; i++)
    {
        int u;
        int min = INF;
        for(int j = 1;j <= n; j++)
            if(!visit[j] && dist[j]map[u][j])
                    dist[j] = map[u][j];
        }
        else break;

    }
    printf("%d\n",price);
}

int main()
{
    int n,m;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d",&m);
        memset(map,0,sizeof(map));
        for(int i = 1;i <= m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(map[a][b] == 0)
                map[a][b] = map[b][a] = c;
            else if(map[a][b] > c)
                map[a][b] = map[b][a] = c;
        }
        prim(n);
    }
    return 0;
}

没什么好解释的,不过又排了一次第一,高兴~

然后接下来kruskal:

#include
#include
#include
using namespace std;
#define MAX 110
#define INF 0x3f3f3f3f
int pre[MAX];
int map[MAX][MAX];
int visit[MAX][MAX];

struct Node{
    int x,y,len;
}edge[MAX];

bool cmp(Node a, Node b)
{
    return a.len < b.len;
}

int find(int a)
{
    if(pre[a] == a)
        return a;
    else
        return pre[a] = find(pre[a]);
}

void Kruskal(int n,int k)
{
    for(int i = 0;i <= n; i++)
        pre[i] = i;
    int price = 0;
    for(int i = 1;i <= k; i++)
    {
        int a = find(edge[i].x);
        int b = find(edge[i].y);
        if(a != b)
        {
            pre[a] = b;
            price += edge[i].len;
        }
    }
    printf("%d\n",price);
}

int main()
{
    int n,m;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d",&m);
        memset(map,0,sizeof(map));
        memset(visit,0,sizeof(visit));
        for(int i = 1;i <=m ; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(map[a][b] == 0)
                map[a][b] = map[b][a] = c;
            else if(map[a][b] > c)
                map[a][b] = map[b][a] = c;
        }
        int k = 1;
        for(int i = 1;i <= n; i++)
        for(int j = 1;j <= n; j++)
        {
            if(!visit[i][j] && map[i][j] != 0)
            {
                edge[k].x = i;
                edge[k].y = j;
                edge[k++].len = map[i][j];
                visit[i][j] = visit[j][i] = 1;
            }
        }
        sort(edge+1,edge+k,cmp);
        Kruskal(n,k-1);
    }
    return 0;
}

kruskal主要看数组啊!!!我刚才没有改MAX,所以re好多次,因为kruskal写的时候要注意本题可以两点之间很多条路线,所以记录最小的即可。要注意哦。

判断用哪一种方法,你就看点多不多,多的话你就用kruskal,少的话就prim

你可能感兴趣的:(soj)