Constructing Roads

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 54   Accepted Submission(s) : 28

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Source

kicc


————————————————————————————————————
给出n个城市两两之间的花费,再给出几个城市已经建好的边,求让所有城市联通的最小花费

思路:最小生成树

Prim算法:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f
int mp[105][105];
int low[105];
int m,n; void prim(int x)
{
int sum=0;
for(int i=1;i<=n;i++)
{
low[i]=mp[x][i];
}
low[x]=-1;
for(int i=1;i<n;i++)
{
int mn=inf;
int v=-1;
for(int j=1;j<=n;j++)
{
if(low[j]!=-1&&low[j]<mn)
{
mn=low[j];
v=j;
}
}
if(v!=-1)
{
low[v]=-1;
sum+=mn;
for(int j=1;j<=n;j++)
{
if(low[j]!=-1&&mp[v][j]<low[j])
{
low[j]=mp[v][j];
}
}
}
}
printf("%d\n",sum);
} int main()
{
int u,v,c,k; while(~scanf("%d",&n)&&n)
{
memset(mp,0,sizeof(mp));
for(int i=1; i<=n; i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&c);
if(i==j)
continue;
else
mp[i][j]=mp[j][i]=c;
}
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=0;
}
prim(1);
}
return 0;
}

kruskal算法:

#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
#define LL long long struct node
{
int u,v,w;
} p[1000005];
int n,cnt,pre[1005]; bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<1005; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} void kruskal()
{
sort(p,p+cnt,cmp);
init();
int cost=0;
int ans=0;
for(int i=0; i<cnt; i++)
{
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
}
if(ans==n-1)
{
break;
}
}
printf("%d\n",cost);
} int main()
{
int mp[105][105],k,x,y;
while(~scanf("%d",&n)&&n)
{ for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d",&mp[i][j]);
cnt=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
p[cnt].u=i,p[cnt].v=j;
p[cnt++].w=mp[i][j];
}
scanf("%d",&k);
for(int i=0; i<k; i++)
{
scanf("%d%d",&x,&y);
p[cnt].u=x-1,p[cnt].v=y-1;
p[cnt++].w=0;
}
kruskal();
}
return 0;
}

HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏的更多相关文章

  1. ZOJ2256 Mincost 2017-04-16 19:36 44人阅读 评论(0) 收藏

    Mincost Time Limit: 2 Seconds      Memory Limit: 65536 KB The cost of taking a taxi in Hangzhou is n ...

  2. 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 83276 Accepted: 31159 Description Mich ...

  3. HDU6029 Happy Necklace 2017-05-07 19:11 45人阅读 评论(0) 收藏

    Happy Necklace                                                                           Time Limit: ...

  4. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  5. Python获取当前时间 分类: python 2014-11-08 19:02 132人阅读 评论(0) 收藏

    Python有专门的time模块可以供调用. <span style="font-size:14px;">import time print time.time()&l ...

  6. 浅谈IOS8之size class 分类: ios技术 2015-02-05 19:06 62人阅读 评论(0) 收藏

    文章目录 1. 简介 2. 实验 3. 实战 3.1. 修改 Constraints 3.2. 安装和卸载 Constraints 3.3. 安装和卸载 View 3.4. 其他 4. 后话 以前和安 ...

  7. HDU6026 Deleting Edges 2017-05-07 19:30 38人阅读 评论(0) 收藏

    Deleting Edges                                                                                  Time ...

  8. HDU6027 Easy Summation 2017-05-07 19:02 23人阅读 评论(0) 收藏

    Easy Summation                                                             Time Limit: 2000/1000 MS ...

  9. 团体程序设计天梯赛L2-009 抢红包 2017-03-22 19:18 131人阅读 评论(0) 收藏

    L2-009. 抢红包 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 没有人没抢过红包吧-- 这里给出N个人之间互相发红包.抢 ...

随机推荐

  1. Eclipse实用用快捷键

    1.ctrl+shift+o 添加必须import并删除无用import.代码被改动时容易产生很多无用引用,此时这个快捷键就可以一次把如下的引用删掉了

  2. 用JavaScript解决Placeholder的IE8兼容问题

    <script type="text/javascript"> if( !('placeholder' in document.createElement('input ...

  3. String..lastIndexOf(".") 返回-1的思考

    String s = tableName.substring(tableName.lastIndexOf(".") + 1); 如果有tableName有'.',那么返回正确的截取 ...

  4. log4net 极简配置

    log4net的配置详解 分类: C#2013-10-01 22:45 5335人阅读 评论(4) 收藏 举报 log4net日志框架         前言:没买到1号回家的票,所以在祖国64岁生日之 ...

  5. [转]Web前端浏览器兼容

    转自: http://www.admin10000.com/document/1900.html 前言 浏览器兼容是前端开发人员必须掌握的一个技能,但是初入前端的同学或者其他后台web开发同学往往容易 ...

  6. Solr Suggest组件的使用

    使用suggest的原因,最主要就是相比于search速度快,In general, we need the autosuggest feature to satisfy two main requi ...

  7. nginx收到空包问题

    tcpdump有收包,但是nginx的access.log显示post数据为空 可以通过tcpdump监控端口 http://www.cnblogs.com/linn/p/4792468.html 修 ...

  8. linux中grep工具

    正则表达式 以前我们用grep在一个文件中找出包含某些字符串的行,比如在头文件中找出一个宏定义.其实grep还可以找出符合某个模式(Pattern)的一类字符串.例如找出所有符合xxxxx@xxxx. ...

  9. 25个必须记住的SSH命令

      OpenSSH是SSH连接工具的免费版本.telnet,rlogin和ftp用户可能还没意识到他们在互联网上传输的密码是未加密的,但SSH 是加密的,OpenSSH加密所有通信(包括密码),有效消 ...

  10. zmq消息订阅

    一个需求,用户预约了手机超时没有使用,要通知到预约的用户“设备超时”. 我本来是自己这一端计时然后超时后推送通知的. 但是上海测说他那边计时,然后释放手机.我这边只要订阅他那边的消息就好了. 外部的应 ...