Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.
Sample Input

1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0

Sample Output

0
17
16
26

题意:
输入城市数、边数
输入两城市名及边长
输出连通各图的最短路
思路:
prim:
每次选取所能抵达点的最小边,同时更新新选取的顶点到其他点的权值,若出现更小路径则更新路径

//prim
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int Maxn = 105;
const int INF = 0x3f3f3f3f;

int Map[Maxn][Maxn],dis[Maxn];//Map地图  dis所连接顶点的权值 
bool vis[Maxn];//判断是否已选取
int n,m; //n顶点数  m边数

void prim()
{
    int mi,v;
    for(int i=0; i<n; i++)
    {
        dis[i] = Map[0][i];//初始化起始点到各顶点的权值
        vis[i] = false;
    }
    for(int j=1 ; j<=n ; j++)//n个顶点
    {
        mi = INF;
        for(int i=0; i<n; i++)
        {
            if(!vis[i] && (dis[i]<mi))//若没选取过,且是目前的最小边
            {
                mi = dis[i];//记录最小边
                v = i;//记录该顶点
            }
        }
        vis[v] = true;//将该点设为已选取
        for(int i=0; i<n; i++)//更新新加入点到其余点的最小距离
        {
            if(!vis[i] && dis[i]>Map[v][i])//若未选取且生成了最小边
            {            //Map[v][i]为新加入顶点到其余点的最小距离
                dis[i] = Map[v][i];//更新最小边
            }
        }
    }
    for(int i=1 ; i<n; i++)//计算所选最小路径和 即结果
    {
        dis[0]+=dis[i];
    }
    cout<<dis[0]<<endl;
}

int main()
{
    while(cin>>n)
    {
        if(n==0)    //城市数为0 结束 
            break;
        cin>>m;
        if(m==0)
        {
            cout<<0<<endl;
            continue;
        }
        int x,y,d;
        for(int j=0; j<n; j++)    //初始化地图 
            for(int i=0; i<n; i++)    //无向图
            {
                if(i==j)
                    Map[j][i] = 0;//自己到自己权值为0
                else
                    Map[j][i]=INF;
            }
        for(int i=0; i<m; i++)
        {
            cin>>x>>y>>d;
            if(Map[x-1][y-1]>d)    //无向图,更新最短路 若出现相同路径选取权值最小边
                Map[x-1][y-1] = Map[y-1][x-1] = min(d,Map[x-1][y-1]);
        }
        prim();
    }
    return 0;
}

kruskal:
将所有边从小到大排序,每次选取所有边中的最小边,并将两顶点置入同一区域内,若两顶点已在同一区域内,则不选取

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Maxn = 10001;
const int INF = 0x3f3f3f3f;
int u[Maxn],v[Maxn],w[Maxn],r[Maxn];//u顶点1 v顶点2 w权值
int p[Maxn];//记录父节点
int n,m;//  n顶点数  m边数

int find(int x)//找父节点
{
    return p[x]==x?x:find(p[x]);
}

int cmp(const int i,const int j)//根据权值从小到大排序
{
    return w[i]<w[j];
}

int kruskal()
{
    int ans = 0;
    for(int i=0;i<n;i++)//初始化父节点,置为自己
    p[i]=i;
    for(int i=0;i<m;i++)//利用数组r判断最小边的顶点
    r[i]=i;
    sort(r,r+m,cmp);//对数组r按照权值大小进行从小到大的排序
    for(int i=0;i<m;i++)
    {
        int e=r[i];//这里所选取的r[]即为最小边的下标
        int x=find(u[e]);//找到两顶点的父节点
        int y=find(v[e]);
        if(x!=y)//若不为同一区域
        {
            ans += w[e];//记录最小边权值
            p[x]=y;//置入同一区域
        }
    }
    return ans;//返回结果
}

int main()
{
    while(cin>>n)
    {
        if(n==0)
        break;
        cin>>m;
        if(m==0)
        {
            cout<<0<<endl;
            continue;
        }
        for(int i=0;i<m;i++)
        {
            cin>>u[i]>>v[i]>>w[i];
        }
        cout<<kruskal()<<endl;
    }
    return 0;
}