zoukankan      html  css  js  c++  java
  • POJ 1258:Agri-Net Prim最小生成树模板题

    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 45050   Accepted: 18479

    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算法模板题。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #pragma warning(disable:4996)
    using namespace std;
    
    int num;
    int map[102][102];
    int stack[102];
    int minidis[102];
    
    int prim()
    {
    	int i,j,s,result;
    
    	memset(stack,0,sizeof(stack));
    	for(i=1;i<=num;i++)
    	{
    		minidis[i]=100005;
    	}
    
    	stack[1]=1;
    	minidis[1]=0;
    	s=1;
    	result=0;
    
    	for(i=1;i<=num-1;i++)
    	{
    		int min_all=100005;
    		int min_temp=0;
    		for(j=2;j<=num;j++)
    		{
    			if(stack[j]==0&&minidis[j]>map[s][j])
    			{
    				minidis[j]=map[s][j];
    			}
    			if(stack[j]==0&&minidis[j]<min_all)
    			{
    				min_temp=j;
    				min_all=minidis[j];
    			}
    		}
    		s=min_temp;
    		stack[s]=1;
    		result += min_all;
    	}
    	return result;
    }
    
    int main()
    {
    	int i,j;
    
    	while(cin>>num)
    	{
    		for(i=1;i<=num;i++)
    		{
    			for(j=1;j<=num;j++)
    			{
    				scanf("%d",&map[i][j]);
    			}
    		}
    		cout<<prim()<<endl;
    	}
    	return 0;
    }
    



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    ar游戏开发入门三步走
    什么是 OpenCL OpenGL
    https 服务器搭建
    javascript 访问cookie信息
    手机网页制作需要注意的一点东西
    asp.net下载文件几种方式
    javascriptM
    PDF转JPG
    java实现PDF转HTML
    关于mysql ERROR 1045 (28000)错误的解决办法
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785785.html
Copyright © 2011-2022 走看看