Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5227    Accepted Submission(s): 1896

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.

 
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

3 0 990 692 990 0 179 692 179 0 1 1 2
 
Sample Output

179
 

很简单的最小生成树,有两种做法,我采用Kruskal和Prim都试了一下。一种是使已经有的边距离为0,这样既能顺利地生成,又能不影响答案;第二种是合并已经有的两条边的集合。我这里kruskal采用的是第二种方法,Prim只能用第一种方法。

Kruskal Algorithm Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 107 int mp[N][N],fa[N],vis[N][N],n,res; struct Edge
{
int s,t,w;
}edge[N*N]; int cmp(Edge ka,Edge kb)
{
return ka.w < kb.w;
} int findset(int x)
{
if(x != fa[x])
fa[x] = findset(fa[x]);
return fa[x];
} void Kruskal()
{
int i,j,k = ,q,A,B;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
edge[k].s = i;
edge[k].t = j;
edge[k].w = mp[i][j];
k++;
}
}
sort(edge,edge+k,cmp);
for(i=;i<=n;i++)
fa[i] = i;
res = ;
scanf("%d",&q);
for(i=;i<q;i++)
{
scanf("%d%d",&A,&B);
int u = findset(A);
int v = findset(B);
fa[u] = v;
}
for(i=;i<k;i++)
{
int u = edge[i].s;
int v = edge[i].t;
int fx = findset(u);
int fy = findset(v);
if(fx != fy)
{
res += edge[i].w;
fa[fx] =fy;
}
}
} int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&mp[i][j]);
Kruskal();
printf("%d\n",res);
}
return ;
}

Prim Algorithm Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
using namespace std;
#define N 107 int mp[N][N],vis[N],n,res,len[N]; void Prim()
{
int i,j,k,mini;
res = ;
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
len[i] = mp[][i];
len[] = ;
vis[] = ;
for(i=;i<=n;i++)
{
mini = Mod;
for(j=;j<=n;j++)
{
if(!vis[j] && len[j] < mini)
{
mini = len[j];
k = j;
}
}
if(mini == Mod)
return;
res += len[k];
vis[k] = ;
for(j=;j<=n;j++)
{
if(!vis[j] && len[j] > mp[k][j])
len[j] = mp[k][j];
}
}
}
int main()
{
int i,j,q,u,v;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&mp[i][j]);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&u,&v);
mp[u][v] = mp[v][u] = ;
}
Prim();
printf("%d\n",res);
}
return ;
}

HDU 1102 Constructing Roads的更多相关文章

  1. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  2. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  3. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  4. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  5. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  6. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  7. HDU 1102 Constructing Roads(kruskal)

    Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...

  8. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  9. HDU 1102 Constructing Roads(最小生成树,基础题)

    注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...

随机推荐

  1. jQuery中的事件冒泡

    1.什么是冒泡 eg: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  2. Html==>>一些经典

    1.CSS overflow 属性 2.<input>标签 <input> 标签用于搜集用户信息. 1 type属性 根据不同的 type 属性值,输入字段拥有很多种形式.可以 ...

  3. 设计模式总结篇系列:命令模式(Command)

    在程序设计中,经常会遇到一个对象需要调用另外一个对象的某个方法以达到某种目的,在此场景中,存在两个角色:请求发出者和请求接收者.发出者发出请求,接收者接收请求并进行相应处理.有时候,当需要对请求发出者 ...

  4. Python tools for Visual Studio插件介绍

          Python tools for Visual Studio是一个免费开源的VisualStudio的插件,支持 VisualStudio 2010,2012与2013.我们想要实现的是: ...

  5. Android图像处理之Bitmap类

      Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保存图像文件.本文从应用的角度,着重介绍怎么用Bitmap来实现 ...

  6. 怎样在win7系统配置数据源

    1.点击桌面的我Windows 图标,找打控制面板 2.进入控制面板主页,选择系统和安全,进入系统和安全 3.进入系统和安全主页后选择管理工具,点击进入 4.进入管理工具后,选择数据源,进行数据源的配 ...

  7. 原型 prototype

    原型 prototype js 的对象比较 由于 js 是解释执行的语言, 那么再代码中出现函数与对象如果重复执行, 会创建多个副本 在代码中重复执行的代码容易出现重复的对象 创建一个 Person ...

  8. 初学Node(一)国际惯例HelloWorld

    简介 没有用过Node,记的这些只是学习的笔记,有什么错的地方,望各位前辈指正. Node是一个服务器端Javascript解释器,依赖于Chrome v8引擎进行代码编译,事件驱动.非阻塞I/O都是 ...

  9. SET UPDATE TASK LOCAL

    SET Effect Switches on the local update task. This means that when you specify CALL FUNCTION ... IN ...

  10. SharePoint 2013开发环境准备一些小事项

    开发慢慢向着SharePoint 2013迁移,环境也开始慢慢准备起来了.由于FAST Search的集成,和缓存服务的加入,SharePoint 2013对开发机器要求的不是一般的高.这里介绍一下如 ...