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 非常相似,改变了输入方式之后, 本题实际上更 ...
随机推荐
- webpack ,react
一小时包教会 —— webpack 入门指南 http://***/Article/50764 ------------------ 轻松入门React和Webpack http://www.in ...
- 《javascript权威指南》第9章 例9-8源码
//创建一个新的枚举类型 //不能使用它来创建该类型的新实例 function Enumeration(nameToValues){ var Enumeration = function(){thro ...
- linux 搭建lamp环境
sudo apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql sudo chmod 777 /var/w ...
- c#进程间通信(Inter-Process Communication)
原文:c#进程间通信(Inter-Process Communication) c#进程间通信(IPC, Inter-Process Communication) 接收端: using System; ...
- vs2010 suite integration toolkit execution
原因是UltraDeamen的问题,重新换个WinMount来解压ISO文件.完美安装运行
- 2015必须推荐的Android框架,猿必读系列!
一.Guava Google 的基于java1.6的类库集合的扩展项目,包括collections, caching, primitives support, concurrency librarie ...
- 【Android】通过Java代码替换TabHost中的drawableTop资源
在博客 http://blog.csdn.net/jueblog/article/details/11837445 中的Tab选项卡中, 点击相应的Tab选项,图标没有发生改变. 这些资源图片也没有尽 ...
- Xmind 快捷键
Xmind 快捷键 快捷键(Windows) 快捷键(Mac) 描述 Ctrl+N Command+N 建立新工作簿 Ctrl+O Command+O 开启工作簿 Ctrl+S Command+S 储 ...
- Android ToggleButton使用介绍
ToggleButton,就是开关按钮,包括选中和未选中状态,并且需要为不同的状态设置不同的事件处理: 例如:使用图片来展示ToggleButton不同的状态: MainActivity.java p ...
- js 获取input file路径改变图像地址
html代码 <img id="newImage" alt="100x100" src="__PUBLIC__/img/1.jpg" ...