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;
    }
    



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

  • 相关阅读:
    第八篇 Flask中的蓝图
    第七篇 Flask实例化配置及Flask对象配置
    第六篇 Flask中的路由系统
    第五篇 Flask 中内置的 Session
    第四篇 Flask 中的模板语言 Jinja2 及 render_template 的深度用法
    第三篇 Flask中的request
    守护进程与守护线程
    GIL(全局解释器锁)与互斥锁
    python 面向对象
    实现能计算类似1
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785785.html
Copyright © 2011-2022 走看看