hdu Travel
来源:互联网 发布:如何使用淘宝积分 编辑:程序博客网 时间:2024/06/08 17:00
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 integersn,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 followingm 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.
Thenq lines follow. Each of them is a query consisting of an integer x where x is the time limit before Jack goes berserk.
For each test case, the first line consists of three integers
Each of the following
Then
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.
Note that
Sample Input
15 5 32 3 63341 5 157243 5 57054 3 123821 3 2172660001000013000
Sample Output
2612
这道题的大概意思:输入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));
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;
}
#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));
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;
}
0 0
- hdu Travel
- hdu-travel
- Travel HDU
- Travel HDU
- Travel HDU
- hdu 1208 pascal travel
- HDU 3053 Group Travel
- hdu 4284 Travel
- HDU 4284 Travel
- hdu 2433 TRAVEL
- hdu 4118 Time travel
- HDU 4284 Travel
- hdu 4418 Time travel
- HDU 4284 Travel
- HDU 4418 Time travel
- HDU-5441 Travel
- hdu 5441 Travel
- HDU 5441 Travel
- 【leetcode】subsets
- 第2周项目3(1)——两种排序算法的运行时间
- C++中的 cout 和cin
- 数据结构—Java版链表相交问题的终极解决方案
- 黑马程序员--------Java基础-----数组和类
- hdu Travel
- cmd命令大全
- Git学习之基础<1>
- 刚开始学习测试的小感触
- Mybatis 引用不同映射文件的结果集
- 微策略2011校园招聘笔试题(找出数组中两个只出现一次的数字)
- ERROR JDBCExceptionReporter:234 - Table 'xxxxxxxx' doesn't exist hibernate无法自动创建表
- Java 加载配置文件的方式
- 数据库确保远程访问