• 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
  • 相关阅读:
    利用 mviews on prebuilt table 进行增量刷新数据
    事务管理 vs. 锁控制:你真的分得清吗?何时使用何种并发控制策略?
    YUV与RGB 以及之间的转换
    【Python Web】Flask框架(九)MYSQL+python案例
    Vulnhub | DC: 8 |【实战】
    几个好用的测试HTTP请求的网站
    cad怎么转成pdf文件?3个方法教你实现cad转pdf
    python中的数据分析(juypter)
    虚拟化运维中:为什么对网络流量监控这么重要?
    分组密码的模式
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/126034565