题目链接: 传送门

Agri-Net

Time Limit: 1000MS     Memory Limit: 10000K

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算法O(V^2)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX_V = 105;
int edge[MAX_V][MAX_V];
int dis[MAX_V];
bool vis[MAX_V];
int N;

int prim()
{
    memset(dis,INF,sizeof(dis));
    memset(vis,false,sizeof(vis));
    for (int i = 1;i <= N;i++)
    {
        dis[i] = edge[i][1];
    }
    dis[1] = 0;
    vis[1] = true;
    int sum = 0;
    for (int i = 1;i < N;i++)
    {
        int tmp = INF,pos;
        for (int j = 1;j <= N;j++)
        {
            if(!vis[j] && tmp > dis[j])
            {
                tmp = dis[j];
                pos = j;
            }
        }
        if (tmp == INF) return 0;
        vis[pos] = true;
        sum += dis[pos];
        for(int j = 1;j <= N;j++)
        {
            if (!vis[j] && edge[pos][j] < dis[j])
            {
                dis[j] = edge[pos][j];
            }
        }
    }
    return sum;
}

int main()
{
    while (~scanf("%d",&N))
    {
        for (int i = 1;i <= N;i++)
        {
            for (int j = 1;j <= N;j++)
            {
                scanf("%d",&edge[i][j]);
            }
        }
        int res = prim();
        printf("%d\n",res);
    }
    return 0;
}

Prim算法O(ElogV)

#include<iostream>
#include<vector>
#include<queue>
#include<utility>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int>pii;  //first  最短距离  second 顶点编号
const int INF = 0x3f3f3f3f;
const int MAX = 105;
struct edge{
    int to,cost;
    edge(int t,int c):to(t),cost(c){}
};
vector<edge>G[MAX];
int N,dis[MAX];
bool vis[MAX];

int prim()
{
    int res = 0;
    priority_queue<pii,vector<pii>,greater<pii> >que;
    memset(dis,INF,sizeof(dis));
    memset(vis,false,sizeof(vis));
    dis[1] = 0;
    que.push(pii(0,1));
    while (!que.empty())
    {
        pii p = que.top();
        que.pop();
        int v = p.second;
        if (vis[v] || p.first > dis[v]) continue;
        vis[v] = true;
        res += dis[v];
        for (int i = 0;i < G[v].size();i++)
        {
            edge e = G[v][i];
            if (dis[e.to] >  e.cost)
            {
                dis[e.to] = e.cost;
                que.push(pii(dis[e.to],e.to));
            }
        }

    }
    return res;
} 

int main()
{
    while (~scanf("%d",&N))
    {
        int tmp;
        for (int i = 1;i <= N;i++)
        {
            G[i].clear();
            for (int j = 1;j <= N;j++)
            {
                scanf("%d",&tmp);
                G[i].push_back(edge(j,tmp));
            }
        }
        int res = prim();
        printf("%d\n",res);
    }
    return 0;
}

Kruskal算法O(ElogV)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = (105*105-105)/2;
struct Edge{
    int u,v,w;
};
int N,father[MAX],rk[MAX];
struct Edge edge[MAX];

bool cmp(Edge x,Edge y)
{
    return x.w < y.w;
}

void init()
{
    memset(father,0,sizeof(father));
    memset(rk,0,sizeof(rk));
    for (int i = 0;i <= N;i++)
    {
        father[i] = i;
    }
}

int find(int x)
{
    int r = x;
    while (father[r] != r)
    {
        r = father[r];
    }
    int i = x,j;
    while (i != r)
    {
        j = father[i];
        father[i] = r;
        i = j;
    }
    return r;
}
/*int find(int x)
{
    return x == father[x]?x:father[x] = find(father[x]);
}*/

void unite(int x,int y)
{
    int fx,fy;
    fx = find(x);
    fy = find(y);
    if (fx == fy)   return;
        if (rk[fx] < rk[fy])
        {
            father[fx] = fy;
        }
        else
        {
            father[fy] = fx;
            if (rk[x] == rk[y])
            {
                rk[x]++;
            }
        }

}

/*void unite(int x,int y)
{
    int fx = find(x),fy = find(y);
    if (fx != fy)
    {
        father[fx] = fy;
    }
}*/

