当前位置:网站首页>POJ2421道路建设题解
POJ2421道路建设题解
2022-08-01 07:05:00 【bj_hacker】
题目
链接
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<cstdio>
#include<string.h>
#include<cstring>
#include<cmath>
#include<algorithm>
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;
}
边栏推荐
猜你喜欢

自制一款远程控制软件——VeryControl

Introduction to the basic principles, implementation and problem solving of crawler

数据机构----线性表之单向链表

datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法

Motion analysis and parameter optimization of crank-slider mechanism

Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers

I have three degrees, and I have five faces. I was "confessed" by the interviewer, and I got an offer of 33*15.

Golang:go模版引擎的使用

戴尔PowerEdge服务器R450 RAID配置步骤

配置我的kitty
随机推荐
【南瓜书ML】(task4)神经网络中的数学推导(更新ing)
【FiddlerScript】利用FiddlerScript抓包保利威下载
2022.7.27 Selected lectures on good topics
LeetCode240+312+394
Srping中bean的生命周期
Practical training Navicat Chinese and English mode switching
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
Vim三种模式
leetcode125 Verify palindrome string
配置我的kitty
Go 支持 OOP: 用 struct 代替 class
13 - JUC CountDownLatch concurrent programming
Using FiddlerScript caught poly FiddlerScript 】 【 download
图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
uva10825
响应式织梦模板园林景观类网站
JVM:运行时数据区-PC寄存器(程序计数器)
dbeaver连接MySQL数据库及错误Connection refusedconnect处理
Leetcode第 304 场周赛
电磁兼容简明教程(6)测试项目