HDOJ 1102 prim
来源:互联网 发布:小米5开4g网络 编辑:程序博客网 时间:2024/05/11 22:08
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17875 Accepted Submission(s): 6799
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思路:裸的最小生成树,只不过题目中有建好的道路,就看做是Map[a][b]=Map[b][a]=0; 即可#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#define inf 0x3f3f3f3fusing namespace std;int Map[110][110],dis[110];bool vis[110],bj[110][110];int n;int s;void prim(){ int i,j,ma,po; for(i=1;i<=n;i++) { dis[i]=Map[1][i]; } vis[1]=true; s=0; for(i=1;i<=n-1;i++) { ma=inf; for(j=1;j<=n;j++) { if(dis[j]<ma&&!vis[j]) { ma=dis[j]; po=j; } } s+=ma; vis[po]=true; for(j=1;j<=n;j++) { if( ( !vis[j])&&(dis[j]>Map[po][j]) ) { dis[j]=Map[po][j]; } } }}int main(){ int m,i,j; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&Map[i][j]); } } int li; memset(vis,false,sizeof(vis)); scanf("%d",&li); int a,b; while(li--) { scanf("%d%d",&a,&b); Map[b][a]=Map[a][b]=0; } prim(); printf("%d\n",s); } return 0;}
0 0
- HDOJ 1102 prim
- [c]HDOJ 1102 prim算法的应用
- hdoj 1102 Constructing Roads (prim)
- HDOJ 1102 Constructing Roads.(Prim算法实现)
- HDOJ题目1102Constructing Roads(最小生成树,prim)
- HDOJ 1102 Constructing Roads 最小生成树 kruskal && Prim
- hdoj 1102 Constructing Roads 【最小生成树&&prim】
- HDOJ——1233(Kruskal+prim)
- HDoj-1233-还是畅通工程-prim算法
- HDOJ还是畅通工程之--prim算法
- hdoj 畅通工程再续prim
- hdoj 1875 畅通工程再续 (prim )
- HDOJ 1863:畅通工程 Prim算法求解最小生成树
- hdoj 3080 The plan of city rebuild(prim)
- HDOJ-1875(最小生成树Prim算法)
- HDOJ 1233 还是畅通工程 【最小生成树】+【prim】
- HDoj-1875-畅通工程再续-prim算法
- HDOJ 题目1301 Jungle Roads(最小生成树,prim)
- Monitoring with Ganglia
- LeetCode(46) Lowest Common Ancestor of a Binary (Search) Tree
- 锚点和空链接
- spring中bean的scope
- transient:修饰符的使用
- HDOJ 1102 prim
- JavaBean使用示例
- 使用FlexPaper实现PDF文件在线预览
- 文献综述格式及写作技巧
- MySQL 常用命令
- 解决Android单个dex文件不能超过65536个方法问题
- C++ 多线程
- Android SDK 源码关联
- PAT(甲级)1005