HDOJ 1102 Constructing Roads.(Prim算法实现)
来源:互联网 发布:php全站搜索 编辑:程序博客网 时间:2024/05/23 15:45
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23961 Accepted Submission(s): 9199
Problem 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.
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.
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
30 990 692990 0 179692 179 011 2
Sample Output
179
Source
kicc
Recommend
Eddy | We have carefully selected several similar problems for you: 1301 1162 1875 1863 1325
题意:输入n,给出一个n*n的矩阵(从1到n编号)。第i行j列的元素表示城市i与j之间的距离。再给出Q,随后Q行表示两城市之间的道路已建成。求所有城市连通所需要建的最短长度。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<set>#include<queue>#include<stack>#include<vector>#include<algorithm>using namespace std;const int inf = 1<<30;int maz[105][105],dis[105],n;bool flag[105];void prim(){ int i,j,pos,t; for(i=1;i<=n;i++) dis[i] = inf; for(i=1;i<=n;i++) flag[i] = false; //标记是否访问 dis[1] = 0; for(i=1;i<=n;i++){ t =inf; for(j=1;j<=n;j++){ //找当前节点最短路 if(!flag[j]&&dis[j]<t){ t = dis[j]; pos = j; //记录最短路下标 } } flag[pos] = true; //不再访问 for(j=1;j<=n;j++){ if(!flag[j]&&dis[j]>maz[pos][j]){ dis[j] = maz[pos][j]; //将当前最短路以外的路长都改为最短长度 } } } int mount = 0; for(i=1;i<=n;i++) mount += dis[i]; cout<<mount<<endl;}int main(){ int i,j; while(cin>>n){ //题目没说有多组数据 因为这个贡献了好几个WA... QAQ for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ scanf("%d",&maz[i][j]); //建图 } } int m,a,b; cin>>m; while(m--){ scanf("%d%d",&a,&b); maz[a][b] = maz[b][a] =0; //将已建成公路造价(长度)改为0 } prim(); }}
阅读全文
1 0
- HDOJ 1102 Constructing Roads.(Prim算法实现)
- hdoj 1102 Constructing Roads (prim)
- Prim 算法, hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads(Prim算法)
- HDOJ题目1102Constructing Roads(最小生成树,prim)
- HDOJ 1102 Constructing Roads 最小生成树 kruskal && Prim
- hdoj 1102 Constructing Roads 【最小生成树&&prim】
- HDOJ-1102-Constructing Roads
- HDOJ 1102 Constructing Roads
- HDU 1102 Constructing Roads(prim)
- HDU - 1102 - Constructing Roads (最小生成树--prim算法!!)
- HDU 1102 Constructing Roads (最小生成树 Prim算法)
- hdu/hdoj 1102 Constructing Roads
- ACM HDOJ 1102 (Constructing Roads)
- HDOJ HDU 1102 Constructing Roads
- hdu 1102 Constructing Roads (prim 、kruskal)
- HDU-#1102 Constructing Roads(Prim & Kruskal)
- HDU 1102:Constructing Roads【Kruskal & Prim】
- VC++ 使用预编译头
- 二分查找
- linux软件安装方式分析
- [工作流与Activiti]
- 操作系统复习-5.2 I/O核心子系统
- HDOJ 1102 Constructing Roads.(Prim算法实现)
- 常见文件的mime类型
- Unity脚本编译顺序
- Ubuntu查看版本信息
- 菲波那切数列
- Java多线程同步:生产者与消费者
- Mysql重启主从同步功能
- Ubuntu用户循环登录
- SPI总线协议介绍