Problem Description
Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is x minutes, how many pairs of city (a,b) are there that Jack can travel from city a to b without going berserk?
 
Input
The first line contains one integer T,T≤5, which represents the number of test case.
For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. The Undirected Kingdom has n cities and mbidirectional roads, and there are q queries.
Each of the following m lines consists of three integers a,b and d where a,b∈{1,...,n} and d≤100000. It takes Jack d minutes to travel from city a to city b and vice versa.
Then q lines follow. Each of them is a query consisting of an integer x where x is the time limit before Jack goes berserk.

Output
You should print q lines for each test case. Each of them contains one integer as the number of pair of cities (a,b) which Jack may travel from a to b within the time limit x.
Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
 
Sample Input
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
 
Sample Output
2
6
12
 

怎么说呢,哎,在这道题上面浪费了整整一天的时间,理解题意得错误导致了这么严重的后果,简直无语了,下面这张截图,非常励志的一个故事= =

说多了都是泪啊,主要错在了合并城市的路的条数上,我现在的心情你无法理解,呜呜呜呜呜..............

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std; #define INF 0x3f3f3f3f
#define lsonl,m,rt<<1
#define rsonm+1,r,rt<<1|1 const int MX = 222222; int road[MX];
int num[MX]; structNode {
int a, b, c;
}node[MX]; structQuery {
int n, sign, ans;
}query[MX]; bool comp1(const Node& n1, const Node& n2) {
return n1.c < n2.c;
} bool comp2(const Query& q1, const Query& q2) {
return q1.n < q2.n;
} bool comp3(const Query& q1, const Query& q2) {
return q1.sign < q2.sign;
} int Find(int x) {
return road[x] == x ? x : (road[x] = Find(road[x]));
} int main() {
//freopen("input.txt", "r", stdin);
int n, m, q;
int cas;
while (scanf("%d", &cas) != EOF) {
while (cas--) {
scanf("%d%d%d", &n, &m, &q);
for (int i = 0; i <= n; i++) {
road[i] = i;
num[i] = 1;
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &node[i].a, &node[i].b, &node[i].c);
}
sort(node + 1, node + m + 1, comp1);
for (int i = 1; i <= q; i++) {
scanf("%d", &query[i].n);
query[i].sign = i;
}
sort(query + 1, query + 1 + q, comp2);
int ans = 0;
for (int i = 1, j = 1; i <= q; i++) {
while (j <= m && node[j].c <= query[i].n) {
int root1 = Find(node[j].a);
int root2 = Find(node[j].b);
j++;
if (root1 != root2) {
ans += num[root1] * num[root2] * 2;//就是这里,好难搞清楚啊,呜呜呜呜............新元素乘以老元素,这就是多出来的新路的条数,乘以二是因为a到d与b到a是两条路
road[root2] = root1;
num[root1] += num[root2];
}
}
query[i].ans = ans;
}
sort(query + 1, query + 1 + q, comp3);
for (int i = 1; i <= q; i++) {
printf("%d\n", query[i].ans);
}
}
}
return 0;
}

HDU - Travel的更多相关文章

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

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

  2. hdu 4885 TIANKENG’s travel(bfs)

    题目链接:hdu 4885 TIANKENG's travel 题目大意:给定N,L,表示有N个加油站,每次加满油能够移动距离L,必须走直线,可是能够为斜线.然后给出sx,sy,ex,ey,以及N个加 ...

  3. hdu 2433 Travel

    http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...

  4. (并查集)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 ...

  5. HDU 4418 Time travel 期望dp+dfs+高斯消元

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4418 Time travel Time Limit: 2000/1000 MS (Java/Othe ...

  6. HDU 4418 Time travel

    Time travel http://acm.hdu.edu.cn/showproblem.php?pid=4418 分析: 因为走到最后在折返,可以将区间复制一份,就变成了只往右走,01234321 ...

  7. hdu 5380 Travel with candy(双端队列)

    pid=5380">题目链接:hdu 5380 Travel with candy 保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别能够以多少价格退货,以及能够推货的量. ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. 【HDU】4418 Time travel

    http://acm.hdu.edu.cn/showproblem.php?pid=4418 题意:一个0-n-1的坐标轴,给出起点X.终点Y,和初始方向D(0表示从左向右.1表示从右向左,-1表示起 ...

随机推荐

  1. HTML5学习之跨文档传输消息(七)

    新标准中提供了文档之间直接的消息传输API.而且不限制跨域消息传递! 发送消息使用的是Window对象的postMessage(data,targetURL)方法就可以了,但给哪个window对象发送 ...

  2. MVC4 WEBAPI(一)使用概述

    所谓概述,也就是总结一些WEB API常用的使用用法.MVC APIWEB是一个轻量级的服务接口,完全符合RestFul框架设计,每个URL代表一种资源,使用方便,没有WCF那么庞大,但是麻雀虽小五脏 ...

  3. 《图形学》实验四:中点Bresenham算法画直线

    开发环境: VC++6.0,OpenGL 实验内容: 使用中点Bresenham算法画直线. 实验结果: 代码: //中点Bresenham算法生成直线 #include <gl/glut.h& ...

  4. HDU5489 Removed Interval(动态规划)

    一个长度为n的序列,删除任意长度为l的连续子序列后,求剩下的序列的最长公共子序列. 先求出以第i个元素为开始的LIS的长度,再一次循环,对所要求的结果更新 #include<iostream&g ...

  5. android 入门-控件 测量状态栏高度

    private ViewTreeObserver viewTreeObserver; /** 获取可見区域高度 **/ WindowManager manager = getWindowManager ...

  6. js判断手机端Android手机还是iPhone手机

    /*判断当前设备是平板.安卓.苹果设备*/ <script type="text/javascript"> function fBrowserRedirect(){ v ...

  7. poj 2337 欧拉回路输出最小字典序路径 ***

    把26个小写字母当成点,每个单词就是一条边. 然后就是求欧拉路径. #include<cstdio> #include<iostream> #include<algori ...

  8. PMP 第十三章 项目干系人管理

    1.识别干系人是干什么?早期就识别干系人的原因是什么?识别干系人的输入和工具有哪些?干系人分析的几种模型是哪些?干系人登记册的内容有哪些?bbs.mypm.net 2.干系人参与程度的分类是怎样的?干 ...

  9. sublime总结

    自定义快捷键: preferences->key binding->user ctrl+d 删除行 ctrl+k 选中下一同名变量,alt+F3 选中全部同名变量 [ {"key ...

  10. Alcatraz安装 不能用解决方案

    1.安装 1>Github上下载Alcatraz,下载地址:https://github.com/supermarin/Alcatraz  2>Alcatraz是xcode的插件,这个插件 ...