Travel

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 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
 
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define inf 100000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//****************************************
#define maxn 1000000+50 struct ss
{
int u,v,w;
bool operator <(const ss &x)const{
return w<x.w;
}
}a[maxn]; ll ans[maxn];
int num[maxn],parent[maxn],n,m,q; struct sss
{
int v,id;
bool operator <(const sss &x)const{
return v<x.v;
}
}Ans[maxn]; int finds(int x){
if(x!=parent[x])parent[x]=finds(parent[x]);
return parent[x];
}
void init()
{
FOR(i,,n){parent[i]=i;num[i]=;}
mem(ans);
}
int main()
{ int T=read();
while(T--)
{ scanf("%d%d%d",&n,&m,&q); FOR(i,,m)
{
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
}
sort(a+,a+m+);
// FOR(i,1,m){cout<<a[i].u<<" "<<a[i].v<<" "<<a[i].w<<endl;} FOR(i,,q)
{
scanf("%d",&Ans[i].v);
Ans[i].id=i;
} sort(Ans+,Ans+q+); init();
//FOR(i,1,q)cout<<Ans[i].v<<" "<<Ans[i].id<<endl;
ll aa=;
int j=;
FOR(i,,q)
{
while(j<=m&&Ans[i].v>=a[j].w)
{
int fx=finds(a[j].u);
int fy=finds(a[j].v);
if(fx!=fy)
{
parent[fy]=fx;
aa+=num[fx]*num[fy];
num[fx]+=num[fy];
}
j++;
}
ans[Ans[i].id]=aa*;
}
for(int i=;i<=q;i++)
printf("%I64d\n", ans[i]);
}
return ;
}

代码

给你一个图,n个点,m条边,并有边权值,q个询问,每个询问是一个限制值,问你多少对不同的(S,T)路径上的最小权值不超过这个限制值,(S,T)与(T,S)是不同的。

题解:离线并查集做,讲边按照权值排序,然后用并查集构造集合中最大权值逐渐增大的联通快,统计两个不同集合时,方案数就是节点数num[A]*num[B]
更新就好了
 

HDU5441 Travel 离线并查集的更多相关文章

  1. HDU5441 Travel (离线操作+并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  2. [bzoj1015](JSOI2008)星球大战 starwar(离线+并查集)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武 器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通 ...

  3. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  4. [USACO18FEB] Snow Boots G (离线+并查集)

    题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...

  5. 【BZOJ-1576】安全路径Travel Dijkstra + 并查集

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1044  Solved: 363[Sub ...

  6. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

  7. 【杭电OJ3938】【离线+并查集】

    http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  8. BZOJ4551 Tjoi2016&Heoi2016树(离线+并查集)

    似乎是弱化的qtree3.树剖什么的非常无脑.考虑离线.并查集维护每个点的最近打标记祖先,倒序处理,删除标记时将其与父亲合并即可. #include<iostream> #include& ...

  9. HDU 5441——Travel——————【并查集+二分查界限】

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

随机推荐

  1. Farseer.net轻量级开源框架 中级篇:事务的使用

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: Where条件的终极使用 下一篇:Farseer.net轻量级开源框架 中级篇: ...

  2. Farseer.net轻量级开源框架 中级篇:自定义配置文件

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 数据绑定 下一篇:Farseer.net轻量级开源框架 中级篇: 动态数据库访问 ...

  3. C/C++ static、extern

    一.static本质作用 与static相对的关键字是auto,两者是一对.我们一般声明变量,如:int a,其实都是auto int a,只是auto省略了而已,但是static不能省略.要理解st ...

  4. Computed Properties vs Property Requirements - protocol

    In addition to stored properties, classes, structures, and enumerations can define computed properti ...

  5. 并发编程学习笔记(3)----synchronized关键字以及单例模式与线程安全问题

    再说synchronized关键字之前,我们首先先小小的了解一个概念-内置锁. 什么是内置锁? 在java中,每个java对象都可以用作synchronized关键字的锁,这些锁就被称为内置锁,每个对 ...

  6. js获取微信版本号的方法

    微信现在几乎已经普遍在使用,但是微信版本一直在更新,不同的版本有不同的功能,于是了解微信的版本号很重要,以便自己及时更新体验新的功能.那么如何用js获取微信版本号呢? 一.判断微信版本号 ? 1 2 ...

  7. HEVC-HM16.9源码学习(1)TEncCu::xCompressCU

    函数入口:Void TEncSlice::compressSlice的m_pcCuEncoder->compressCtu( pCtu );调用xCompressCU( m_ppcBestCU[ ...

  8. ZOJ - 3985 - String of CCPC (思维 + 暴力)

    题意: 询问一共有有多少个CCPC,每个得1分,可以自己在任意位置添加字母,第i次添加需要耗费i-1分 思路: 既然每次添加需要耗分,添加第二个字母,相当于没有添加,所以只需要添加一次就好 先计算出原 ...

  9. 面试:B

    协程 材质和贴图 任意模块的深入理解

  10. 搭建一台功能简单的FTP服务器

    #vi /etc/sysconfig/network-scripts/ifcfg-eno33554952 #revice network restart #ping www.baidu.com #ip ...