题目链接:http://poj.org/problem?id=1251
因为博主太菜了,不会发布图片,所以就只能发链接了,/泪奔

题目描述(ps:因为没有图片,将就着看一下,如果需要看完整提议,请点击上面的链接 /可怜):
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.
Output

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
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

题目大意为,在一个小岛上有一些村庄(数量为n),村庄之间有一些道路,我们可以通过这些道路到达任意一个村庄,但是修缮这些道路是很消费钱的,所以我们需要找到一些道路,让人们可以通过这些道路到达任意一个村庄,并且使得修缮这些道路的费用最低,这就是一个最小生成树的模板题,本题就是求一个该图的最小生成树,我们不介绍最小生成树的定义了。

最小生成树 锁定
本词条由“科普中国”科学百科词条编写与应用工作项目 审核 。
一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边。 [1]  最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里姆)算法求出。 [2] 

这是百度百科上对“最小生成树”的解释,接下来我们来看求最小生成树的两种算法:kruskal算法和prim算法,其实这两种算法都是采用的是贪心思想。

kruskal
kruskal算法是将所以的边都根据权值排序,每次选择最短的边(这点可以参考求最短路的dijskra算法),然后将这条边加入最小生成树中,但是在加入这条边之前会有一个问题,因为我们是通过加入边的方式寻找最小生成树,所以我们需要考虑这条边两边的节点是否在连通这条边之前就已经连通了,如果已经连通了,那么我们就不需要在添加这条边,否则就加入这条边,怎么去判断两个点是否已经连通了呢?这当然要用到并查集了,如果不知道并查集的同学,可以看我之前的博客,这里就不叙述了,因为要连通一个含有n个节点的图,需要n-1条边所以我们需要选择n-1条边。
代码:

#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
struct edge
{
    int v,w;
    int u;
    edge(int u,int v,int w):u(u),v(v),w(w){}
};

int n;
vector e[30];
vector t;
int vis[30];
int dis[30];

//库鲁斯
int f[31];
int findfa(int x)
{
    return f[x]=f[x]==x?x:findfa(f[x]);
}

bool cmp(edge x,edge y)
{
    return x.w<y.w;
    //对边进行排序,这样每次选择的都是最小边
}
ll kulus()
{
    for(int i=0;i<n;i++){
        f[i]=i;
    }
    int len=t.size();
    int i=0,j=1;
    //i记录寻找到第几条边
    //j记录最小生成树中即将加入第几条边
    ll ans=0;
    sort(t.begin(),t.end(),cmp);
    while(i<len && j<n){//
        int u=t[i].u,v=t[i].v,w=t[i].w;
        //处理第i条边
        int tu=findfa(u),tv=findfa(v);
        //判断这条边两边的点是否已经连通
        if(tu!=tv){
        //没有联通就将这条边加入最小生成树
            f[tv]=tu;
            j++;
            ans+=w;
        }
        i++;
    }
    return ans;
}

int main()
{
    while(cin>>n,n){
        for(int i=0;i<n;i++){
            e[i].clear();
        }
        t.clear();
        for(int i=1;i<n;i++){
            char op;
            int k;
            cin>>op>>k;
            for(int i=0;i<k;i++){
                char op1;
                int x;
                cin>>op1>>x;
                e[op-'A'].push_back(edge(op-'A',op1-'A',x));
                e[op1-'A'].push_back(edge(op1-'A',op-'A',x));
                t.push_back(edge(op-'A',op1-'A',x));
            }
        }
        ll ans;
        //ans=prime(0);
        //cout<<ans<<endl;
        ans=kulus();
        cout<<ans<<endl;
    }
    return 0;
}

prim算法
prim算法也是采用的贪心思想,与kruskal算法不同的是,kruskal算法是遍历边,从小到大选择边加入最小生成树,而prim算法是先选择一个点加入最小生成树,然后选择离最小生成树中的点距离最小的点加入最小生成树,但是在加入一个点之后可能会对其他点到最小生成树中的点的最小距离长生影响,所以在加入一个点之后就需要更新一下最小距离,保证其他点到最小生成树中的点的距离是最小距离,然后重复上诉操作就行了,因为prim算法是寻找点,所以需要寻找到n个点,但是我们最开始已经加入了一个点,所以我们还需要循环n-1次。
代码:

#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
struct edge
{
    int v,w;
    int u;
    edge(int u,int v,int w):u(u),v(v),w(w){}
};

int n;
vector e[30];
vector t;
int vis[30];
int dis[30];
//prime
ll prime(int s)
{
    ll ans=0;
    memset(vis,0,sizeof vis);
    //所有点都没有加入最小生成树
    memset(dis,inf,sizeof dis);
    //所有点到最小生成树中的距离为无穷大
    for(int i=0;i<e[s].size();i++){
        int v=e[s][i].v,w=e[s][i].w;
        dis[v]=w;
        //将点s加入最小生成树,并且更新距离
    }
    vis[s]=1;
    //标记s点
    for(int i=1;i<n;i++){
        int minn=inf,u=-1;
        for(int j=0;j<=n;j++){//寻找离最小生成树最短的点
            if(!vis[j] && dis[j]<minn){
                minn=dis[j];
                u=j;
            }
        }
        vis[u]=1;//标记点u
        ans+=minn;//最小生成树的总权值加minn
        for(int j=0;j<e[u].size();j++){
            int v=e[u][j].v,w=e[u][j].w;
            //更新其他点到最小生成树中的距离
         //因为可能其他点到刚加入最小生成树的点的距离更小
            dis[v]=min(dis[v],w);
        }
    }
    return ans;
}

int main()
{
    while(cin>>n,n){
        for(int i=0;i<n;i++){
            e[i].clear();
        }
        t.clear();
        for(int i=1;i<n;i++){
            char op;
            int k;
            cin>>op>>k;
            for(int i=0;i<k;i++){
                char op1;
                int x;
                cin>>op1>>x;
                e[op-'A'].push_back(edge(op-'A',op1-'A',x));
                e[op1-'A'].push_back(edge(op1-'A',op-'A',x));
                t.push_back(edge(op-'A',op1-'A',x));
            }
        }
        ll ans;
        //ans=prime(0);
        //cout<<ans<<endl;
        ans=kulus();
        cout<<ans<<endl;
    }
    return 0;
}