一个链接:https://ac.nowcoder.com/acm/contest/82/E
来源:牛客网

无向图中的最短距离
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

有一个n个点的无向图,有m次查询,每次查询给出一些(xi,yi)

令dist(x,y)表示x和y点在图中最短距离,dist(x,x)=0,如果x,y不连通则dist(x,y) = inf

每次查询图中有多少个点v与至少一个这次询问给出的(xi,yi)满足dist(v,xi)<=yi

输入描述:

第一行三个数表示n,m,q

之后m行每行两个数x,y表示有一条x与y之间的边,边权为1

之后q次询问,每个询问先给你一个数a

之后一行2a个数,第2i-1个数xi和第2i个数yi表示一个二元组(xi,yi

输出描述:

输出q行,每行一个数表示这次询问的答案
示例1

输入

复制

5 6 6
2 3
1 3
2 5
1 3
3 2
2 5
1
3 1
1
1 1
1
1 4
1
5 2
1
1 4
2
1 0 5 1

输出

复制

3
2
4
3
4
3

备注:

对于100%的数据,n <= 1000 , m <= 100000 , q <=
100000
a的和<= 2100000 题意:
给你一个有向图,然后q个询问,每一个询问给你num个对数x,y 。
问你在图中有多少个节点与这num对个数中的至少一对中的x的距离小于等于y。 思路:
首先对于每一个节点进行bfs找到它对其他节点的最短路。
用数组 dis[i][j] 来维护 i到j的距离,
那么我们定理一个 bitset<1011> bs[1011][1011]
bs[i][j][k] 代表 第i个节点距离小于等于j的有没有k,有没有只有两个状态,即0/1
所以用 bitset可以完美维护。
对于处理,
我们对每一个节点bfs后,更新 bs
repd(j,1,n)
{
  bs[i][dis[i][j]][j]=1;
}
又因为是 小于等于, 所以 bs[i][j][k] =1的话, 那么bs[i][j~n][k] 都为1.
这样我们就可以从1到n,用bitset 的或运算|=来更新使距离j更大的一定包含更小的情况。
然后对于询问,
我们只需要让一个全是0的bitset 去|= 那num个 bs[x][y] 即可。
最后得到的bitset中的1的个数就是答案数。 细节见代码:
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int dis[maxn][maxn];
bitset<maxn> bs[maxn][maxn];
int m;
int q;
bitset<maxn> res;
std::vector<int> G[maxn];
void bfs(int x)
{
dis[x][x] = ;
queue<int> q;
q.push(x);
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto y : G[u])
{
if (~dis[x][y])continue;
dis[x][y] = dis[x][u] + ;
q.push(y);
}
}
}
int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
// freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
memset(dis, -, sizeof(dis));
gbtb;
cin >> n >> m >> q;
int x, y;
repd(t, , m)
{
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
repd(i, , n)
{
bfs(i);
repd(j, , n)
{
bs[i][dis[i][j]][j] = ;
}
repd(j, , n)
{
bs[i][j] |= bs[i][j - ];
}
}
int num;
repd(t, , q)
{
cin >> num;
res.reset();
repd(i, , num)
{
cin >> x >> y;
res |= bs[x][y];
}
cout << res.count() << endl;
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

 

最新文章

  1. hibernate5.2需要的最少jar文件
  2. 面试题目——《CC150》Java
  3. 团队开发——冲刺2.e
  4. QT学习之路--深入了解信号槽
  5. mysql外键详解
  6. java web 学习二(Tomcat服务器学习和使用1)
  7. 我们一起学Windows Phone 8-01-开发环境搭建
  8. myeclipse、eclipse去掉无用的workSpace
  9. XDocument和XmlDocument的区别
  10. Python报错:SyntaxError: Non-ASCII character &#39;\xe5&#39; in file
  11. Morse Clock
  12. sqlserver存储过程学习笔记(一)基础知识篇(全)
  13. Prototype and Constructor in JavaScript
  14. 3.linux常用软件的安装方法
  15. Promise的两种处理异步的方式
  16. spark优化整理
  17. tensorflow_目标识别object_detection_api,RuntimeError: main thread is not in main loop,fig = plt.figure(frameon=False)_tkinter.TclError: no display name and no $DISPLAY environment variable
  18. Java如何处理运行时异常?
  19. 架构 : 三层架构、MVC、MVP、MVVM
  20. dvwa 源码分析(二) --- dvwaPage.inc.php分析

热门文章

  1. Linux内核中的cmpxchg函数
  2. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_3 mybatis连接池的分类
  3. mysql explain中的列
  4. 大sd卡 裂开了,写保护掉了。重新装好后,被写保护的解决办:
  5. text_to_be_present_in_element
  6. 【读书笔记】GitHub入门
  7. python+selenium浏览器截图
  8. express中app.use()使用方法
  9. /proc/cpuinfo 查看cpu信息
  10. zookeeper 分布式协调服务