Regional Changchun Online--Travel(最小生成树&& 并查集)
Travel
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1852 Accepted Submission(s): 641
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?
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 m
bidirectional 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 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.
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.
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
2
6
12
通过并查集计算新加入的,每次的计算公式为原来的s+合并后的两个集合数量乘积。。。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
#include <queue>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define eps 1e-8
#define INF 20000000005
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z);
#define rdLL(x) scanf("%I64d",&x)
#define MAXn 20005
#define MAXe 100005
#define QUEnum 5005
struct Node{
int sta,end,val;
}node[MAXe]; struct Que{
int queval , num;
}que[QUEnum]; int sett[MAXn]; int sett_find(int x)
{
if(sett[x]<0) return x;
return sett[x]=sett_find(sett[x]);
} bool cmp1(Node a,Node b){
return a.val<b.val;
} bool cmp2(Que a,Que b){
return a.queval<b.queval;
}
int main ()
{
int Case;
rd(Case);
while(Case--)
{
memset(sett,-1,sizeof(sett));
int sum[MAXn];
int res[QUEnum];
for(int i=0;i<MAXn;i++) sum[i]=1;
int n,e,quenum;
rd3(n,e,quenum); for(int i=0;i<e;i++)
rd3(node[i].sta,node[i].end,node[i].val); sort(node,node+e,cmp1); ///这里的排序不是+n 而是e 找了两个小时
for(int i=0;i<quenum;i++) {
rd(que[i].queval);
que[i].num=i;
} sort(que,que+quenum,cmp2);
int j=0,s=0; ///计数有多少能到达
for(int i=0 ; i<quenum ;i++) ///不能在这里添加j<e,因为大于的还需要计算
{
while( j<e && node[j].val <= que[i].queval ){
int a=node[j].sta,b=node[j].end; int p = sett_find(a);
int q = sett_find(b);
if(p!=q){
s += (sum[q]*sum[p]); ////((sum[q]*(sum[q]+1))>>1) - ((sum[p]*(sum[p]+1))>>1) +
sett[q] = p;
sum[p]=sum[q]+sum[p]; ///p作为根节点
}
j++;
}
res[ que[i].num ] = (s<<1);
}
for(int i=0;i<quenum;i++)
printf("%d\n",res[i]);
}
return 0;
}
Regional Changchun Online--Travel(最小生成树&& 并查集)的更多相关文章
- UVA 1395 苗条的生成树(最小生成树+并查集)
苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...
- CSP 201703-4 地铁修建【最小生成树+并查集】
问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...
- HDU 5441——Travel——————【并查集+二分查界限】
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 【BZOJ-1576】安全路径Travel Dijkstra + 并查集
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1044 Solved: 363[Sub ...
- 关于最小生成树(并查集)prime和kruskal
适合对并查集有一定理解的人. 新手可能看不懂吧.... 并查集简单点说就是将相关的2个数字联系起来 比如 房子 1 2 3 4 5 6 ...
- 【BZOJ4144】[AMPPZ2014]Petrol(最短路+最小生成树+并查集)
Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油站可以补满. q次询问,每次给出x,y,b,表示出发点是 ...
- bzoj 3559: [Ctsc2014]图的分割【最小生成树+并查集】
读题两小时系列-- 在读懂题意之后,发现M(c)就是c这块最大权割边也就是的最小生成树的最大权边的权值,所以整个问题都可以在MST的过程中解决(M和c都是跟着并查集变的) 不过不是真的最小生成树,是合 ...
- HDU5441 Travel 离线并查集
Travel Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, ...
- HDU 5441 Travel (并查集+数学+计数)
题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u ...
随机推荐
- C#动态数组ArrayList和List<T>的比较
C#中一维动态数组(即列表)分ArrayList和List<T>两种,其容量可随着我们的需要自动进行扩充 一.ArrayList类(少用) ArrayList位于System.Collec ...
- NFC会员管理-转载自http://technews.cn/2014/09/13/nfc-sticker/
基隆的百年名店“李鹄饼店”误用馊水油,客人纷纷上门退货,因退货条件宽松,客人一货两退,造成巨大的损失.为了平息客人的愤怒,店家允许客人凭发 票或商品办理退货,有的客人先用发票退一次钱,再用商品退一次钱 ...
- scala vs java 相同点和差异
本贴是我摘抄自国外网站,用作备忘,也作为分享! Similarities between Scala and Java Following are some of the major similari ...
- [原]在Fedora中编译Libevent测试实例
在我的昨天的博文<[原]我在Windows环境下的首个Libevent测试实例>中介绍了在Windows环境下如何编译一个echo server例子.今天我又试了一下在Linux环境中编译 ...
- .NET,Cookie,写Cookie,取Cookie
Cookie是一段文本信息,在客户端存储 Cookie 是 ASP.NET 的会话状态将请求与会话关联的方法之一.Cookie 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一 ...
- Angular学习(1)
天天都是hello world,老子玩1+1. 最简单的例子,见证无聊的时刻: <!DOCTYPE html> <html> <head> <title> ...
- HackerRank "Chocolate in Box" !
XOR -> 0 is the key (make it even pair): http://www.cnblogs.com/lautsie/p/3908006.html Something ...
- [工具开发] Perl 爬虫脚本--从美国国家漏洞数据库抓取实时信息
一.简介 美国国家漏洞数据库收集了操作系统,应用软件的大量漏洞信息,当有新的漏洞出现时,它也会及时发布出来. 由于信息量巨大,用户每次都需要到它的网站进行搜索,比较麻烦.如果能有个工具,每天自动分析它 ...
- 黄聪:让WordPress主题支持语言本地化(使用poedit软件实现中文翻译功能)
如果你的WordPress主题要提交到WordPress官方主题库,使用者来自世界各地的多种语言,那么,你就要让你的WordPress主题支持语言本地化,方便使用者进行语言翻译和制作语言包. 让Wor ...
- POJ 2393 贪心 简单题
有一家生产酸奶的公司,连续n周,每周需要出货numi的单位,已经知道每一周生产单位酸奶的价格ci,并且,酸奶可以提前生产,但是存储费用是一周一单位s费用,问最少的花费. 对于要出货的酸奶,要不这一周生 ...