您的位置:首页 > 其它

HDU 1102 Constructing Roads

2015-08-11 21:50 501 查看

Constructing Roads

[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 17218 Accepted Submission(s): 6542

[/b]

[align=left]Problem Description[/align]
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.



[align=left]Input[/align]
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.



[align=left]Output[/align]
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.



[align=left]Sample Input[/align]

3
0 990 692
990 0 179
692 179 0
1
1 2


[align=left]Sample Output[/align]

179


[align=left]Source[/align]
kicc

题意:

给出一个数n,代表村庄的数目,然后输入n行,每行n个数,分别代表第i行到第j列的村庄的距离,然后再输入一个数m,代表已经有的道路的个数,然后输入m行代表已经有道路的村庄,让求需要建的最短的路!

思路:

就是将点之间的距离与两个点联系起来,也就是从边出发进行考虑,然后现将建好的路连接在一起,然后再考虑没有建的路,用最小生成树的方法就能解决了!(用到的工具:并查集!)

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int n,m;
int d[105][105];
struct node
{
int u,v;
int w;
}p[10005];
int pre[105];
void init()
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
int r=x;
while(r!=pre[r])
{
r=pre[r];
}
int i,j;
i=x;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
int join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return 1;
}
return 0;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=1;i<=n;i++)//输入距离
{
for(int j=1;j<=n;j++)
scanf("%d",&d[i][j]);
}
scanf("%d",&m);
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
join(a,b);
}
int t=0;
for(int i=1;i<=n;i++)//这道题的核心!将边的端点和距离联系起来!
{
for(int j=1;j<=n;j++)//放到结构体里面!
{
if(i!=j)
{
t++;
p[t].u=i;
p[t].v=j;
p[t].w=d[i][j];
}
}
}
sort(p+1,p+t+1,cmp);
int sum=0;
for(int i=1;i<=t;i++)
{
if(join(p[i].u,p[i].v))
{
sum+=p[i].w;
}
}
printf("%d\n",sum);
}
return 0;
}


方法2:

prim算法:

代码

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define N 105
int n,m;
int d

;
int vis
;
int dis
;
void init()
{
int a,b;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&d[i][j]);
}

scanf("%d",&m);
for(int i=1;i<=m;i++)//	这一点只能够说明是a与b之间已经连好,但是不能说明a与b与其他的路也连接好了!
{
scanf("%d%d",&a,&b);//例如:a连b,c连d,但是这四个并没有进一个集合里面!
d[a][b]= d[b][a] = 0;
}
vis[1]=1;
for(int i=1;i<=n;i++)
{
dis[i]=d[1][i];
}
dis[1]=0;
}
void prim()
{
int min,t;
for(int i=1;i<n;i++)
{
min=INF;t=1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&(dis[j]<min))
{
min=dis[j];
t=j;
}
}
vis[t]=1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&(dis[j]>d[t][j]))
{
dis[j]=d[t][j];
}
}
}
int sum=0;
for(int i=1;i<=n;i++)
sum+=dis[i];
printf("%d\n",sum);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
prim();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: