[CodeForces]986A Fair
大意:给一张图,每个图上有一个数,问以每个点为源点,经过的点包含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的更多相关文章
- CodeForces - 986A Fair (BFS+贪心)
题意:有N个点M条边的无向图,每个点有给定的ai(1<=ai<=K,K<=200)表示该点拥有的物品编号,保证1-K在N个点全部出现.求每个点收集S个不同的物品所要走过的最短路程(边 ...
- Codeforces 986A. Fair(对物品bfs暴力求解)
解题思路: 1.对物品i bfs,更新每个小镇j获得每个物品i的最短距离. 2.时间复杂度o(n*k),满足2s的要求. 代码: #include <iostream> #include ...
- [Codeforces 864A]Fair Game
Description Petya and Vasya decided to play a game. They have n cards (n is an even number). A singl ...
- CF 986A Fair——多源bfs
题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...
- CodeForces - 987D Fair (BFS求最短路)
题意:有N个城市,M条双向道路连接两个城市,整个图保证连通.有K种物品,但每个城市只有一种,现在它们都需要S种物品来举办展览,可以去其他城市获取该城市的物品,花费是两城市之间的最短路径长度.求每个城市 ...
- CF 986A Fair(多源BFS)
题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...
- 【反向多源点同时BFS一个强连通图+类最短路题面】Fair-986C-Codeforce
借鉴博客:https://www.cnblogs.com/zhangjiuding/p/9112273.html 986-A. Fair /* 986-A-Fair,codeforce: 大致题意: ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
- Codeforces Round #485 (Div. 2) D. Fair
Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...
随机推荐
- Welcome to the Real World
 Welcome to the Real World Gregor Hohpe EnginEERS liKE pRECiSion, especially software engineers who ...
- 怎样从C++代码直接訪问android framework层的WifiService
说究竟,Java层的service就是就C++层的binder的封装.所以从原理上来讲通过C++代码直接訪问android framework层的service是全然可能的,这篇文章以訪问WifiSe ...
- 【c语言】 模拟实现库函数的atoi函数
// 模拟实现库函数的atoi函数 #include <stdio.h> #include <string.h> #include <assert.h> #incl ...
- Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft.Build.En
错误具体信息: Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft. ...
- Android解决使用findViewById时须要对返回值进行类型转换问题的辅助类
在我们的开发工作时,findViewById可能是用得最多的函数之中的一个.但它特别讨厌的地方就是我们常常须要对返回的view进行类型转换,输入麻烦.代码丑陋,比如曾经我们在Activity中找一些子 ...
- 9517 Link Link Look
9517 Link Link Look 该题有题解 时间限制:2000MS 内存限制:65535K提交次数:67 通过次数:18 题型: 编程题 语言: G++;GCC Description ...
- 不使用系统自带的button
// // LKTitleBtn.m // 01-彩票 // // Created by Lenny on 3/17/15. // Copyright (c) 2015 Lenny. All ...
- Linux VGA驱动移植实验【转】
本文转载自:http://m.blog.csdn.net/bzw73/article/details/46564275 有了前面的LCD驱动的框架,再移植VGA驱动就相当的容易了.默认在光盘中已经支持 ...
- Oracle 11g RAC for LINUX rhel 6.X silent install(静默安装)
一.前期规划 1.硬件环境 CPU: Intel(R) Xeon(R) CPU E7-4820 v4 @ 2.00GHz 8*10核 内存:512GB OCR:2147*5 MB DATA1:2TB ...
- cookie,session,viewstate
viewstate的原理是隐藏域. protected void Page_Load(object sender, EventArgs e) { ViewState["v1"] = ...