首页 技术 正文
技术 2022年11月19日
0 收藏 438 点赞 4,959 浏览 2690 个字

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≤, which represents the number of test case.For each test case, the first line consists of three integers n,m and q where n≤,m≤,q≤. 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∈{,...,n} and d≤. 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


 Sample Output


 Source2015 ACM/ICPC Asia Regional Changchun Online 

比赛的时候坑在这题了,一直TLE,统计个数的方法不对,导致一直在这题徘徊,否则就能去思考其它的题了,可能就不是这种破成绩了。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 600000
#define inf 1e12
int n,m,q;
struct Node{
int u,v,d;
}edge[N];
struct Node1{
int num;
int idd;
int ans;
}query[];
bool cmp1(Node a,Node b){
return a.d<b.d;
}
bool cmp2(Node1 a,Node1 b){
return a.num<b.num;
}
bool cmp3(Node1 a,Node1 b){
return a.idd<b.idd;
}
/////////////////////////////////////////////
int fa[];
int cnt[];
void init(){
for(int i=;i<;i++){
fa[i]=i;
cnt[i]=;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
} ////////////////////////////////////////////////////// int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].d);
}
for(int i=;i<q;i++){
scanf("%d",&query[i].num);
query[i].idd=i;
query[i].ans=;
}
sort(edge,edge+m,cmp1);
sort(query,query+q,cmp2);
int w=;
int ans=;
for(int i=;i<q;i++){
//printf("+=====");
while(w<m && edge[w].d<=query[i].num){ int root1=find(edge[w].u);
int root2=find(edge[w].v); if(root1!=root2){
ans+=cnt[root1]*cnt[root2]; //printf("***%d\n",ans);
cnt[root2]+=cnt[root1];
fa[root1]=root2;
}
w++;
}
//printf("%d\n",ans*2);
query[i].ans=ans*;
}
sort(query,query+q,cmp3);
for(int i=;i<q;i++){
printf("%d\n",query[i].ans);
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902