Travel

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

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 m bidirectional 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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int maxm=;
const int maxq=;
struct node1{
int u,v,w;
node1(){}
node1(int u,int v,int w):u(u),v(v),w(w) {}
}g[maxm]; struct node2{
int d,id;
node2(){}
node2(int d,int id):d(d),id(id) {}
}que[];
long long ans[];
int num[maxn], rak[maxn],father[maxn];
bool cmp1(struct node1 t1,struct node1 t2){
return t1.w<t2.w;
}
bool cmp2(struct node2 t1,struct node2 t2){
return t1.d<t2.d;
} int find(int x){
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
long long tnum;
void Union(int u,int v){
int x=find(u);
int y=find(v);
if(x==y)
return ;
tnum+=num[x]*num[y]; if(rak[x]<rak[y]){
father[x]=y;
num[y]+=num[x];
num[x]=;
}
else {
father[y]=x;
if(rak[x]==rak[y])
++rak[x];
num[x]+=num[y];
num[y]=;
}
} int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,q; scanf("%d%d%d",&n,&m,&q);
int u,v,w; for(int i=;i<=n;i++){
father[i]=i;
num[i]=;
rak[i]=;
} for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
g[i]=node1(u,v,w);
}
sort(g,g+m,cmp1);
int d;
for(int i=;i<q;i++){
scanf("%d",&d);
que[i]=node2(d,i);
}
sort(que,que+q,cmp2);
memset(ans,,sizeof(ans));
tnum=;
for(int i=,j=;i<q;i++){
int cur=que[i].d;
while(j<m){
node1 temp=g[j]; if(cur>=temp.w){ Union(temp.u,temp.v);
}
else
break;
j++;
}
ans[que[i].id]=tnum;
}
for(int i=;i<q;i++)
printf("%lld\n",ans[i]*); }
return ;
}

最新文章

  1. 如何删除 eclipse debugger 下不用的Java Application
  2. Spring事务解析3-增强方法的获取
  3. ASP.NET MVC中,怎么使用jquery/ajaxForm上传文件
  4. C#函数式编程之缓存技术
  5. HDU 2082 找单词 --生成函数
  6. lable自动适配大小
  7. 使用cnpm搭建企业内部私有NPM仓库
  8. WPF 多线程处理(6)
  9. Jacob - Outlook
  10. clock_gettime测代码运行时间
  11. SQL Server sp_configure 控制内存使用
  12. java基础第二天
  13. webpack 引入 bootstrap
  14. Oracle处理XML字段时遇到的ORA-31013: XPATH 表达式无效问题
  15. Android的图片,字符串,demin,color,以及Array,boolean,Integer资源的使用-android学习之旅(五十四)
  16. 一次故障解决过程梳理:mysql varchar text timestamp
  17. (四) Keras Dropout和正则化的使用
  18. [转]Java工程师技术栈--成神之路
  19. 用条件属性而不是#if
  20. python day05 作业答案

热门文章

  1. 对卷积(convolution)的理解
  2. HDU Rabbit and Grass 兔子和草 (Nim博弈)
  3. windows server 2008 R2 的 FTP 防火墙的正确配置方法
  4. Zero to One书摘
  5. HDU 5452 Minimum Cut (Spaning Tree)
  6. CodeForces 77C Beavermuncher-0xFF (树形dp)
  7. Android(java)学习笔记118:BroadcastReceiver之 外拨电话的广播接收者
  8. 连接惠普打印机(通过WIFI)
  9. 组件的通信 :provide / inject 对象进入后,就等于不用props,然后内部对象,直接复制可以接受数组,属性不能直接复制,可以用Object.assgin覆盖对象,或者Vue的set 双向绑定数据
  10. Hicharts图表的使用