int main()
{
    while (~scanf("%d",&N))
    {
        int tmp,cnt = 0,sum = 0;
        for (int i = 1;i <= N;i++)
        {
            for (int j = 1;j <= N;j++)
            {
                scanf("%d",&tmp);
                if (i < j)
                {
                    edge[cnt].u = i;
                    edge[cnt].v = j;
                    edge[cnt].w = tmp;
                    cnt++;
                }
            }
        }
        init();
        sort(edge,edge+cnt,cmp);
        for (int i = 0;i < cnt;i++)
        {
            int x,y;
            x = find(edge[i].u);
            y = find(edge[i].v);
            if (x != y)
            {
                unite(x,y);
                sum += edge[i].w;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)的更多相关文章

  1. 最小生成树 Prim Kruskal

    layout: post title: 最小生成树 Prim Kruskal date: 2017-04-29 tag: 数据结构和算法 --- 目录 TOC {:toc} 最小生成树Minimum ...

  2. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  3. 数据结构学习笔记05图(最小生成树 Prim Kruskal)

    最小生成树Minimum Spanning Tree 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 树: 无回路   |V|个顶 ...

  4. 布线问题 最小生成树 prim + kruskal

    1 : 第一种 prime     首先确定一个点 作为已经确定的集合 , 然后以这个点为中心 , 向没有被收录的点 , 找最短距离( 到已经确定的点 ) , 找一个已知长度的最小长度的 边 加到 s ...

  5. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  6. POJ 1258 Agri-Net (最小生成树)

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

  7. POJ 1751 Highways(最小生成树&Prim)题解

    思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...

  8. 最小生成树-Prim&Kruskal

    Prim算法 算法步骤 S:当前已经在联通块中的所有点的集合 1. dist[i] = inf 2. for n 次 t<-S外离S最近的点 利用t更新S外点到S的距离 st[t] = true ...

  9. 邻接表c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    graph.c #include <stdio.h> #include <stdlib.h> #include <limits.h> #include " ...

随机推荐

  1. windows7下启动mysql服务出现服务名无效

    出现提示: WIN 7 cmd命令行下,net start mysql,出现 服务名无效提示: 问题原因: mysql服务没有安装. 解决办法: 在 mysql bin目录下 以管理员的权限 执行 m ...

  2. centos7 安装nginx和php5.6.25遇到 无法访问php页面 报错file not found 问题解决

    php-fpm安装完成,nginx安装完成 netstap -ntl| 发下端口正常开启 iptables -L 返现9000端口已经开放 ps -aux|grep nginx 发下nginx进程正常 ...

  3. web性能优化——浏览器相关

    简介 优化是一个持续的过程.所以尽可能的不要有人为的参与.所以能自动化的或者能从架构.框架级别解决的就最更高级别解决. 这样即能实现面对开发人员是透明的.不响应,又能确保所有资源都是被优化过的. 场景 ...

  4. 系统升级日记(2)- 升级到SharePoint Server 2013

    最近一段时间在公司忙于将各类系统进行升级,其最主要的目标有两个,一个是将TFS2010升级到TFS2013,另外一个是将SharePoint 2010升级到SharePoint 2013.本记录旨在记 ...

  5. 国内网站常用的一些 CDN 公共库加速服务

    CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用.与将JS库存放在服务器单机上相比,CDN公共库更加稳定.高速.一 般的CDN公共库都会包含全球所有最流行的开源JavaScri ...

  6. js滚动到底部事件

    window.innerHeight表示窗口高度 $(document).height()返回文档高度 $(document).scrollTop()返回滚动条与顶部的距离,在最上部时为0,在最下部时 ...

  7. android 资讯阅读器

    最近找申请到了一个不错的接口 , 非常适合拿来写一个资讯类的app. 现在着手写,随写随更.也算是抛砖引玉.烂尾请勿喷.╭(╯^╰)╮ android 资讯阅读器 第一阶段目标样式(滑动切换标签 , ...

  8. 个人对final发布产品的排名

    结果 作品 组长 个人评委名次 个人评委平均 个人评委方差 投票数 团队评委名次 团队评委平均 团队评委方差 武志远-新蜂-俄罗斯 武志远 1 2.22 1.91 23 1 2 0.80 王森-天天向 ...

  9. Ajax深入学习

    1.ajax如何减轻服务器的负担的? 2.如何合理的使用ajax? 3.一个页面一进来等文档加载完毕:走ajax请求去了?    用户体验真的好吗?

  10. MyBatis学习--SqlMapConfig.xml配置文件

    简介 SqlMapConfig.xml是MyBatis的全局配置文件,在前面的文章中我们可以看出,在SqlMapConfig.xml主要是配置了数据源.事务和映射文件,其实在SqlMapConfig. ...