zoukankan      html  css  js  c++  java
  • 北大 ACM highways问题研究(最小生成树)

    #include<stdlib.h> 
    #include<stdio.h>  
    #include<queue>
    
    struct vertex//代表一个村庄 
    {
    	int minDist;//到相邻结点的最小的距离
    	bool inMST;//这个村庄是否已经被走过 
    };
    
    int case_num=0;//用例数 
    int village_num; //村庄的数目
    
    int edge[500][500];
    vertex* vertices=NULL;//指向结构体  村庄 的首地址 
    
    int max_lenth=0;//最大的路径 的长度
    
    void init()
    {
    	vertices=(vertex*)malloc(village_num*sizeof(vertex));
    	
    	for(int i=1;i<village_num;i++)
    	{
    		vertices[i].minDist=65536;//都初始化两个村庄之间无限远,也就是没有之间公路 
    		vertices[i].inMST=false;//所有的村庄都没有加入 
    	} 
    	
    	vertices[0].minDist=0;//第一个村庄为开始点 
    	vertices[0].inMST=false;
    } 
    
    /*获得未处理的权值最小的顶点*/
    int getVertexOfMinDist()
    {
    	int min=65536;
    	int minIndex=-1;
    		//遍历所有的点 
    	for(int i=0;i<village_num;i++)
    	{
    		//这个点既是未选中点,并且这个点与相邻结点的距离也是与所有结点最小 
    		if(!vertices[i].inMST&&vertices[i].minDist<min){
    			min=vertices[i].minDist;
    			minIndex=i;
    		}
    	}
    	return minIndex;
    } 
    
    
    /*更新和index顶点相邻的顶点的权值*/
    void updateMinDist(int index)
    {
    	//遍历所有的点 
    	for(int i=0;i<village_num;i++)
    		//这个点未选中,并且这个点和所有点距离的最小值大于与目标点(就是距离最小的结点) 之间的距离 
    		if(!vertices[i].inMST&&(vertices[i].minDist>edge[index][i]))
    			vertices[i].minDist=edge[index][i];
    } 
    
    void prim()
    {
    	vertex* v=NULL;
    	int minIndex=0;
    	
    	while((minIndex = getVertexOfMinDist()) >= 0)
    	{  
            v = vertices + minIndex;  
            v->inMST = true;
            
            if(max_lenth < vertices[minIndex].minDist)
    		{  
            	max_lenth = vertices[minIndex].minDist;  
            }  
    		
    		updateMinDist(minIndex);  
    	}
    	
    	printf("%d
    ",max_lenth); 
    }
    
    void readLine()
    {
    	scanf("%d",&case_num);
    	for(int i=0;i<case_num;i++)
    	{
    		scanf("%d",&village_num);
    		for(int j=0;j<village_num;j++)
    		{
    			for(int k=0;k<village_num;k++)
    				scanf("%d",&edge[j][k]);
    		}
    	}
    	
    	init();
    	prim();
    	max_lenth=0;
    	free(vertices);
    }
    
    int main()
    {
    	readLine();
    	return 0;
    } 
    

    问题描述

    Highways

    Time Limit: 1000MS   Memory Limit: 65536K
         

     

    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
    

    Hint

    Huge input,scanf is recommended.
     
    以下为详细介绍
    首先要确定方法:最小生成树
    求最小生成树有两种算法另外一个博客里有介绍:https://www.cnblogs.com/lyxcode/p/9176020.html
    下面解决这个问题的方法是其中的prim算法
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    node中fs模块
    node生成excel,动态替换表格内容
    Postgresql存放数组形式的数据
    ubuntu下安装typescript
    随笔6
    excel文件导出相应数据统计内容
    随笔4
    随笔3.2
    随笔2
    随笔1
  • 原文地址:https://www.cnblogs.com/lyxcode/p/9176017.html
Copyright © 2011-2022 走看看