最小生成树 D - Constructing Roads
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
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
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
#define MAXN 104
#define INF 0x3f3f3f3f
bool been[MAXN];
int n,q,g[MAXN][MAXN],lowcost[MAXN];
/*
最小生成树,如果D(a,b)<=ra+rb,那么g[a][b]=0
否则D(a,b)>ra+rb,g[a][b] = ra+rb-D(a,b)
*/
int Prim(int beg)
{
int ans = ;
memset(been,false,sizeof(been));
for(int i=;i<=n;i++)
{
if(i==beg) lowcost[i] = ;
else lowcost[i] = g[beg][i];
}
been[beg] = true;
for(int j=;j<n;j++)
{
int Minc = INF,k=-;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]<Minc)
{
Minc = lowcost[i];
k = i;
}
}
if(k==-) return -;
been[k] = true;
ans+=Minc;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]>g[k][i])
lowcost[i] = g[k][i];
}
}
return ans;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>g[i][j];
}
}
cin>>q;
int tx,ty;
for(int i=;i<=q;i++)
{
cin>>tx>>ty;
g[tx][ty] = g[ty][tx] = ;
}
int ans = Prim();
cout<<ans<<endl;
return ;
}
最小生成树 D - Constructing Roads的更多相关文章
- (最小生成树)Constructing Roads -- poj -- 2421
链接: http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2113 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Constructing Roads(1102 最小生成树 prim)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 推荐一波 瀑布流的RecylceView
推荐博客:http://www.bubuko.com/infodetail-999014.html
- 51nod1265判断四点共面
1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共 ...
- 获取Sprite上某一个点的透明度
转载[ http://www.cnblogs.com/Androider123/p/3795050.html] 本篇文章主要讲一下怎么做一个不规则的按钮,比如如下图的八卦,点击绿色和点击红色部分,需要 ...
- selenium-server 启动命令
启动hub主机: java -jar selenium-server-standalone-2.39.0.jar -role hub 启动node 本地:java -jar selenium-serv ...
- 初学spring之入门案列
spring其实是一个很大的开源框架,而我学的就是spring framework,这只是spring其中的一小部分.有疑惑的可以去官网去看看,spring官网我就不提供了.一百度肯定有.和sprin ...
- Dom编程的入门
<html><head> <meta http-equiv="Content-Type" content="text/html; ch ...
- AndroidStudio3.0 Canary 8注解报错Annotation processors must be explicitly declared now.
体验最新版AndroidStudio3. Canary 8的时候,发现之前项目的butter knife报错,用到注解的应该都会报错 Error:Execution failed for task ' ...
- Android 仿微信调用第三方应用导航(百度,高德、腾讯)
实现目标 先来一张微信功能截图看看要做什么 其实就是有一个目的地,点击目的地的时候弹出可选择的应用进行导航. 大脑动一下,要实现这个功能应该大体分成两步: 底部弹出可选的地图菜单进行展示 点击具体菜 ...
- [转] Android利用Fiddler进行网络数据抓包
主要介绍Android及IPhone手机上如何利用Fiddler进行网络数据抓包,比如我们想抓某个应用(微博.微信.墨迹天气)的网络通信请求就可以利用这个方法. Mac 下请使用 Charles 代替 ...
- LR接口测试---Java Vuser之增删改查
import lrapi.lr; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...