• POJ2421道路建设题解


    题目

    链接

    http://poj.org/problem?id=2421

    字面描述

    Constructing Roads
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 36942 Accepted: 16631
    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
    Source

    PKU Monthly,kicc

    重点处理

    对于已经加好的路径边权赋值为0

    代码实现

    #include
    #include
    #include
    #include
    #include
    using namespace std;
    
    const int maxn=100+10;
    const int inf=2e9;
    int n,ans,m;
    int lowcost[maxn];
    int c[maxn][maxn];
    bool vis[maxn];
    inline void Prim(){
    	//初始化 
    	memset(vis,false,sizeof(vis));
    	ans=0;
    	vis[1]=true;
    	for(int i=1;i<=n;i++)lowcost[i]=c[1][i];
    	//n-1条边的添加 
    	for(int i=1;i<n;i++){
    		int temp=inf;
    		int t=1;
    		//寻找离已添加最近的节点t 
    		for(int j=1;j<=n;j++){
    			if(!vis[j]&&lowcost[j]<temp){
    				temp=lowcost[j];
    				t=j; 
    			}
    		}
    		ans+=temp;
    		if(t==1)break;
    		vis[t]=true;
    		//更新未添加节点 
    		for(int j=1;j<=n;j++){
    			if(!vis[j]&&c[t][j]<lowcost[j])lowcost[j]=c[t][j];
    		}
    	}
    } 
    int main(){
    	scanf("%d",&n);
    	memset(c,0x3f,sizeof(c));
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++)scanf("%d",&c[i][j]); 
    	}
    	scanf("%d",&m);
    	for(int i=1;i<=m;i++){
    		int u,v;
    		scanf("%d%d",&u,&v);
    		c[u][v]=0;
    		c[v][u]=0;
    	}
    	Prim();
    	printf("%d\n",ans);
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    Java知识点--IO流(上)
    C++模板编程(6)---实际运用模板:分离式模型(Separation Model)
    js数组介绍:创建、length的用法、冒泡排序法、选择排序法
    报道 | 国内外运筹优化会议精选
    低代码平台协同OA升级,促进金融企业信息化建设
    Redis-使用java代码操作Redis
    手搓js轮播图_JavaScript进阶
    数仓埋点体系与归因实践
    docker引擎学习
    Typescript学习
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/126034565