码迷,mamicode.com
首页 > 其他好文 > 详细

最小生成树Prim poj1258 poj2485

时间:2014-07-23 15:06:26      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

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,布布扣,bubuko.com

最小生成树Prim poj1258 poj2485

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/zhangying/p/3862840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!