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. Java集合源码学习(四)HashMap分析

    ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...

  2. Ubuntu下调整swap分区的大小

    转自:http://blog.chinaunix.net/uid-7573623-id-2048964.html 由于安装oracle 的时候,swap太小不能继续安装,于是想有什么方法在不不用安装o ...

  3. Delphi线程简介---Create及其参数、Resume、Suspend

    TThread在Classes单元里的声明如下 type TThread = class private FHandle: THandle; FThreadID: THandle; FTerminat ...

  4. 排序练习【sdut 1582】【堆排序】

    排序 Time Limit: 1000ms   Memory limit: 32678K  有疑问?点这里^_^ 题目描述 给你N(N<=100)个数,请你按照从小到大的顺序输出. 输入 输入数 ...

  5. org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:

    详细错误堆栈信息: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" ...

  6. 6-01T-SQL中的运算符

    算术运算符:+.-.*./.%. 赋值运算符:= 逻辑运算符:AND.OR.NOT. 比较运算符:>,<,<=,>=,<>.=,!=. 连接运算符:"+& ...

  7. [Outlook] 重新取得outlook中被禁止访问的文件

    摘要:接收到老大的邮件,邮件中带有jar包,导致我无法接收到这个文件,outlook2010中提示:outlook禁止访问不安全……,相信很多人都遇到过这个问题,以前也遇到过,总没去想着解决这个问题, ...

  8. 六款小巧的HTTP Server[C语言]

    1.micro_httpd - really small HTTP server特点: 支持安全的 .. 上级目录过滤 支持通用的MIME类型 支持简单的目录 支持目录列表 支持使用 index.ht ...

  9. PMP 第二章 项目生命周期与组织

    1 项目组织机构类型有哪些? 区别是什么? 职能型  矩阵型  项目性 2 什么是事业环境因素? 什么是组织过程资产? 如何区分事业环境因素和组织过程资产? 事业环境因素:事业环境因素指围绕项目或能影 ...

  10. Unity3D项目开发一点经验

    我们主要使用3dsmax2010进行制作,输出FBX的类型导入Unity3D中.默认情况下,3dsmax8可以和U3D软件直接融合,自动转换为FBX物体. 注意事项如下: 1.面数控制 在MAX软件中 ...