hdoj 1102 Constructing Roads 【最小生成树&&prim】
来源:互联网 发布:淘宝商品销售方案 编辑:程序博客网 时间:2024/04/27 15:40
Constructing Roads
d
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 74 Accepted Submission(s) : 41
Problem Description
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#define maxn 1011using namespace std;const int inf=0x3f3f3f3f;int map[maxn][maxn];int dis[maxn];int vis[maxn];int n;int sum;void prim(){int mn,v;memset(vis,0,sizeof(vis));//memset(dis,inf,sizeof(dis));for(int i=1;i<=n;i++)dis[i]=map[1][i];vis[1]=1;//dis[1]=0;for(int i=1;i<n;i++){mn=inf;for(int j=1;j<=n;j++)if(!vis[j]&&dis[j]<mn)mn=dis[v=j];vis[v]=1;sum+=mn;if(mn==inf)break;for(int j=1;j<=n;j++)if(!vis[j]&&dis[j]>map[v][j])dis[j]=map[j][v];}//dis[1]=0;}int main(){while(scanf("%d",&n)!=EOF){memset(map,inf,sizeof(map));for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){scanf("%d",&map[i][j]);//if(i==j)//map[i][i]=inf; }}int m;int x,y;scanf("%d",&m);while(m--){scanf("%d%d",&x,&y);map[x][y]=map[y][x]=0;}sum=0;prim();printf("%d\n",sum);}return 0;}
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.
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
代码:
0 0
- HDOJ题目1102Constructing Roads(最小生成树,prim)
- HDOJ 1102 Constructing Roads 最小生成树 kruskal && Prim
- hdoj 1102 Constructing Roads 【最小生成树&&prim】
- HDOJ 1102 Constructing Roads(最小生成树)
- hdoj 1102 Constructing Roads 【最小生成树】
- hdoj-1102-Constructing Roads【最小生成树】
- hdoj 1102 Constructing Roads 【最小生成树】
- HDOJ 1102 Constructing Roads(最小生成树)
- POJ2421 Constructing Roads [最小生成树 prim]
- hdu1102 Constructing Roads 最小生成树+prim
- hdu1102 Constructing Roads 最小生成树Prim
- hdu1102 Constructing Roads 最小生成树Prim
- HDU1102 Constructing Roads 【最小生成树Prim】
- hdu1102(Constructing Roads)--prim最小生成树
- hdu1102 Constructing Roads--最小生成树prim
- POJ2421 Constructing Roads(最小生成树,Prim)
- hdu 1102 Constructing Roads(Prim最小生成树)
- 【最小生成树+Prim】杭电 hdu 1102 Constructing Roads
- 【图像处理笔记】平滑空间滤波器
- hdu 5461 Largest Point (2015 ACM/ICPC Asia Regional Shenyang Online)
- PHP
- Android 利用addView 动态给Activity添加View组件
- 第三周项目一 顺序表的基本运算(2)
- hdoj 1102 Constructing Roads 【最小生成树&&prim】
- static静态变量的理解
- iptables详解
- 查找所有字符的位置
- ViewGroup
- web.xml 中的listener、 filter、servlet 加载顺序
- This version of the rendering library is more recent than your version of ADT plug-in. Please updat
- 电商网站多语言架构
- PING用一生诠释了TCP/IP------二三层转发为PING劈荆斩棘(转载)