hdu Travel
2015-09-13 19:56
274 查看
Travel
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 46 Accepted Submission(s): 20
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<
4000
/span> 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
这道题的大概意思:输入T表示T组测试数据,输入n m k 表示有n个点,m条边,k次询问。接下来输入m条边,a b c ,表示a到b权值为c。两点之间最多一条边。然后输入k个询问每次询问为一个数s,问你有多少个点集(a,b)使得经过的边的权值都不大于s,注意(a,b)和(b,a)是不同点集。
思路:
一看到这道题的数据,我第一感觉是用线段树,因为涉及到信息的查询量非常大,但是想了想又觉得用线段树不合适,最后觉得用并查集来解。首先这里要了解在一个有n个点的连通图中,假如每条边都是小于s的,那么这个连通图中有n*(n-1)条符合要求的点集。这样我们就可以先按边权值的大小排序一遍,然后不断地将边加入集合中,当我们加入第x条边时,前面的边的权值都是小于第x条边的,只要将它们构成的每个联通图的点的个数统计好,就能得出经过边的权值都不大于第x条边的权值的点集了。
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
struct edg{
int x,y;
int value;
};
edg edgs[100005];//保存每一条边。
bool cmp(edg a, edg b)
{
return a.value < b.value;
}
int n,m,k;
long long ans[100005];//保存答案
long long sum;
int num[20005][2];
int fnd(int s)//判断s点所属的集合
{
if ( num[s][0] == s ) return s;
else return fnd(num[s][0]);
}
void gb(int a,int b)//这一步是必须的不然会超时,降低查找所属集合时的时间复杂度
{
if ( b != num[b][0] ) gb(a,num[b][0]);
num[b][0] = a;
}
void join()
{
int a,b;
for(int i = 0; i < m; i ++ )
{
a = fnd(edgs[i].x);
b = fnd(edgs[i].y);
if ( a != b )//如果这条边的两个点不属于同一个连通图
{
sum -= num[b][1]*(num[b][1]-1);
sum -= num[a][1]*(num[a][1]-1);
sum += (num[a][1]+num[b][1])*(num[a][1]+num[b][1]-1);
num[a][1] += num[b][1];
num[b][0] = a;
gb(a,edgs[i].y);
}
ans[edgs[i].value] = sum;//将答案保存起来
}
int s = 0;
for(int i = 0; i < 100005; i ++ )
{
if ( ans[i] > s ) s = ans[i];
ans[i] = s;
}
}
int main()
{
int T,a,b,value,w;
scanf("%d",&T);
while ( T-- )
{
scanf("%d%d%d",&n,&m,&k);
for(int i = 0; i < m; i ++ )
{
scanf("%d%d%d",&edgs[i].x,&edgs[i].y,&edgs[i].value);
}
sort(edgs,edgs+m,cmp);
memset(ans,0,sizeof(ans
8b61
));
memset(num,0,sizeof(num));
for(int i = 0; i < n; i ++ )
{
num[i][0] = i;
num[i][1] = 1;
}
sum = 0;
join();
for(int i = 0; i < k; i ++ )
{
scanf("%d",&w);
if ( w >= 100005 )
printf("%lld\n",ans[100003] );
else printf("%lld\n",ans[w]);
}
}
return 0;
}
相关文章推荐
- cheng@Linux--JZ2440学习之SecureCRT/SecureFX
- Git学习之基础<1>
- cmd命令大全
- Mybatis 引用不同映射文件的结果集
- Android(java)学习笔记244:多媒体之SurfaceView
- 黑马程序员--------Java基础-----数组和类
- 数据结构—Java版链表相交问题的终极解决方案
- C++中的 cout 和cin
- 【软件工程】 之 测试维护
- 二叉树递归遍历与非递归遍历(终极版)
- OpenCV之响应鼠标(三):响应鼠标信息
- HTML及简单标签介绍
- 构造方法与重载
- eclipse配置maven
- 第2周项目3(1)——两种排序算法的运行时间
- 【leetcode】subsets
- java中使用sort()对ArrayList进行排序
- mysql出现Data truncated for column
- C学习之结构体
- 浙江大学PAT_乙级_1043. 输出PATest(20)