HDOJ 1301 Jungle Roads(最小生成樹

Jungle Roads
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

Description


The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems. 

Input

The input consists of one to 100 data sets, followed 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, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will 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 (before or after in the alphabet). In the sample input below, the first data set goes with the map above. 


題意很簡單,求一個最小生成樹,求出樹的最短距離和,最小生成樹有兩種算法,一種是prim算法,一種是kruskal算法,先來看prim,基本過程爲:1.將圖中n個點分爲兩個集合。2.U集合爲已經在生成樹上的頂點集。3.V集合爲尚未落在生成樹上的頂點集。4.不斷選取U中頂點與V中頂點權值最小的邊。結束。

再來看這次用到的Kruskal算法,將每條邊的權值排序,每次選擇最小的邊加入生成樹中,如果它的添加不使樹產生迴路那麼這條邊就加入生成樹中,我們將邊排序,然後根據最小的邊來不斷的構造樹,其中判斷迴路比較難,這裏使用了一種數據結構需要學習一下,並查集。能夠快速的判斷樹中是否有環,並且合併不相交的集合,代碼雖短,很實用效率也很高,這裏需要注意一下並查集的應用。

#include<algorithm>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int l;
    int r;
    int len;
    bool operator <(const node &x)const
    {
        if(len<x.len)
            return true;
        else
            return false;
    }
}a[1000];
int F[30];   //i點的父節點
int findfa(int x)     //查找最先的父節點,既是根節點
{
    if(x!=F[x])
        return F[x]=findfa(F[x]);
    return F[x];
}
int main()
{
    int n,nn,len,nown;
    char s[5];
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=1;i<=40;i++)
            F[i]=i;
        node now,next;
        int Len=0;
        for(int i=1;i<n;i++)
        {
            scanf("%s%d",s,&nn);
            now.l=s[0]-'A'+1;
            while(nn--)
            {
                scanf("%s%d",s,&len);
                now.r=s[0]-'A'+1;
                now.len=len;
                a[Len++]=now;
            }
        }
        sort(a,a+Len);
        int ans=0;

        for(int i=0;i<Len;i++)
        {
            now=a[i];
            int dx=findfa(now.l);
            int dy=findfa(now.r);
            if(dx!=dy)   //如果dx=dy,說明有相同的一條邊的兩個端點有相同的父節點,會出現環,則不加這條邊
            {
                F[dx]=dy;   //合併兩個子集合
                ans+=now.len;
            }
        }
        printf("%d\n",ans);
    }
return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章