博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3938 Portal(离线+Kruskal+并查集)
阅读量:4073 次
发布时间:2019-05-25

本文共 1767 字,大约阅读时间需要 5 分钟。

链接:

题目:

Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length 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.
 

Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains 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).
 

Output
Output the answer to each query on a separate line.
 

Sample Input
10 10 107 2 16 8 34 5 85 8 22 8 96 4 52 1 58 10 57 3 77 8 810615918276
 

Sample Output
36131133613621613

分析与总结:

做这题学到了什么是“离线算法”的概念。所谓“离线”,就是把所有的数据都输入之后再计算,“在线”就是边输入边计算。

用在这题中,是因为输入中的“询问部分”,有Q 个问,每个L可以有多少种不同路径。由于大的L必定会包含到小的L, 所以把所有问题都输入,再从大到小排序,再计算,可以减少很多计算量。

这题还需要用到的是并查集中的“权值”, 用rank数组表示,也就是某个棵树k有rank【k】个结点。同一个树之间的点都是连通的,任何点都可以通往其它的任意点, 那么当两颗树合并成一棵树时, 将会增加rank[a]*rank[b]条路径。

代码:

#include
#include
using namespace std;#define N 10005int f[N], rank[N], ans[N], n, m, Q;struct Edge{ int u, v, val; friend bool operator < (const Edge&a,const Edge&b){ return a.val < b.val; }}arr[N*5];struct Query{ int id, L; friend bool operator<(const Query&a,const Query&b){ return a.L

——  生命的意义,在于赋予它意义。  
原创 , By D_Double (转载请标明)

你可能感兴趣的文章
事件冒泡阻止event.stopPropagation()
查看>>
Flex4 beta 的 Spark 布局
查看>>
Spark 架构和组件集的简要概述
查看>>
关于flex4中文(zh_CN)本地化应用编译不通过的解决方法
查看>>
摩斯密码表
查看>>
一段摩斯密码里的爱情故事
查看>>
游戏测试的技术难点和测试技术
查看>>
线程简介
查看>>
线程挂起自己,让出CPU
查看>>
线程同步(C# 编程指南)
查看>>
创建高效的线程安全类的步骤
查看>>
Failed to load class "org.slf4j.impl.StaticLoggerB
查看>>
使用 Apache MINA 2 开发网络应用
查看>>
MANIFEST.MF文件的格式
查看>>
NIO入门-了解Buffer
查看>>
database如何管理超过4GB的文件
查看>>
[转载]java.util.concurrent.ConcurrentHashMap 如何在不损失线程安全的同时提供更高的并发性...
查看>>
sun game server (sgs)初探
查看>>
類別 ConcurrentHashMap<K,V>的更新,删除
查看>>
如何使用Flex 4新的CSS语法,兼容halo组件
查看>>