PAT (Advanced Level) 1076. Forwards on Weibo (30)
最短路。
每次询问的点当做起点,然后算一下点到其余点的最短路。然后统计一下最短路小于等于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)的更多相关文章
- 【PAT甲级】1076 Forwards on Weibo (30 分)
题意: 输入两个正整数N和L(N<=1000,L<=6),接着输入N行数据每行包括它关注人数(<=100)和关注的人的序号,接着输入一行包含一个正整数K和K个序号.输出每次询问的人发 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PAT甲题题解-1076. Forwards on Weibo (30)-BFS
题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...
- 1076. Forwards on Weibo (30)
时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...
- 1076. Forwards on Weibo (30) - 记录层的BFS改进
题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...
- 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 ...
随机推荐
- APUE读书笔记:关于sigsuspend
sigsuspend是一个原子操作,为了防止信号丢失而存在的,具体含义看下函数原型. int sigsuspend(const sigset_t *mask); 先忽略参数,sigsuspend完成的 ...
- ios导航栏适配
我们做屏幕导航栏横竖屏适配的时候,会发现top的值多少都有一点的偏移,加了背景色之后从0开始,不加背景色从64开始,解决方法self.extendedLayoutIncludesOpaqueBars ...
- Eclipse报错 due to restriction on required library C:/Java/jdk1.7.51/jre/lib/rt.jar 解决方案
Eclipse报错 due to restriction on required library C:/Java/jdk1.6.0_10/jre/lib/rt.jar 解决方案 Eclipse 编译时 ...
- Apache开启gzip压缩传输
修改Apache配置文件 第一步,添加两个模块 LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module m ...
- 其他信息: Error creating context 'spring.root': 未能加载文件或程序集“EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项
详细错误情况: “System.Configuration.ConfigurationErrorsException”类型的异常在 Spring.Core.dll 中发生,但未在用户代码中进行处理 其 ...
- IOS传值之Block传值(二)
@interface QWViewController : UIViewController @property(nonatomic,strong)UILabel *label; @property( ...
- git 提交到github时不用每次都输入用户名,密码
Permanently authenticating with Git repositories, Run following command to enable credential caching ...
- java模式:深入单例模式
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://devbean.blog.51cto.com/448512/203501 在GoF ...
- 使用jstl标签遍历双层的map(map下面的map)
<c:forEach var="firstMap" items="${map}"> <c:forEach var="secondMa ...
- c++模板两个数的加法
1.最简单的情况: template<class T> T Add(const T& a, const T& b) { return a + b; } 缺点是不能够处理不同 ...