最小生成树Prim poj1258 poj2485

poj:1258

Agri-Net

Time Limit: 1000 MS Memory Limit: 10000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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题意:N*N个城市   联通全部城市花费的最小代价  eg:0 4 9 2 第二个城市到第一个代价为4   第三个到第一个代价为9  第四个到第一个代价为21    用的Prim
#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
#define INF 100001

int t,map[105][105];

int prim()
{
    int sum_dis=0;
    int m=1,s=1;   ///s用来标记选当时中的点  m用来标记选过的点
    int point; ///标记当时所在的点
    int u[105]= {false};
    u[s]=true;
    int min;
    int low_dis[105];

    for(int i=1; i<=t; i++)
        low_dis[i]=INF;

    while(true)
    {
        if(t==m)
        break;
        min=INF;
        for(int i=2; i<=t; i++)
        {
             if(!u[i]&&low_dis[i]>map[s][i])
             low_dis[i]=map[s][i];    ///各点到s点的距离
             if(!u[i]&&min>low_dis[i])
             {
                 min=low_dis[i];  ///选取最近的到s的距离
                 point=i;     ///记录这一点
             }
        }  ///遍历完一行了
        sum_dis+=min;
        s=point;
        u[s]=true;  ///这点找过了
        m++;

    }
    return sum_dis;
}

int main()
{
    while(cin>>t)
    {
        for(int i=1; i<=t; i++)
        {
            for(int j=1; j<=t; j++)
            {
                cin>>map[i][j];
            }
        }
        cout<<prim()<<endl;
    }
    return 0;
}

poj2485

Highways

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They‘re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N. Each highway connects
exactly two towns. All highways follow straight lines. All highways can
be used in both directions. Highways can freely cross each other, but a
driver can only switch between highways at a town that is located at the
end of both highways.

The Flatopian government wants to minimize the length of the longest
highway to be built. However, they want to guarantee that every town is
highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed.

The first line of each case is an integer N (3 <= N <= 500),
which is the number of villages. Then come N lines, the i-th of which
contains N integers, and the j-th of these N integers is the distance
(the distance should be an integer within [1, 65536]) between village i
and village j. There is an empty line after each test case.

Output

For each test case, you should output a line
contains an integer, which is the length of the longest road to be built
such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

题意: 最小代价走最远路程   样例意思与上一题一样

解析:这次多了一个判断

///走的路最长 花费时间最小
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
#define INF 0x1f1f1f1f
int n,a[505][505];

int prim()
{
    ///前提的初始化
    int low[65537];
    int low_ss;
    int vis[505]= {false};
    int i,j,point,p,s=1,m=1;
    int min,res=0;
    vis[s]=true;
    for(int i=1; i<=n; i++)
        low[i]=655339;

    ///进行遍历
    while(true)
    {
        low_ss=INF;
        if(n==m)   ///同样遍历了全部点
            break;
        for(int i=2; i<=n; i++)
        {
            if(!vis[i]&&low[i]>a[s][i])
                low[i]=a[s][i];
            if(!vis[i]&&low_ss>low[i])
            {
                low_ss=low[i];
                point=i;
            }
        }
        if(res<low_ss)    ///判断寻找最短代价
            res=low_ss;
        s=point;
        vis[s]=true;
        m++;
    }
    return res;

}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ///输入部分
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        ///输出部分
        printf("%d\n",prim());
    }
    return 0;
}

最小生成树Prim poj1258 poj2485

时间: 2024-09-28 00:02:25

最小生成树Prim poj1258 poj2485的相关文章

POJ1258最小生成树(prim算法)

POJ1258 思路:首先把第一个结点加入树中,每次往树中加入一个结点,加入的结点必须是与当前树中的结点距离最小那个点,这样每次把结点加入树中选取的都是最小权值,循环n-1次后把所有结点都加入树中. #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 1e9; //创建map二维数组储存图表,low数组记录每2个点间最小权值,vis数组标记

poj2485最小生成树prim

Highways Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways.

poj1861 最小生成树 prim &amp; kruskal

// poj1861 最小生成树 prim & kruskal // // 一个水题,为的只是回味一下模板,日后好有个照应不是 #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <iostream> using namespace std; const int MAX_N = 1008; const int INF =

nyist oj 38 布线问题 (最小生成树 prim)

布线问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件: 1.把所有的楼都供上电. 2.所用电线花费最少 输入 第一行是一个整数n表示有n组测试数据.(n<5) 每组测试数据的第一行是两个整数v,e. v表示学校里楼的总个数(v<=500) 随后的e行里,每行有三个整数a,b,c表示a与b之间如果建铺设线路花费为c(c<=100).(哪两栋楼间如果没有指明花费

最小生成树--prim算法

一个无向图G的最小生成树就是由该图的那些连接G的所有顶点的边构成的树,且其总价值最低,因此,最小生成树存在的充分必要条件为图G是连通的,简单点说如下: 1.树的定义:有n个顶点和n-1条边,没有回路的称为树 生成树的定义:生成树就是包含全部顶点,n-1(n为顶点数)条边都在图里就是生成树 最小:指的是这些边加起来的权重之和最小 2.判定条件:向生成树中任加一条边都一定构成回路 充分必要条件:最小生成树存在那么图一定是连通的,反过来,图是连通的则最小生成树一定存在 上图的红色的边加上顶点就是原图的

hdu 3371 最小生成树prim算法

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8992    Accepted Submission(s): 2519 Problem Description In 2100, since the sea level rise, most of the cities disappear. Thoug

最小生成树Prim

首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注意,是对树最近的距离,而不是源点,这和单源最短路径是有区别的. 3.用maps数组保存边的关系. 4.每次先找到离树最近的且没有被访问过的点,以这点的所有边去更新dis数组,也就是更新和树的最近距离. 算法模型: for(循环n-1次,因为默认1点为起始点,已经被访问了) { for(循环n次.)

poj1789Truck History(最小生成树prim算法)

题目链接: 啊哈哈,点我点我 思路:根据字符串中不同的长度建图,然后求图的最小生成树.. 题目: Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18272   Accepted: 7070 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vege

poj2031Building a Space Station(最小生成树prim)

题目链接: 啊哈哈,点我点我 题意: 就是空间站之间有很多球形的东西,然后这些球可能相交,如果相加那么距离为0,否则距离为两球表面的距离,最后求联通这些球形实验室所需要的最小距离... 思路: 相信大家都学过圆相交吧,类推到球是一样的,当两球的距离大于两球的半径之和的时候,说明两球不想交,反之则两球相交,转换成公式为d-(r1+r2)>0则说明两球不相交... 建图后基本上就是套模板了... 题目: Building a Space Station Time Limit: 1000MS   Me