当前位置:网站首页>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;
}
边栏推荐
- Guest brush SQL - 2
- 【FiddlerScript】利用FiddlerScript抓包保利威下载
- 点餐系统数据库设计--SQL Server
- 13 - JUC CountDownLatch concurrent programming
- Dbeaver connect the MySQL database and error Connection refusedconnect processing
- 监听父元素宽高,自适应插件大小
- ORACLE modify another user package (package)
- Matlab simulink particle swarm optimization fuzzy pid control motor pump
- 上课作业(7)——#598. 取余运算(mod)
- matlab wind speed model wavelet filtering
猜你喜欢
随机推荐
JVM: Runtime Data Area - PC Register (Program Counter)
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
Dart exception details
Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
05-SDRAM: Arbitration
小白的0基础教程SQL: 关系数据库概述 02
头歌MySQL数据库实训答案 有目录
特别数的和
Fist game copyright-free music download, League of Legends copyright-free music, can be used for video creation, live broadcast
leetcode125 Verify palindrome string
信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
crypto-js使用
Matlab simulink particle swarm optimization fuzzy pid control motor pump
The Bean's life cycle
Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)
Dbeaver connect the MySQL database and error Connection refusedconnect processing
Vim简介
2022杭电多校第二场1011 DOS Card(线段树)
weight distribution
WebSocket implements chat function