HDU 1102 Constructing Roads
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5227 Accepted Submission(s): 1896
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.
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.
很简单的最小生成树,有两种做法,我采用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的更多相关文章
- 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 (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- 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 (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads(kruskal)
Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...
- 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 ...
随机推荐
- C语言回滚(一)
//用循环计算输入的字符数 #include<stdio.h> #include<string.h> #include<stdlib.h> int main(){ ...
- servlet中的转发和重定向问题
重定向和请求转发在学习servlet的时候很容易混淆,故在此特意记录. 1. 重定向---------sendRedirect()方法 Servlet响应请求有两种方式,一个是重定向,返回一个页面给客 ...
- ASP.NET Web API 数据提供系统相关类型及其关系
- dapper的增、删、查改的CodeSmith模板
<%@ Template Language="C#" TargetLanguage="Text" %> <%@ Property Name=& ...
- Echarts图表控件使用总结1(Line,Bar)
问题篇(详解):http://www.cnblogs.com/hanyinglong/p/4708337.html 1.前言 a.在系统开发过程中可能会使用到图表控件,一个好的图标控件可以使我们的网站 ...
- ASP.NET数据绑定技术
1.DataBinder.Eval()方法 DataBinder.Eval()方法是ASP.NET框架支持的一个静态方法,用来计算Late_Bound(后期绑定)数据绑定表达式,并随时将结果转换为字符 ...
- Microsoft Dynamics CRM 前瑞开发
做CRM开发最大的感受就是其前瑞开发过程中,调试起来比较麻烦,需要做一些断点还要配制一些浏览器设置,对新手来说比较困难.还有就是对REST调试,经常为了调试一个正确的结果而花费大量的时间.现在推荐一个 ...
- arcgis安装msi安装包提示"在未标记为正在运行时,调用了RunScript”解决办法
安装msi安装包提示"在未标记为正在运行时,调用了RunScript”解决办法 windows/temp目录相关权限不对,右击temp文件夹,选择管理员获取所有权限.
- 【转】Android 语言切换过程分析
最近在看一个bug,系统切换语言后,本来退到后台的音乐,会在通知栏上显示通知.为了解决这个bug,我学习了下android的语言切换流程,也参考了大量其他人的资料.(主要参考了http://blog. ...
- Linux0.11内核剖析--内核体系结构
一个完整可用的操作系统主要由 4 部分组成:硬件.操作系统内核.操作系统服务和用户应用程序,如下图所示: 用户应用程序是指那些字处理程序. Internet 浏览器程序或用户自行编制的各种应用程序: ...