大意:给一张图,每个图上有一个数,问以每个点为源点,经过的点包含k种数字的最小距离。

显然跑最短路会T,但我们注意到边权一定。某次学校考试就是类似题,可以bfs做,复杂度O(n),每种货物做一次,复杂度O(kn),n=1e5,k=100,稳了。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
using namespace std;
const int N=1e5+8;
int n,m,ecnt,head[N],k,s,dis[N][105],cnt;
struct Edge{int to,nxt;}e[N<<1];
void add(int bg,int ed){e[++ecnt].to=ed;e[ecnt].nxt=head[bg];head[bg]=ecnt;}
vector<int>S[105];
void bfs(int x){
queue<int>q;
for(int i=0;i<S[x].size();i++)q.push(S[x][i]),dis[S[x][i]][x]=0;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u],v;i;i=e[i].nxt) {
v=e[i].to;
if (dis[v][x]>dis[u][x]+1) {dis[v][x]=dis[u][x]+1;q.push(v);}
}
}
}
int main() {
memset(dis,0x3f,sizeof dis);
scanf("%d%d%d%d",&n,&m,&k,&s);
for(int i=1,x;i<=n;i++) {scanf("%d",&x);S[x].push_back(i);}
for(int i=1,u,v;i<=m;i++){scanf("%d%d",&u,&v);add(u,v);add(v,u);}
for(int i=1;i<=k;i++)
bfs(i);
for(int i=1;i<=n;i++) {
// int mn=0x3f3f3f3f;
sort(dis[i]+1,dis[i]+1+k);
int ans=0;
for(int j=1;j<=s;j++) ans+=dis[i][j];
printf("%d ",ans);
}
}

[CodeForces]986A Fair的更多相关文章

  1. CodeForces - 986A Fair (BFS+贪心)

    题意:有N个点M条边的无向图,每个点有给定的ai(1<=ai<=K,K<=200)表示该点拥有的物品编号,保证1-K在N个点全部出现.求每个点收集S个不同的物品所要走过的最短路程(边 ...

  2. Codeforces 986A. Fair(对物品bfs暴力求解)

    解题思路: 1.对物品i bfs,更新每个小镇j获得每个物品i的最短距离. 2.时间复杂度o(n*k),满足2s的要求. 代码: #include <iostream> #include ...

  3. [Codeforces 864A]Fair Game

    Description Petya and Vasya decided to play a game. They have n cards (n is an even number). A singl ...

  4. CF 986A Fair——多源bfs

    题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...

  5. CodeForces - 987D Fair (BFS求最短路)

    题意:有N个城市,M条双向道路连接两个城市,整个图保证连通.有K种物品,但每个城市只有一种,现在它们都需要S种物品来举办展览,可以去其他城市获取该城市的物品,花费是两城市之间的最短路径长度.求每个城市 ...

  6. CF 986A Fair(多源BFS)

    题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...

  7. 【反向多源点同时BFS一个强连通图+类最短路题面】Fair-986C-Codeforce

    借鉴博客:https://www.cnblogs.com/zhangjiuding/p/9112273.html 986-A. Fair /* 986-A-Fair,codeforce: 大致题意: ...

  8. codeforces 897A Scarborough Fair 暴力签到

    codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...

  9. Codeforces Round #485 (Div. 2) D. Fair

    Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...

随机推荐

  1. innodb_support_xa=1

    mysql> show create table t; CREATE TABLE `t` ( `id` ) NOT NULL AUTO_INCREMENT, `num` ) DEFAULT NU ...

  2. http400错误基本都是http请求参数与服务器接收参数不匹配

    http400错误基本都是http请求参数与服务器接收参数不匹配造成的, 如:1)post请求,你发了个get请求 2)content-type指定不匹配致使参数无法读出来

  3. [React] Capture values using the lifecycle hook getSnapshotBeforeUpdate in React 16.3

    getSnapshotBeforeUpdate is a lifecycle hook that was introduced with React 16.3. It is invoked right ...

  4. Tomcat扩展——监控

    (转过来,源地址:http://www.jmatrix.org/notes/1067.html) 近期心血来潮.想能否够通过添加一个tomcat的扩展,来持续收集tomcatserver本身的性能信息 ...

  5. POJ3090 Visible Lattice Points 欧拉筛

    题目大意:给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 线y=x和坐标轴上的点都被(1,0)(0,1)(1,1)挡住了.除这三个钉子外,如果一个点(x,y)不互质,则 ...

  6. Linux USB 驱动开发(一)—— USB设备基础概念【转】

    本文转载自:http://blog.csdn.net/zqixiao_09/article/details/50984074 在终端用户看来,USB设备为主机提供了多种多样的附加功能,如文件传输,声音 ...

  7. Keras简单使用

    Keras简单使用在keras中建立模型测试自己的图片一些有用的函数(持续更新) Keras简单使用 在keras中建立模型 相对于自己写机器学习相关的函数,keras更能快速搭建模型,流程如下: 通 ...

  8. Codeforces Round #438 (Div.1+Div.2) 总结

    本来兴致勃勃的想乘着这一次上紫,于是很早很早的到了机房 但是好像并没有什么用,反而rating-=47 Codeforces Round #438(Div.1+Div.2) 今天就这样匆匆的总结一下, ...

  9. Codeforces Round #198 (Div. 2)E题解

    E. Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all ...

  10. C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...