题目:click here

题意:

  有个很暴躁的人,想坐车旅行n个城市。连接城市共有m条路(双向)。他坐在车上很不爽,每次最多忍耐x分钟。但是每站下车他又可以休息(重新计时)。总共有q次询问。问途中有多少条路他可以不爆发。a到b 和 b到a 算不同的路。 a 和 b 必须不相同。 

分析:

  并查集

 #include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
const int M = 2e4+; struct Edge { // 保存结点信息
int from, to, cost;
bool operator < ( const Edge x ) const { return cost < x.cost; }
} es[M*]; struct Query { // 保存问题信息
int val, id;
bool operator < ( const Query x ) const { return val < x.val; };
} que[]; int n, m, q;
int pre[M]; // 并查集
int ans[M]; // 答案
int sum[M]; // 记录并查集树高度 int find( int x ) {
return pre[x] == x ? x : pre[x] = find( pre[x] );
}
void merge( int x, int y ) {
x = find( x ); y = find( y );
if( sum[x] > sum[y] ) { sum[x] += sum[y]; pre[y] = x; }
else { sum[y] += sum[x]; pre[x] = y; }
}
void solve() {
scanf("%d%d%d", &n, &m, &q );
for( int i=; i<m; i++ )
scanf("%d%d%d", &es[i].from, &es[i].to, &es[i].cost );
sort( es, es+m );
for( int i=; i<q; i++ ) {
scanf("%d", &que[i].val );
que[i].id = i;
}
sort( que, que+q );
for( int i=; i<=n; i++ ) { pre[i] = i; sum[i] = ; }
int j = ;
int res = ;
for( int i=; i<q; i++ ) {
for( ; j<m && que[i].val>=es[j].cost; j++ ) {
int x = find( es[j].from );
int y = find( es[j].to );
if( x == y ) continue;
res += sum[x] * sum[y];
merge( es[j].from, es[j].to );
}
ans[que[i].id] = res;
}
for( int i=; i<q; i++ )
printf("%d\n", ans[i]<< );
} int main() {
int t; scanf("%d", &t );
while( t-- ) {
solve();
} return ;
}

HDU OJ 5441 Travel 2015online E的更多相关文章

  1. [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

    前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...

  2. hdu oj 1285 确定比赛名次

    hdu oj 1285 确定比赛名次 题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  3. sort(hdu oj 1425)计数排序和快速排序

    Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0 < n,m < 1000000),第二行包含n个各不 ...

  4. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  5. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

  6. hdu 5441 Travel (2015长春网赛)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个 ...

  7. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  8. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  9. HDU 5441——Travel——————【并查集+二分查界限】

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

随机推荐

  1. mini KMS Activator v1.3破解激活microsoft Office 2010 RTM

    利用mini KMS Activator v1.3破解激活microsoft Office 2010 RTM方法,只是为体验office而做测试使用的哦...大家觉得好就自觉购买正版去... 使用步骤 ...

  2. 原来ipad的浏览器也可以直接clip到evernote

    今天才发现是有方法通过邮件方式保存ipad上浏览的内容到evernote,之前以为要反复切换app来做到. 只要在toread.cc登记evernote对应帐号的邮箱,就可以根据toread返回到ev ...

  3. 前端开发工具—fiddle

  4. 关于java同步包中ConcurrentLinkedQueue类的深入分析与理解

    一,官方描写叙述 一个基于连接节点的无界线程安全队列.这个队列的顺序是先进先出.队列头部的元素是留在队列中时间最长的,队列尾部的元素是留在队列中时间最短的.新元素被插入到元素的尾部,队列从队列的头部检 ...

  5. org.openqa.selenium.SessionNotCreatedException: A new session could not be created.

    解决方案 1. 重新插拔手机. 2. 检查appium端口是否被占用,如是,杀掉占用了改端口的进程,然后重启appium. 3.

  6. VlanTrunk

    简单的vlan trunk的配置: 第一步:添加vlan 1 Switch>enable 2 Switch#show vlan VLAN Name Status Ports ---- ----- ...

  7. 【android】两个按钮的宽度各占屏幕的一半

    <LinearLayout> <Button android:layout_height="wrap_content" android:layout_width= ...

  8. Java I/O流操作(二)---缓冲流[转]

    转自:http://blog.csdn.net/johnny901114/article/details/8710403 一.BufferWriter类 IO的缓冲区的存在就是为了提高效率,把要操作的 ...

  9. Linux编程环境介绍(3) -- linux下的c/c++程序开发

    目录: 1. 编辑器( Vi ) [vi 与 vim] vi(visual interface)是linux系统最重要的文本编辑器, 所有的 Unix-Like 系统都会内置vi文本编辑器.  vim ...

  10. Android 实现GIF播放(解码)

    实现原理很简单,先把GIF动画解码成多张Bitmap图片,然后放到AnimationDrawable里面去逐一播放即可. GifHelper代码: package com.android.view; ...