hdu 5441

Travel

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


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,T5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n20000,m100000,q5000. 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 d100000. 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
 

 

Source
 

 

Recommend
hujie   |   We have carefully selected several similar problems for you:  6361 6360 6359 6358 6357 
 
 
复制代码
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<vector>
 7 using namespace std;
 8 typedef long long ll;
 9 #define N 100009
10 #define ls l,m,rt<<1
11 #define rs m+1,r,rt<<1|1
12 #define mem(a,b) memset(a,b,sizeof(a))
13 int  t,n,m,q;
14 struct Node{
15     int u,v,w;
16 }nod[N];
17 struct Nod{
18     int id,w;
19 }no[N];
20 int pre[20009];
21 int cnt[20009];
22 bool cmp(Node a,Node b){
23     return a.w<b.w;
24 }
25 bool cmp1(Nod a,Nod b){
26     return a.w<b.w;
27 }
28 void init()
29 {
30     for(int i=0;i<=n;i++){
31         pre[i]=i;
32         cnt[i]=1;
33     }
34 }
35 int find(int x)
36 {
37     return pre[x]=x==pre[x]?x:find(pre[x]);
38 }
39 void unite(int u,int v)
40 {  /*
41     pre[v]=u;
42     cnt[u]+=cnt[v];
43     */
44     pre[u]=v;
45     cnt[v]+=cnt[u];
46     //上面两种写法都是对的。
47 }
48 int ret[N];
49 int  main()
50 {
51     scanf("%d",&t);
52     while(t--)
53     {
54         scanf("%d%d%d",&n,&m,&q);
55         init();
56         for(int i=0;i<m;i++){
57             scanf("%d%d%d",&nod[i].u,&nod[i].v,&nod[i].w);
58             }
59             sort(nod,nod+m,cmp);
60             for(int i=0;i<q;i++){
61                 scanf("%d",&no[i].w);
62                 no[i].id=i;
63             }
64             sort(no,no+q,cmp1);
65             int j=0,ans=0;
66             int u,v;
67             //预处理
68             for(int i=0;i<q;i++){
69                 while(j<m&&nod[j].w<=no[i].w){
70                     u=find(nod[j].u);
71                     v=find(nod[j].v);
72                     if(u!=v){
73                         int tmp=cnt[v]+cnt[u];
74                         //任意一对点都行
75                         ans+=(tmp*(tmp-1))-(cnt[u]*(cnt[u]-1))-(cnt[v]*(cnt[v]-1));
76                         //减去已经加过的
77                         unite(u,v);
78                     }
79                     j++;
80                 }
81                 ret[no[i].id]=ans;
82             }            
83             for(int i=0;i<q;i++){
84                 printf("%d\n",ret[i]);
85             }        
86     }
87     return 0;
88 }
复制代码

 

posted on   cltt  阅读(189)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示