Constructing Roads--hdu1102
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17187 Accepted Submission(s): 6526
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.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int per[],map[][];
struct node
{
int b,e,w;
}s[];
bool cmp(node x,node y)
{
return x.w<y.w;
}
void init()
{
for(int i=;i<;i++)
per[i]=i;
} int find(int x)
{
while(x!=per[x])
x=per[x];
return x;
} bool join (int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
return true;
}
return false;
}
int main()
{
int n,i,n1,a,b,j;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&map[i][j]);
scanf("%d",&n1);
for(i=;i<n1;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=;
}
int k=;
for(i=;i<=n;i++)
{
for(j=i;j<=n;j++)
{
s[k].b=i;
s[k].e=j;
s[k].w=map[i][j];
k++;
}
}
sort(s,s+k,cmp);
int sum=;
for(i=;i<k;i++)
{
if(join(s[i].b,s[i].e))
sum+=s[i].w;
}
printf("%d\n",sum);
}
return ;
}
Constructing Roads--hdu1102的更多相关文章
- HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏
Constructing Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads (MST)
Constructing Roads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
随机推荐
- ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over()
ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over() 今天女票问我SqlServer的四种排序,当场写了几句Sql让她了解,现把相关Sql放上来. 首先, ...
- HSV颜色识别demo
HSV(Hue, Saturation, Value)色彩空间是一种区别与RGB的表示形式.其模型可视为一个倒立的棱锥或圆锥. 其中H为色调,用角度度量,取值范围为0°-360°,从红色开始按逆时针方 ...
- [POJ] 1274 The Perfect Stall(二分图最大匹配)
题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...
- [工具]web开发时自动刷新网页:liveReload
传统网页开发流程:用sublime text写好代码,运行,发现问题,再回到sublime text修改,运行…如此往复,十分繁琐.今天看到有人(<LiveReload>讓Sublime ...
- windows批处理命令教程
批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用Cmd.exe按照该文件中各个命令 ...
- Sessions, Window Stations and Desktops(GetDesktopWindow函数得到的桌面句柄, 是Csrss.exe创建的一个窗口)
由于不是搞安全的,对这块的东西一直不是很清楚,今天特意整理下. 总的来说,他们的关系如下: 我们知道Windows是支持多用户的,Session可以理解为每个用户登录,比如当前登录和远程登陆.Wind ...
- POST多个参数到Web API控制器
交互基于Json的方式打包传递就不介绍了,主要设置请求头为 contentType: "application/json", //必须有 数据位Json格式的字符串,在服务器端定义 ...
- 【转】Android ListView长按事件触发点击事件
原文网址:http://blog.csdn.net/twlkyao/article/details/17301609 算法在实现ListView的onItemLongClickListener的时候, ...
- 【转】64位win7硬盘安装64位ubuntu 13.04
原文网址:http://www.cnblogs.com/jiangz/p/3751617.html 最近本来是准备通过升级的方式把ubuntu从12.04升级到12.10再升级到13.04的,但是升级 ...
- 解决android4.0系统中菜单(Menu)添加Icon无效问题
本文转载自: http://blog.csdn.net/stevenhu_223/article/details/9705173 在Android4.0系统中,创建菜单Menu,通过setIcon方法 ...