【最小生成树】poj1258

Agri-Net

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
The distance between any two farms will not exceed 100,000. 

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28
裸的最小生成树,分别用prim, kruskal, prim + priority_queue做了,那天有心情可以再写个prim + 手工堆
  
  
kruskal
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ty
{
    long x, y, l;
};
long n;
long fa[110];
ty a[10010];
long const INF = 1000000;

bool comp (ty a, ty b)
{
    return (a.l < b.l);
}

long findfa(long x)
{
    if (fa[x] == x) return x;
    return fa[x] = findfa(fa[x]);
}
int main()
{
    freopen("poj1258.in", "r", stdin);
    while (scanf("%d\n", &n) != EOF)
    {
        long m = 0;
        memset(a, 0 ,sizeof(a));
        for (long i = 1; i <= n; i++)
            for (long j = 1; j <= n; j++)
            {
                long tmp;
                scanf("%d ", &tmp);
                if ((tmp != 0) && (i < j))
                {
                    a[m].x = i;
                    a[m].y = j;
                    a[m].l = tmp;
                    m++;
                }
            }
        m++;
        sort (a, a + m, comp);
        for (long i = 1; i <= n; i++)
            fa[i] = i;
        long cnt = 0;
        long sum = 0;
        for (long i = 0; i < m; i++)
        {
            long fx = findfa(a[i].x);
            long fy = findfa(a[i].y);

            if (fx != fy)
            {
                fa[fx] = fy;
                cnt++;
                sum += a[i].l;
                if (cnt >= n - 1) break;
            }
        }

        printf("%d\n", sum);

    }
    return 0;
}


  
prim:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
long n;
long a[110][110];
long dist[110];
bool v[110];
long const INF = 1000000;
void prim()
{
    memset(v, 0, sizeof(v));
    v[1] =  1;
    for (long i = 1; i <= n; i++)
    {
        dist[i] = a[1][i];
       // cout << dist [i]<<' ';
    }
    //cout << endl;
    long sum = 0;
    for (long k = 1; k < n; k++)
    {
        long minn = INF;
        long point = 0;
        for(long i = 1; i <= n; i++)
        {
            if((!v[i]) && (dist[i] < minn))
            {
                minn = dist[i];
                point = i;
            }
        }

        v[point] = true;
        sum += dist[point];
        //cout << sum <<endl;
        dist[point] = INF;

        for (long i = 1; i <= n; i++)
        {
            if (!v[i]&&(a[point][i] < dist[i]))
                dist[i] = a[point][i];
          //  cout << dist [i]<<' ';
        }
       // cout << endl;
    }
    printf("%d\n", sum);

}

int main()
{
    freopen("poj1258.in", "r", stdin);
    while (scanf("%d\n", &n) != EOF)
    {
        for (long i = 1; i <= n; i++)
        {
            for (long j = 1; j <= n; j++)
            {
                scanf("%d ", &a[i][j]);
            }
        }
        prim();
    }
    return 0;
}


  
prim + priority_queue:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const  long N = 110, M=10100;
struct ty
{
    long t, w, next;
    bool operator < (const ty &a) const
    {
        if (w > a.w) return true;
			else return false;
    }
}edge[M*2];
priority_queue <ty> q;
long n, dist[N], head[N], cnt;
bool v[N];
void insertedge(long x, long y, long z, long k)
{
    edge[k].t = y;
    edge[k].next = head[x];
    edge[k].w = z;
    head[x] = k;
}
void init()
{
    long m = 0;
    for(long i = 1; i <= n; i++)
    {
        for(long j = 1; j <= n; j++)
        {
            long tmp;
            scanf("%d", &tmp);
            insertedge(i, j, tmp, ++m);
            insertedge(j, i, tmp, ++m);
        }
    }
}
void prim()
{
    long result=0;
    ty t1, t2;
    memset(dist, 127, sizeof(dist));
    dist[1] = 0;
    for (long i = head[1]; i != 0; i = edge[i].next)
    {
        if (edge[i].w < dist[edge[i].t])
        {
            dist[edge[i].t] = edge[i].w;
            q.push(edge[i]);
        }
    }
    while( !q.empty() )
    {
        t1 = q.top();
        q.pop();
        if (dist[t1.t] == 0) continue;
        result += t1.w;
        dist[t1.t] = 0;
        for (long j = head[t1.t]; j != 0; j = edge[j].next)
        {
            long u = edge[j].t;
            if ((dist[u] != 0) && (dist[u] > edge[j].w))
            {
                dist[u] = edge[j].w;
                t2.t = u;
                t2.w = dist[u];
                q.push(t2);
            }
        }
    }
    cout << result << endl;
}
int main()
{
    freopen("poj1258.in", "r", stdin);
    while (scanf("%d", &n) != EOF)
    {
        memset(edge, 0, sizeof(edge));
        memset(head, 0, sizeof(head));
        init();
        prim();
    }
}


  
  
全部评论

相关推荐

会飞的猿:本人来了,手一抖转错了,我是学生,能还给我吗
点赞 评论 收藏
分享
2024-12-27 23:45
已编辑
三江学院 Java
程序员牛肉:死局。学历+无实习+项目比较简单一点。基本就代表失业了。 尤其是项目,功能点实在是太假了。而且提问点也很少。第一个项目中的使用jwt和threadlocal也可以作为亮点写出来嘛?第二个项目中的“后端使用restful风格”,“前端采用vue.JS”,“使用redis”也可以作为亮点嘛? 项目实在是太简单了,基本就是1+1=2的水平。而你目标投递的肯定也是小厂,可小厂哪里有什么培养制度,由于成本的问题,人家更希望你来能直接干活,所以你投小厂也很难投。基本就是死局,也不一定非要走后端这条路。可以再学一学后端之后走测试或者前端。 除此之外,不要相信任何付费改简历的。你这份简历没有改的必要了,先沉淀沉淀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务