最短路。

每次询问的点当做起点,然后算一下点到其余点的最短路。然后统计一下最短路小于等于L的点有几个。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=+;
struct Edge
{
int u,v;
int dis;
}e[*maxn];
vector<int>g[maxn];
int tot;
int flag[maxn],dis[maxn];
int n,L; void SPFA(int s)
{
queue<int>Q;
memset(flag,,sizeof flag);
for(int i=;i<=n;i++) dis[i]=INF;
Q.push(s); flag[s]=; dis[s]=;
while(!Q.empty())
{
int head=Q.front(); Q.pop(); flag[head]=;
for(int i=;i<g[head].size();i++)
{
int id=g[head][i];
if(dis[head]+e[id].dis<dis[e[id].v])
{
dis[e[id].v]=dis[head]+e[id].dis;
if(flag[e[id].v]==)
{
flag[e[id].v]=;
Q.push(e[id].v);
}
}
}
}
} int main()
{
tot=;
scanf("%d%d",&n,&L);
for(int i=;i<=n;i++)
{
int m; scanf("%d",&m);
while(m--)
{
int id; scanf("%d",&id);
e[tot].u=id; e[tot].v=i; e[tot].dis=;
g[id].push_back(tot);
tot++;
}
}
int k; scanf("%d",&k);
for(int i=;i<=k;i++)
{
int id; scanf("%d",&id);
SPFA(id);
int ans=;
for(int i=;i<=n;i++)
if(i!=id&&dis[i]<=L) ans++;
printf("%d\n",ans);
}
return ;
}

PAT (Advanced Level) 1076. Forwards on Weibo (30)的更多相关文章

  1. 【PAT甲级】1076 Forwards on Weibo (30 分)

    题意: 输入两个正整数N和L(N<=1000,L<=6),接着输入N行数据每行包括它关注人数(<=100)和关注的人的序号,接着输入一行包含一个正整数K和K个序号.输出每次询问的人发 ...

  2. 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...

  3. PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)

    1076 Forwards on Weibo (30分)   Weibo is known as the Chinese version of Twitter. One user on Weibo m ...

  4. PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]

    题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...

  5. PAT 1076. Forwards on Weibo (30)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  6. PAT甲题题解-1076. Forwards on Weibo (30)-BFS

    题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...

  7. 1076. Forwards on Weibo (30)

    时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...

  8. 1076. Forwards on Weibo (30) - 记录层的BFS改进

    题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...

  9. 1076 Forwards on Weibo (30)(30 分)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

随机推荐

  1. 更改web project 访问项目名称

    1.新建web project 2.右键该项目名称------properties 3.访问该项目的URL http://localhost:8806/ssm/.......... 相比书写整个项目名 ...

  2. hdu_4046_Panda(树状数组)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 题意:一大堆篇幅介绍,跳过直奔主题,让你寻找给定区间的“wbw”的个数. 题解:直接上树状数组, ...

  3. what a malloc has to do

    1) Allocate a chunk of memory big enough to satisfy the request, and return a pointer to it.2) Remem ...

  4. js判断当前时间前几天和格式校验

    addday天后的日期 function time(addday) { var now= new Date(); nowdate=now.getDate(); //alert(nowdate); no ...

  5. 【多重背包模板】poj 1014

    #include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 us ...

  6. jquery的校验规则的方法

    //validate 选项*********************************************************** $("form").validat ...

  7. Java Interrupt Related

    In Java, the main process can have several threads at a time, but only a few of them can run concurr ...

  8. Swift POP+MVVM

    Swift2.0中引入了协议扩展的特性,并且建议开发者一切从协议(Protocol)出发,经过几个月的学习探索,博主发现Swift作为一门面向协议编程(POP)的语言非常适合时下火热的MVVM架构.M ...

  9. Android自动化测试之环境搭建

    Android自动化测试之环境搭建 一.Android-sdk介绍 SDK(Software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台. ...

  10. JS-DOM元素灵活查找

    用className选择元素 封装成函数 <title>无标题文档</title> <script> /* window.onload=function () { ...