hdu1102(Constructing Roads)--prim最小生成树

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14545    Accepted Submission(s): 5521


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
 

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
 

Sample Input
  
  
3 0 990 692 990 0 179 692 179 0 1 1 2
 

Sample Output

  
  
179

prim最小生成树的基本算法:首先以一个结点作为最小生成树的初始结点,然后以迭代的方式找出与最小生成树中各结点权重最小边,并加入到最小生成树中,选择一条权值最小,且一端点A已加入生成树,另一端点B在剩余结点集合内的边作为最小生成树上的边,同时将B列入生成树的已有点集中。当所有结点都加入到最小生成树之后,就找出了连通图中的最小生成树。Prim算法复杂度时O(n^2),适合稠密图(稠密图的定义为边很多点相对较少,稀疏图则反之)

回到本题:本题其实是prim的模板题,其实题目就多了一些已有边而已,只要把已有边的两个结点的权值置为0即可,比如题目输入的1 2,就把a[1][2],和a[2][1]置为0下面是本题的代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 105;
#define inf 1000000000  //定义为无穷大,表示无法直接到达
int a[maxn][maxn];  //a[i][j]表示结点i到结点j的权值
bool vis[maxn]; //是否访问过
int dis[maxn];
int m;
int ans;
int prim(int n)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=m;i++)
        dis[i] = inf;   //初始化距离都为无穷大
    ans = 0;dis[1] = 0;
    for(int i=1;i<=m;i++)   //m*m次遍历
    {
        int tmp = inf,k = 0;
        for(int j=1;j<=m;j++)   //一直循环比较所有可能的大小,取最小值
        {
            if(!vis[j] && dis[j] <tmp)
            {
                tmp = dis[j];   //更新当前的最小值dis[j]等价于a[0][j]
                k = j;      //最小值的结点
            }
        }
        vis[k] = true;//把这个点加入集合中,标记为已经访问
        ans += tmp; //累加最小权值
        for(int j=1;j<=m;j++)   //更新权值
        {
            if(!vis[j] && dis[j] > a[k][j]) //如果要访问的结点没有访问过,且可以找到新的最小值就更新
            {
                dis[j] = a[k][j];   //k到j未被访问各点更新权值
            }
        }
    }
    return ans;
}
int main()
{
    int i,j,k,b,c;
    while(cin>>m)
    {

        for(i=1; i<=m; i++)
        {
            vis[i] = 0;//表示没有访问过
            for(j=1; j<=m; j++)
            {
                cin>>a[i][j];
            }
        }
        cin>>k;
        for(i=1; i<=k; i++)
        {
            cin>>b>>c;
            a[b][c] = a[c][b] = 0;
        }
        cout<<prim(m)<<endl;
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值