hdu 5441 Travel (2015长春网赛)
http://acm.hdu.edu.cn/showproblem.php?pid=5441
题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个询问一个时间值
求不大于这个时间的可以连通的城市有多少种连法
比如样例中第一个询问6000,不大于6000时间值的连通城市有3,5.所以有3-->5,5-->3两种
第二个询问10000,符合条件的连通的城市有2,3,5,所以有2-->3,3-->2,2-->5,5-->2,3-->5,5-->3六种
利用结构体储存点与权值然后把权值排序,找出符合条件的城市点,利用并查集记录相连的点然后合并,整合路线
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int father[],num[];
int a[];
void lsy(int n)
{
for (int i=;i<=n;i++)
{
father[i]=i;
num[i]=;
}
}
struct point {
int x,y,z;
bool operator <(const point& q)const
{
return z<q.z;
}
};
point yj[];
struct node {
int w,id;
bool operator <(const node &q)const
{
return w<q.w;
}
};
node yjj[];
int _find(int x)
{
if (father[x]==x)return x;
father[x]=_find(father[x]);
return father[x];
}
int main()
{
int t,n,m,q,i;
while (~scanf("%d",&t))
{
while (t--)
{
scanf("%d %d %d",&n,&m,&q);
memset(a,,sizeof(a));
for (i=;i<=m;i++)
scanf("%d %d %d",&yj[i].x,&yj[i].y,&yj[i].z);
sort(yj+,yj+m+);
lsy(n);
for (i=;i<=q;i++)
{
scanf("%d",&yjj[i].w);
yjj[i].id=i;
}
sort(yjj+,yjj+q+);
int ans=,j=;
for (i =;i<=q;i++)
{
while (j<=m&&yj[j].z<=yjj[i].w )
{
int sx=_find(yj[j].x);
int sy=_find(yj[j].y);
j++;
if (sx==sy) continue;
ans+=(num[sx]+num[sy])*(num[sx]+num[sy]-)-num[sx]*(num[sx]-) - num[sy]*(num[sy]-);//合并之后的关系
father[sx]=sy;
num[sy]+=num[sx];
}
a[yjj[i].id]=ans;
}
for (i=;i<=q;i++)
printf("%d\n",a[i]);
}
}
return ;
}
hdu 5441 Travel (2015长春网赛)的更多相关文章
- hdu 5441 (2015长春网络赛E题 带权并查集 )
n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...
- hdu 5443 (2015长春网赛G题 求区间最值)
求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...
- ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)
Problem Description In Land waterless, water is a very limited resource. People always fight for the ...
- ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...
- HDU 5531 Rebuild (2015长春现场赛,计算几何+三分法)
Rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- hihocoder1236(2015长春网赛J题) Scores(bitset && 分块)
题意:给你50000个五维点(a1,a2,a3,a4,a5),50000个询问(q1,q2,q3,q4,q5),问已知点里有多少个点(x1,x2,x3,x4,x5)满足(xi<=qi,i=1,2 ...
- (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )
http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others) Memo ...
- HDU 4764 Stone (2013长春网络赛,水博弈)
Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Travel(HDU 5441 2015长春区域赛 带权并查集)
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
随机推荐
- tensorflow 指定使用gpu处理,tensorflow占用多个GPU但只有一个在跑
我们在刚使用tensorflow的过程中,会遇到这个问题,通常我们有多个gpu,但是 在通过nvidia-smi查看的时候,一般多个gpu的资源都被占满,但是只有一个gpu的GPU-Util 和 21 ...
- SQL Server和MySQL数据库
导读:接下来的网上商城的项目,需要用到MySQL数据库了.这个对于我来说,是一个新接触的东西,按照惯例,在刚开始学习一个东西的时候,先从宏观上去了解它.本篇博客,先介绍SQL Server的基本内容, ...
- 关于 uboot 的异常向量表
ldr r,address 表示将 address 地址中的内容存入 r0 中
- Redis服务器开启远程访问
重启 ps aux|grep redis root 32684 0.0 0.0 48452 6944 ? Ssl 21:27 0:00 ./redis-server *:6379 kill了这个进程 ...
- 02_数据库基础之(二)sql语句入门
1.基本增删改查操作 #一. 数据类型 常用的3中 # .字符串 例如:你的名字 我是中国人 在数据库中要使用 ‘’引起来 '苹果手机' # .整数类型 例如: 你的年龄 ,办公室的人数 个 ,直接使 ...
- IDEA中配置Maven+spring MVC+tomcat
一:配置Maven安装教程如下: http://blog.csdn.net/qq_32588349/article/details/51461182 实际安装过程中,如果按照教程配置如下属性,最后创建 ...
- codeforces 722D Generating Sets 【优先队列】
You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...
- stm32学习基本知识点
1.AHB系统总线分为APB1(36MHz)和APB2(72MHz),其中2>1,意思是APB2接高速设备 2.Stm32f10x.h相当于reg52.h(里面有基本的位操作定义),另一个为st ...
- SAP中的slashX
SlashX /n This terminates the transaction. 关闭当前事务. /nxxxx This terminates the current transaction, a ...
- 28- foreach里面实现一次遍历两个链表
由于业务需求,要在一个foreach里面实现一次遍历两个链表:后台传来的是连个list: 分别是 <c:set var = "i" value = "0" ...