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=1000+100;
struct Edge
{
    int u,v;
    int dis;
}e[200*maxn];
vector<int>g[maxn];
int tot;
int flag[maxn],dis[maxn];
int n,L;

void SPFA(int s)
{
    queue<int>Q;
    memset(flag,0,sizeof flag);
    for(int i=0;i<=n;i++) dis[i]=INF;
    Q.push(s); flag[s]=1; dis[s]=0;
    while(!Q.empty())
    {
        int head=Q.front(); Q.pop(); flag[head]=0;
        for(int i=0;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]==0)
                {
                    flag[e[id].v]=1;
                    Q.push(e[id].v);
                }
            }
        }
    }
}

int main()
{
    tot=0;
    scanf("%d%d",&n,&L);
    for(int i=1;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=1;
            g[id].push_back(tot);
            tot++;
        }
    }
    int k; scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        int id; scanf("%d",&id);
        SPFA(id);
        int ans=0;
        for(int i=1;i<=n;i++)
            if(i!=id&&dis[i]<=L) ans++;
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-26 20:32:47

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

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

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 Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with follo

PAT 1076. Forwards on Weibo (30)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1076 此题考查图的遍历操作: 本来我个人觉得可以用dfs的,但是不知何故,有两个case没有过,贴出代码,望指出错误之处: #include <cstdio> #include <map> #include <vector> #include <memory.h> using namespace std; const int NUM=1001; bool

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 may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followe

PAT:1076. Forwards on Weibo (30) AC

#include<cstdio> #include<cstring> #include<queue> #include<vector> using namespace std; const int MAX=1010; bool tag[MAX]; //标记BFS是是否被访问过 struct node { int ID; //编号 int layer; //层号 }; vector<node> Adj[MAX]; //邻接表,每个位置都是一个nod

1076. Forwards on Weibo (30)

时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers

PAT (Advanced Level) 1095. Cars on Campus (30)

模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<cstring> #include<stack> #include<vector> #include<iostream> using namesp

PAT (Advanced Level) 1018. Public Bike Management (30)

先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; co

PAT (Advanced Level) 1014. Waiting in Line (30)

简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> using namespace std; struct X { int st; int len; int en; }p[1500]; queue<int>Q[2