hdu3938 Portal
of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
7 2 1
6 8 3
4 5 8
5 8 2
2 8 9
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
10
6
1
5
9
1
8
2
7
6
13
1
13
36
1
36
2
16
题目要求a、b两点之间所有的路径中的最大边的最小值。
这题可以用并查集做,因为询问次数很多,所以要离线操作。设数组num[i],只记录i所在集合的总顶点数(一开始以为是记录边的,其实是记录顶点的),先对所有的边排序,从小到大,(其实可以看做是依次枚举最大边的最小值,因为如果这条线段的两端点不在一个集合,即两个集合的边不相连,那么这条线段就起到了桥梁的作用,也就是说任意两个集合的点相连都要通过这条边,因为之前两个集合的最大边的最小值都符合条件,那么如果这条线段也符合条件,就可以把两个集合的点都连起来),因为能量大的值一定包括能量小的路径,所以每次初始化的时候p[i].sum=p[i-1].sum;
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int pre[10006],num[10006];
struct edge
{
int from,to,len;
}e[50006];
struct node
{
int id,num,sum;
}q[10006];
bool cmp1(edge a,edge b)
{
int temp;
if(a.len>b.len){
temp=a.from;a.from=b.from;b.from=temp;
temp=a.to;a.to=b.to;b.to=temp;
return a.len<b.len;
}
return a.len<b.len;
}
bool cmp2(node a,node b)
{
int temp;
if(a.num>b.num){
temp=a.id;a.id=b.id;b.id=temp;
return a.num<b.num;
}
return a.num<b.num;
}
bool cmp3(node a,node b)
{
int temp;
if(a.id>b.id){
temp=a.sum;a.sum=b.sum;b.sum=temp;
return a.id<b.id;
}
return a.id<b.id;
}
int find(int x)
{
int r=x,i,j=x;
while(r!=pre[r])r=pre[r];
while(j!=pre[j]){
i=pre[j];
pre[j]=r;
j=i;
}
return r;
}
int main()
{
int n,m,i,j,a,b,c,p,t1,t2,ans;
while(scanf("%d%d%d",&n,&m,&p)!=EOF)
{
for(i=0;i<=n;i++){
pre[i]=i;num[i]=1;
}
for(i=1;i<=m;i++){
scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].len);
}
sort(e+1,e+1+m,cmp1);
for(i=1;i<=p;i++){
scanf("%d",&q[i].num);
q[i].id=i;
}
sort(q+1,q+p+1,cmp2);
ans=1;
q[0].sum=0;
for(i=1;i<=p;i++){
q[i].sum=q[i-1].sum;
while(ans<=m && e[ans].len<=q[i].num){
t1=find(e[ans].from);t2=find(e[ans].to);
if(t1==t2){
ans++;continue;
}
pre[t1]=t2;
q[i].sum+=num[t1]*num[t2];
num[t2]+=num[t1];
ans++;
}
}
sort(q+1,q+1+p,cmp3);
for(i=1;i<=p;i++){
printf("%d\n",q[i].sum);
}
}
return 0;
}
hdu3938 Portal的更多相关文章
- hdu3938 Portal 离线+并查集
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ...
- hdu3938 Portal 离线的并查集
离线算法是将全部输入都读入,计算出所有的答案以后再输出的方法.主要是为避免重复计算.类似于计算斐波那契数列的时候用打表的方法. 题目:给一个无向图,求有多少个点对,使得两点间的路径上的花费小于L,这里 ...
- Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务
<Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...
- 安装Portal for ArcGIS时如何正确配置HTTPS证书
SSL协议位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持.SSL协议可分为两层: SSL记录协议(SSL Record Protocol):它建立在可靠的传输协议(如TCP)之上,为 ...
- AEAI Portal V3.5.4升级说明,门户集成平台
1 总体说明 本次升级是AEAI Portal的一次重要升级,主要扩展了开发社区(论坛).移动门户功能,同时修正一些功能BUG等,具体内容如下: 2 升级内容 功能扩展: 扩展开发社区导航 扩展移动门 ...
- 手动删除portal中托管服务。
在portal中将server作为托管联合服务器,然后发布了托管服务.若中间取消了托管联合服务器,再重新连接,那么会出现之前的托管服务无法删除的现象. 下文为怎样手动删除这些服务的方法,(不过貌似之后 ...
- server与Portal联合,portal许可过期无法登录。
server与Portal联合,portal许可过期无法登录,怎样解除联合. 解决方案由峥姐友情提供~~ (1)删除有两种方式(①/②选其一即可): ①类似 链接ttps://sunl.esrichi ...
- UNDER THE HOOD OF THE NEW AZURE PORTAL
http://jbeckwith.com/2014/09/20/how-the-azure-portal-works/ So - I haven’t been doing much blogging ...
- Camstar Portal modeling user guid --设置本地时间
登陆--studio --portal settings -- timezone settings 里面有EST 和EDT两个时间. 现在要设置为北京时间(08:00) 原文: Localizatio ...
随机推荐
- 【C++】《C++ Primer 》第十九章
第十九章 特殊工具与技术 一.控制内存分配 1. 重载new和delete new表达式的工作机理: string *sp = new string("a value"); //分 ...
- stat filename
查看文件的mtime,atime,ctime 3个时间
- SAPLink 非常好用的工具
对于SAP LINK,如果你想将一个程序完整的保存到本地,包括程序的自定义屏幕.菜单等等,那么请使用这个工具,它能够将一个程序完整的保存下来,并且移植到另一个SAP系统中,用来左程序的迁移和本地保存备 ...
- 手动验证MySQL Innodb RR级别加锁 需要注意的几个点
记录几个坑 优化器在表行数比较少的时候 会使用全表扫描,会造成全表所有的行加锁,所以需要使用force index 强制使用索引 来实现gap-lock(间隙锁)的应用 next-lock 加锁 会锁 ...
- 浅析鸿蒙中的 Gn 与 Ninja(一)
目录: Ninja简介 make 的 3 个特性 举例说明Ninja 的用法 如何向构建工具 Ninja 描述构建图 后记 鸿蒙系统的编译构建是基于 Gn 和 Ninja 完成的,那么 Gn 和 Ni ...
- jQuery 真伪数组的转换
//真数组转换伪数组 var arr = [1,3,5,7,9]; var obj = {}; [].push.apply(obj,arr); console.log(obj) //伪数组转真数组 v ...
- (转)iOS工具--CocoaPods 安装使用总结
本文转载自:CocoaPods 安装使用总结(最新) 一.前言 关于什么是CocoaPods,使用CocoaPods的好处等问题本文不做说明,本文只是CocoaPods的安装和使用教程.根据此教程可以 ...
- 找不到:DarchetypeCatalog=local
设置IDEA Maven->Runner 界面的VM Options参数值为-DarchetypeCatalog=local 刷新项目Maven配置,在项目右边界面,重新引入Maven
- 回滚原理 Since database connections are thread-local, this is thread-safe.
mysql django 实践: django @transaction.atomic 机制分析 1.数据库清空表Tab 2.请求django-view @transaction.at ...
- redis6.0多线程
https://www.sohu.com/a/331991216_268033 执行还是单线程 读写解析多线程 6.0 https://segmentfault.com/a/1190000 ...