hdu 1102(最小生成树)
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19579 Accepted Submission(s): 7474
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.
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.
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.
0 990 692
990 0 179
692 179 0
1
1 2
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; const int N = ;
const int INF = ;
int graph[N][N];
int low[N];
bool vis[N]; int prim(int n,int start){
memset(vis,false,sizeof(vis));
memset(low,false,sizeof(low));
int pos = start,cost=;
vis[pos]=true;
for(int i=;i<=n;i++){
low[i] = graph[pos][i];
}
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]<Min){
pos = j,Min = low[j];
}
}
vis[pos]=true;
cost+=Min;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]>graph[pos][j]) low[j] = graph[pos][j];
}
}
return cost;
} int main(){
int n,m;
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
scanf("%d",&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
graph[a][b]=graph[b][a] = ;
}
printf("%d\n",prim(n,));
}
}
hdu 1102(最小生成树)的更多相关文章
- HDU 1102 最小生成树裸题,kruskal,prim
1.HDU 1102 Constructing Roads 最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- 图论问题(2) : hdu 1102
题目转自hdu 1102,题目传送门 题目大意: 输入一个n*n的邻接矩阵,其中i行j列代表从i到j的路径的长度 然后又m条路已经帮你修好了,求最短要修多长的路才能使所有村庄连接 不难看出,这道题就是 ...
- HDU 1233(最小生成树)
HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
随机推荐
- 关于Python、Java、C#语言的一些比较
不能说某某语言不好! 首先,千万别说某一个语言好不好,应为这样的用词是错的,我曾经在好多场合听到一些程序员说java好,.net不好这类的话. 其实语言不分好坏,只是在具体的某些领域或业务场景上不合适 ...
- centos使用--软件启动关闭等操作的命令
关于centos服务的命令经常会遇到这几种 service systemctl /etc/init.d/ 1 service和/etc/init.d/ service是一个脚本命令,分析service ...
- GTID环境中手动修复主从故障一例(Error 1146)
Preface In my last test of pt-heartbeat,both of master and slave were out of disk.And the my ...
- Python 爬虫-豆瓣读书
import requests from bs4 import BeautifulSoup def parse_html(num): headers = { 'User-Agent': 'Mozill ...
- [转]Docker学习笔记之一,搭建一个JAVA Tomcat运行环境
本文转自:http://www.blogjava.net/yongboy/archive/2013/12/12/407498.html 前言 Docker旨在提供一种应用程序的自动化部署解决方案,在 ...
- mininet、floodlight在第一次SDN上机作业中出现的一些问题
mininet.floodlight在第一次SND上机作业中出现的一些问题 首先给出链接 VMware安装 mininet安装 floodlight安装及问题,各个版本Ubuntu SDN第一次上机作 ...
- sql server获取后天距离某一日期还有多少周的写法
),,),'2012-10-18 00:00:00.000')
- 【转】TCP通信的三次握手和四次撒手的详细流程(顿悟)
TCP(Transmission Control Protocol) 传输控制协议 三次握手 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位 ...
- hdu 1426 Sudoku Killer (dfs)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- SpringBoot Redis序列化配置
Redis配置 #Redis spring.redis.host= spring.redis.port=6379 spring.redis.database=0 # Redis服务器连接密码(默认为空 ...