HDU-1102 Constructing Roads(最小生成树[Prim])
来源:互联网 发布:js判断app安卓版本号 编辑:程序博客网 时间:2024/05/17 01:03
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)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
邻接矩阵的prim和Dijkstra真的好像,都一样简洁易懂
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;int n,u,cnt,ans,vis[105],dis[105],g[105][105];//dis[i]表示点i到最小生成树的距离void Prim() { int i; memset(vis,false,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); dis[1]=ans=0,dis[0]=INF; while(true) { u=0; for(i=1;i<=n;++i) if(!vis[i]&&(dis[i]<dis[u])) u=i; if(u==0) return ; vis[u]=true; ans+=dis[u]; for(i=1;i<=n;++i) dis[i]=min(dis[i],g[u][i]); }}int main() { int i,j,s,e,q; while(scanf("%d",&n)==1) { for(i=1;i<=n;++i) for(j=1;j<=n;++j) scanf("%d",&g[i][j]); scanf("%d",&q); while(q--) { scanf("%d%d",&s,&e); g[s][e]=g[e][s]=0;//如果该边已经存在,则其权值置为0 } Prim(); printf("%d\n",ans); } return 0;}
0 0
- HDU - 1102 - Constructing Roads (最小生成树--prim算法!!)
- HDU 1102 Constructing Roads(Prim求最小生成树)
- HDU-1102 Constructing Roads(最小生成树[Prim])
- HDU 1102 Constructing Roads(最小生成树-Prim)
- hdu 1102 Constructing Roads(最小生成树,prim)
- HDU 1102 Constructing Roads(prim求最小生成树)
- HDU 1102 Constructing Roads (最小生成树 Prim算法)
- hdu 1102 Constructing Roads(Prim最小生成树)
- 【最小生成树+Prim】杭电 hdu 1102 Constructing Roads
- hdu-1102 Constructing Roads(prim最小生成树)
- hdu 1102 Constructing Roads(最小生成树 Prim)
- hdu 1102 Constructing Roads(最小生成树prim)
- hdu 1102 Constructing Roads 最小生成树prim模板题
- HDOJ题目1102Constructing Roads(最小生成树,prim)
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads - HDU 1102 最小生成树
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads(HDU 1102 最小生成树)
- 希尔排序
- Dubbo简介及实例
- C# VS 2010 创建、安装、调试 windows服务(windows service)
- apache的https访问配置
- ToggleButton
- HDU-1102 Constructing Roads(最小生成树[Prim])
- Android笔记(二)LogCat
- cocos2d-x 3.7 C++ 使用lua做脚本
- Android自定义布局
- 阅读方法
- 求一个整数的所有位相加之和
- 黑马程序员---java基础之集合Set
- Redis在windows下的安装使用
- ACM_阶段性总结 ACM_动态规划(DP)