(step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目。在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离。(下面会结合样例说明)。接着,输入一个整数q,表示已经有q条路修好。
在接下来的q行中,会给出修好的路的起始村庄和结束村庄。。
输入样例说明如下:
解题思路:最小生成树(kruscal算法)
1)以前的题会直接给村庄编号以及村庄距离。而这道题,这是给出村庄的距离矩阵。村庄的编号信息蕴含在
矩阵中。这时候的读取方法为:
for(i = 1 ; i <= n ; ++i){
for(j = i + 1 ; j <= n ; ++j){
e[count].begin = i;
e[count].end = j;
e[count].weight = map[i][j];
count++;
}
}
2)点的起始编号与变得起始编号没有必然的联系。即点可以从1开始计数,而边则从0开始计数。
for( i = 1 ; i < maxn ; ++i){
father[i] = i;
}
for( i = 0 ; i < count ; ++i){
int fx = find(e[i].begin);
int fy = find(e[i].end);
if(fx != fy){
father[fx] = fy;
sum += e[i].weight;
}
}
3)已修的路,令weight为0即可。
代码如下:
/*
* 1102_1.cpp
*
* Created on: 2013年8月26日
* Author: Administrator
*/ #include <iostream> using namespace std; struct edge{
int begin;
int end;
int weight;
}; const int maxn = 6000;
int father[maxn];
edge e[maxn*maxn]; int find(int x){
if( x == father[x]){
return x;
} father[x] = find(father[x]);
return father[x];
} int kruscal(int count){
int i;
int sum = 0; for( i = 1 ; i < maxn ; ++i){
father[i] = i;
} for( i = 0 ; i < count ; ++i){
int fx = find(e[i].begin);
int fy = find(e[i].end); if(fx != fy){
father[fx] = fy;
sum += e[i].weight;
}
} return sum;
} bool compare(const edge& a , const edge& b){
return a.weight < b.weight;
} int main(){
int n;
while(scanf("%d",&n)!=EOF){
int i,j;
int map[n+1][n+1];
for( i = 1 ; i <= n ; ++i){
for( j = 1 ; j <= n ; ++j){
scanf("%d",&map[i][j]);
}
}
int q;
scanf("%d",&q);
for( i = 1 ; i <= q ; ++i){
int a ,b;
scanf("%d%d",&a,&b);
map[a][b] = 0;
}
int count = 0;
for(i = 1 ; i <= n ; ++i){
for(j = i + 1 ; j <= n ; ++j){
e[count].begin = i;
e[count].end = j;
e[count].weight = map[i][j];
count++;
}
} sort(e, e + count , compare); int sum = kruscal(count); printf("%d\n",sum); }
}
(step6.1.4)hdu 1102(Constructing Roads——最小生成树)的更多相关文章
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- HDU 1102 Constructing Roads(最小生成树,基础题)
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- 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 ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- Qt之日志输出文件
在Qt开发过程当中经常使用qDebug等一些输出来调试程序,但是到了正式发布的时候,都会被注释或者删除,采用日志输出来代替. 做过项目的童鞋可能都使用过日志功能,以便有异常错误能够快速跟踪.定 ...
- mongdb修改密码
正确做法,利用db.changeUserPassword > db.changeUserPassword('tank2','test');
- 两张图解读Java异常与断言
两张图解读Java异常与断言 --转载请注明出处:coder-pig 本节引言: 前天公布的"七张图解析Java多线程&quo ...
- 如何在settings里的休眠模式里添加永不休眠
最近项目需求里需要在设置菜单的休眠模式里添加一项永不休眠选择项.针对MTK平台的修改方式有如下几步骤.(其他平台和android原生系统的修改方式类似,只是android原生系统修改永不休眠需要通过设 ...
- Python WxPython 的安装以及使用
WxPython 网址 学习网址 http://wiki.wxpython.org/How%20to%20Learn%20wxPython 安装网址 http://wiki.wxpython.org/ ...
- 研究一下FBrush,它是从TWinControl才有的属性(可能是因为需要句柄)——发现{$R *.dfm}在运行期执行,而且很有深意,读到属性后赋值还会触发事件,这些无法在VCL代码里直接看到
定义和创建: TWinControl = class(TControl) private FBrush: TBrush; end; constructor TWinControl.Create(AOw ...
- VS2008下编译BOOST 1.39的ASIO库
由于全部编译BOOST库需要的时间太长,而且耗费空间,况且我只需要用ASIO库,所以就没有必要全部编译了. boost库到www.boost.org上下载. 编译很简单,假设你的boost存放的目录是 ...
- hdu1695(莫比乌斯)或欧拉函数+容斥
题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...
- 特殊的Windows消息
WM_CREATE消息 该消息是Windows发送给视图的第一个消息.由于当应用程序框架调用Create函数时该消息就会被发送,而此时窗口创建还未完成,窗口还不可见,因此在控制函数OnCreate内部 ...
- QEventLoop等待另外一个事件的停止,非常实用 good
void MyWidget::SendRequest(QString strUser) { network_manager = new QNetworkAccessManager(); connect ...