Acwing 5475. 聚会 ( BFS )

https://www.acwing.com/problem/content/5478/

输入样例1:
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
输出样例1:
2 2 2 2 3
输入样例2:
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
输出样例2:
1 1 1 2 2 1 1
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC target("avx")
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const LL MAXN=1e18,MINN=-MAXN,INF=0x3f3f3f3f;
const LL N=100200,M=2020;
LL n,m,k,s;
LL a[N];
vector<LL> g[N],v[N];
LL dist[N][110];//干草到某个地点的距离
queue<int> q;
void bfs()
{
    memset(dist,-1,sizeof dist);
    for(int i=1;i<=k;i++)//k种干草
    {
        for(auto vv:v[i])//存储的是地点
        {
            q.push(vv);
            dist[vv][i]=0;//标记每个节点到该存量的位置都是0
        }
        //添加进来一种干草就迅速更新其它地点的距离
        while(q.size())
        {
            auto t=q.front();//抛出一个地点
            q.pop();
            for(auto gg:g[t])//该地点的连接边
            {
                if(dist[gg][i]==-1)//未标记
                {
                    dist[gg][i]=dist[t][i]+1;
                    q.push(gg);
                }
            }
        }
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m>>k>>s;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            v[a[i]].push_back(i);//装草粮的位置
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;
            cin>>x>>y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        bfs();
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            ans=0;
            sort(dist[i]+1,dist[i]+1+k);
            for(int j=1;j<=s;j++)//一共需要s种干草
            {
                ans+=dist[i][j];
            }
            cout<<ans<<" ";
        }
        cout<<endl;
    }
    return 0;
}
posted @ 2024-03-30 16:15  Vijurria  阅读(8)  评论(0编辑  收藏  举报