http://acm.hdu.edu.cn/showproblem.php?pid=5441

Travel

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2061    Accepted Submission(s): 711

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 band 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
 

题意:是有n个城市,m条边包含u v w;代表u到v的时间是w;

给q的时间x,求在x时间内Jack可以到达多少对城市;其中ab和ba是不同的;

1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000   ///可以到达35和53;
10000 ///2 5 3是可以相互到达的所以有6种;
13000 ///2 3 5 4是相通的有12种;

 
Source
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. #define N 100100
  8.  
  9. struct node
  10. {
  11. int u, v, w, index;
  12. } a[N], b[N];
  13.  
  14. int r[N], f[N], Q[N];
  15.  
  16. int cmp(node n1, node n2)
  17. {
  18. return n1.w < n2.w;
  19. }
  20.  
  21. int Find(int x)
  22. {
  23. if(x!=f[x])
  24. f[x]=Find(f[x]);
  25. return f[x];
  26. }
  27.  
  28. int main()
  29. {
  30. int t;
  31. scanf("%d", &t);
  32. while(t--)
  33. {
  34. int n, m, q, i;
  35. scanf("%d%d%d", &n, &m, &q);
  36.  
  37. for(i=; i<=n; i++)
  38. {
  39. f[i] = i;
  40. r[i] = ;
  41. }
  42.  
  43. for(i=; i<m; i++)
  44. scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
  45. for(i=; i<q; i++)
  46. {
  47. scanf("%d", &b[i].w);
  48. b[i].index = i;
  49. }
  50.  
  51. sort(a, a+m, cmp);
  52. sort(b, b+q, cmp);
  53.  
  54. int sum = , j = ;
  55. for(i=; i<q; i++)
  56. {
  57. while(j<m && a[j].w<=b[i].w)
  58. {
  59. int fu = Find(a[j].u);
  60. int fv = Find(a[j].v);
  61.  
  62. if(fu!=fv)
  63. {
  64. f[fu] = fv;
  65. sum += r[fu]*r[fv]; ///两个集合的任意组合
  66. r[fv] += r[fu]; ///r[i]代表i的根节点所包含的元素的个数
  67. }
  68. j++;
  69. }
  70. Q[b[i].index] = sum*; ///ab和ba是不一样的
  71. }
  72.  
  73. for(i=; i<q; i++)
  74. printf("%d\n", Q[i]);
  75. }
  76. return ;
  77. }

(并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )的更多相关文章

  1. (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)

    http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others)  ...

  2. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online

    Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...

  4. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  5. 2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)

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

  6. 2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel (Krsukal变形)

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

  7. 2015 ACM/ICPC Asia Regional Changchun Online

    1001 Alisha’s Party 比赛的时候学长stl吃T.手写堆过. 赛后我贴了那两份代码都过.相差.2s. 于是用stl写水果. # include <iostream> # i ...

  8. (线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/ ...

  9. Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)

    题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果 ...

随机推荐

  1. Object-c 创建按钮

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //动态创建我们自己的按钮 //1.创建按钮(UIB ...

  2. Haskell语言学习笔记(40)Arrow(1)

    Arrow class Category a => Arrow a where arr :: (b -> c) -> a b c first :: a b c -> a (b, ...

  3. delphi常用函数和方法

     uses ShellApi, ActiveX, ComObj, ShlObj; function HasText(Text: string; const Values: array of strin ...

  4. linux sleep用法

    应用程序:#include <syswait.h>usleep(n) //n微秒Sleep(n)//n毫秒sleep(n)//n秒驱动程序:#include <linux/delay ...

  5. What is API Level?

    [What is API Level?] 参考:http://android.xsoftlab.net/guide/topics/manifest/uses-sdk-element.html#ApiL ...

  6. HTTP Error 500.0 - Internal Server Error错误代码0x80070002

    案例研究:AspNetInitClrHostFailureModule中的“HTTP错误500.0 - 内部服务器错误” 症状 当用户访问在Windows Server 2008 R2计算机上运行的A ...

  7. np.random.seed()

    124.np.random.seed()的作用 陈容喜 关注 2018.01.11 21:36 字数 3 阅读 4460评论 0喜欢 6 今天看到一段代码时遇到了np.random.seed(),搞不 ...

  8. Java的三种多线程

    项目结构 继承Thread类 /* * Thread类实现了Runnable接口 */ public class MyThread extends Thread { @Override public ...

  9. ECMAScript5新特性之获取对象特有的属性

    'use strict'; // 父类 function Fruit(){ } Fruit.prototype.name = '水果'; // 子类 function Apple(desc){ thi ...

  10. C#数字类型输出字符串时保留指定小数位数的方法

    1.使用占位符: 1)float f = 321.12345F;f.ToString("0.00");这样做无论f是不是整数,都将加上2位小数. 2)float f = 321.1 ...