Junle Roads(最小生成ツリー)

7594 ワード

テーマリンク:http://poj.org/problem?id=1251
 
Description

The Head Elder of the troppical island of Lagrishas has a problem.A burst of foreign aid money was spent on extra roads between villages.Butthe jungle over.orads rentlessly,so the large road network is too expensive to mantain.The Council of Elders must chose to stop mantaining some roads.The map above on the left shows the roads in the cored the cored the coress the cores the coress the coremsmontings.The Chief Elder would like to tell the Council the Council of Elders what would be the ssmallest mountthey could spend in aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaattttttttttmantaintaintaintaintaintaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaas that could be mantained most checapply,for 216 aacms per moneth.Your task is to write a program that will solive such probles. 
Input
The input consists of one to 100 data sets、followowed by a final line containing only 0.Each data set starts with a line containing only a number n、which is the number of villages、1n<27、and the village berse berse able ofcapitalized.Each data set iscopleted with n-1 lins s stath with village labels in labelabelse in alphaaaaatical ordes.The e e isinininininininininininininininininininininininininininininininininininininaaaafffffffffrererererereaaaaaaaaaaaafffffffffffffffffffffffrerererererereaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0,theオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンラインオンライン.Mainininininininininininininininininininstststststststcococococoststststststststststststststststststs s s s s..ll always allow travel between all the villages.The network will never have more than 75 roads.No village will have more than 15 roads going to other villages.Inthe sample input below,the firs the mars.over.with 
Output
The output is one integer per line for each data set:the minimum cost in aacms per montain a road system.that connect all the villages.Caution:A bree force solution soution theminese ry evere possible the minable the minit mintre the mintre minit. 
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30
タイトルの大意:
すべての接続する道を示します.例では2行目:Aは2つあります.bとiはそれぞれ12と25の距離です.以下の通りです.
後出力を最小にする距離(2つの例を示しています.)
最小の木を生成するアルゴリズムは2種類あります.1.primと2.ボスを探しています.
両者のテンプレートは違って複雑さも違っています.
prim(O(n^2)時間の複雑さ;
ボス(O)の複雑さを探します.
次に私達は一つずつ話します.
まず、一番簡単なprimだと思います.
primアルゴリズムはまた二つの方法に分けられます.
1.リンク表法(構造体でデータを保存する)
2.連結行列(配列によるデータ保存)
まずリンク表法です.実は彼らのアルゴリズムのステップは同じです.比較する時は空間の複雑さも違います.
リンクテーブルの空間複雑度はリンクマトリックスの空間複雑度より低いです.
リンク行列:
これはリンク表よりよく理解しています.
acコード:
 
#include
#include
#include

#include
#include
#include
#include
#include
using namespace std;

#define ll long long
#define da    0x3f3f3f3f
#define xiao -0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))


int shuzu[30][30];										//        ,           ,     da       
bool biaoji[30];
int shuaxin[30];
int sum,n;

void prime(int x)										//        ,         ,     0     
{
	int i,j;
	sum=0;												//sum    0 
	clean(biaoji,0);									//       (  ) 
	clean(shuaxin,da);									//       (     ) 
	shuaxin[x]=0;										//      ,   0 
	for(i=0;ishuzu[can][j])//      &&            
				shuaxin[j]=shuzu[can][j];	//    ,                 
		}
	}
}

int main()
{
	while(~scanf("%d",&n))
	{
		if(n==0)
			break;
		int i,j;
		clean(shuzu,da);						//       
		for(i=0;i>ch>>x;							//            
			for(j=0;j>ar>>st;					//         
				int fang=ar-'A';				//      
				shuzu[i][fang]=st;				//          ,        
				shuzu[fang][i]=st;				//     ,        
			}
		}
		prime(0);								//       ,            ,     1    
		printf("%d
",sum); // } }
 
 
リンクテーブル:
 
 
ac:
 
#include
#include
#include

#include
#include
#include
#include
#include
using namespace std;

#define ll long long
#define da    10000000
#define xiao -10000000
#define clean(a,b) memset(a,b,sizeof(a))

struct ac{						//        
	int num;					//        
	int space;					//           
};

vector home[30];			//vector     ,              
bool biaoji[30];				//            
int shuaxin[30];				//       
int sum,n;

void prime(int x)				//        
{
	int i,j;
	sum=0;
	clean(biaoji,0);			//      
	for(i=0;i<30;++i)
		shuaxin[i]=da;
	shuaxin[x]=0;				//    0 
	for(i=0;i>ch>>x;
			for(j=0;j>ar>>st;
				int fang=ar-'A';
				ac one,two;						//                
				one.num=i;						//      
				one.space=st;
				two.num=fang;					//    
				two.space=st;
				home[i].push_back(two);			//      
				home[fang].push_back(one);		//    
			}
		}
		prime(0);								//       (           ) 
		printf("%d
",sum); // for(i=0;i<30;++i) // home[i].clear(); } }
 
 
 
ここにリンクがありますので、詳しく説明します.コードを書いてください.分かりやすくします.
オーディのブログリンク:http://blog.csdn.net/niushuai666/article/details/6662911
 
  
リンクを書きたくないです.http://blog.csdn.net/yzllz001/article/details/51789281
#include  
#include  
using namespace std;  
int a[10002];  
struct st  
{  
    int x,y,len;  
}s[10001];  
int find(int x)  			//    
{  
    while(x!=a[x])  
    {  
        x=a[x];  
    }  
    return x;  				//     
}  
int un(int x,int y)  		//     
{  
    x=find(x);  
    y=find(y);  
    if(x!=y)  
    {  
        a[x]=y;  
        return 1;  
    }  
    return 0;  
}  
int cmp(const void *a,const void *b)  	//   
{  
    st c=*(st *)a;  
    st d=*(st *)b;  
    return c.len-d.len;  
}  
int main()  
{  
    int i,j,k,n,m,l,t,kk;  
    char cc[2];  
    char dd[2];  
    while(~scanf("%d",&n))  
    {  
        if(n==0)  
        break;  
        for(i=1;i<=300;i++)  
        {  
            a[i]=i;  
        }  
        int k=0;  
        for(i=0;i