C - 最小生成树 HDU - 3371

1.题目
C - 最小生成树 HDU - 3371
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.
Input
The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
Sample Output
1

2.题目大意
某地发洪水,导致某些城市被淹而消失,现在想把剩下的零散的城市通过修路连接起来,已知现在有部分城市是连通的(最后一行输入就是)。可选择修的路有m条,城市总共有n个,给出了m条路的起点终点和修路花费,问最少可花多少钱能保证所有的城市连通。

3.解题思路
A.首先是建图,把每个点到另外一个点修路所需要的花费给确定,graph[a][b] = graph[b][a] = cost; 然后已经联通的两个点花费就为0,无关的点就赋给一个特定的值。
B.然后用prime求出最少的花费就可以了

4.prime

最小生成树的prime算法是以点为核心来进行计算.
原理:从一个点开始遍历它到其他所有点的距离,无法直接到达的距离为INF,记录为mindis数组
如:一共有四个点ABCD 五条边
A——B   2;    A——C   1;        A——D   3;        B——C   4;        C——D   2;
选取A为第一个点,那么mindis数组中的值为0,2,1,3;并将A标记为已经访问过的点;
然后从未访问过的点中找最小的值加入生成树,即此时mindis数组的最小值为1(即A——C的距离最短),为C点,将C加入生成树(即标记C已经访问),并将sum加上权值1;
现在生成树中有两个点A和C,更新mindis数组,即看C到各个点的值与原来mindis数组中的值哪一个小,此时C——D的权值为2,mindis中到D的权值为3,所以更新mindis,更新后为0,2,1,2;
重复上述步骤直到访问所有点
原文链接:https://blog.csdn.net/lalala445566/article/details/80069746

博客学习https://blog.csdn.net/aa793336532/article/details/71680311?utm_source=distribute.pc_relevant.none-task

5.AC代码

#include 
#include
#include
#include
using namespace std;
#define maxn 555
#define maxint 1005
int pri[maxn][maxn], d[maxn];
int n;

void  prime()
{
    int i, j, k;
    int lowcost[maxn];
    lowcost[1] = 1;
    memset(lowcost,0,sizeof(lowcost));
    int min1,minid,sum=0;
    for( i = 1; i<n; i++)
    {
        min1 = maxint;
        for(j = 1; j<=n; j++)
        {
            if(!lowcost[j] && pri[1][j] <min1) //找到未标记的点中的最小值
            {
                min1 = pri[1][j]; 
                minid = j;
            }
        }
        if(min1 == maxint)
            break;
        sum+=min1;
        lowcost[minid] = 1;//已经用过了,就标记
        for(j  = 1; j <=n;  j++)
        {
            if(!lowcost[j] && pri[1][j] > pri[minid][j]) //更新
            {
                pri[1][j] = pri[minid][j];
            }
        }
    }
    if(min1 == maxint)
        printf("-1\n");
    else
  printf("%d\n",sum);
}
int main()
{
    int T;
    scanf("%d", &T);
    while( T-- )
    {
        int m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i = 1; i<=n; i++)//初始化图
        {
            for(int j = 1; j <= n; j++)
            {
                pri[i][j] = pri[j][i] = maxint; //标记,消耗是不可能超过这个值的
            }
        }
        for(int i = 0; i< m; i++ )
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(pri[a][b] > c)
            {
                pri[a][b] = pri[b][a] = c;//a到b和b到a的路消耗c
            }
        }
        for(int i = 0; i<k; i++) //已经联通了的点
        {
            int t,j,a,s;
            scanf("%d%d",&t,&a);
            for(j = 0; j<t-1; j++ )
            {scanf("%d",&s);
                pri[a][s] = pri[s][a] =0;
            }
        }
        prime();
    }
    return 0;
}

你可能感兴趣的:(C - 最小生成树 HDU - 3371)