Travel

Travel

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


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

 题意:Jack喜欢到处旅游,但是他在不停的行程中确有时间限制,超过时间就会晕车。当然他到达某一个地方会休息。给一他在行程中会晕车的时间限制,问他可以有多少像(a->b这样的旅游路线,当然b->a看做是不同的。输入数据给你n个city,m个路线,q个查询。m个路线,每个路线有x->y,及x->y的行程时间。

Attention:TLE,由时间转化成空间……就是先打表

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 #define maxn 100008
 9 
10 int f[maxn], num[maxn], Harsh[maxn];
11 
12 struct node
13 {
14     int u, v, d;
15 }P[maxn];
16 
17 int found(int x)
18 {
19     if(f[x] != x)
20         f[x] = found(f[x]);
21     return f[x];
22 }
23 
24 int cmp(node a, node b)
25 {
26     return a.d < b.d;
27 }
28 
29 int main()
30 {
31     int c, n, m, q, s, maxx;
32     scanf("%d", &c);
33     while(c--)
34     {
35         maxx = 0;
36         scanf("%d%d%d", &n, &m, &q);
37         for(int i = 0; i <= n; i++)
38             f[i] = i, num[i] = 1;    // num最开始初始化为1……
39         memset(Harsh, 0, sizeof(Harsh));
40 
41         for(int i = 0; i < m; i++)
42         {
43             scanf("%d%d%d", &P[i].u, &P[i].v, &P[i].d);
44             maxx = max(maxx, P[i].d);
45         }
46         sort(P, P+m, cmp);   // 处理前应先排序,
47         for(int i = 0; i < m; i++)
48         {
49             int x = found(P[i].u), y = found(P[i].v);
50             if(x != y)   // 如果在一个集合,说明已经算过了,就不用算了
51             {
52                 f[x] = y;
53                 Harsh[P[i].d] += num[x]*num[y]*2;
54                 num[y] += num[x];
55             }
56         }
57         for(int i = 1; i <= maxx; i++)
58             Harsh[i] += Harsh[i-1];
59         while(q--)
60         {
61             scanf("%d", &s);
62             if(s > maxx)
63                 printf("%d
", Harsh[maxx]);   // 注意
64             else
65                 printf("%d
", Harsh[s]);
66         }
67     }
68     return 0;
69 }
原文地址:https://www.cnblogs.com/Tinamei/p/4815306.html