题目描述

Some company is going to hold a fair in Byteland. There are n n n towns in Byteland and m m m two-way roads between towns. Of course, you can reach any town from any other town using roads.

There are k k k types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least s s s different types of goods. It costs d(u,v) d(u,v) d(u,v) coins to bring goods from town u u u to town v v v where d(u,v) d(u,v) d(u,v) is the length of the shortest path from u u u to v v v . Length of a path is the number of roads in this path.

The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of n n n towns.

输入输出格式

输入格式:

There are 4 4 4 integers n n n , m m m , k k k , s s s in the first line of input ( 1≤n≤105 1 \le n \le 10^{5} 1≤n≤105 , 0≤m≤105 0 \le m \le 10^{5} 0≤m≤105 , 1≤s≤k≤min(n,100) 1 \le s \le k \le min(n, 100) 1≤s≤k≤min(n,100)
) — the number of towns, the number of roads, the number of different
types of goods, the number of different types of goods necessary to hold
a fair.

In the next line there are n n n integers a1,a2,…,an a_1, a_2, \ldots, a_n a1​,a2​,…,an​ ( 1≤ai≤k 1 \le a_{i} \le k 1≤ai​≤k ), where ai a_i ai​ is the type of goods produced in the i i i -th town. It is guaranteed that all integers between 1 1 1 and k k k occur at least once among integers ai a_{i} ai​ .

In the next m m m lines roads are described. Each road is described by two integers u u u v v v ( 1≤u,v≤n 1 \le u, v \le n 1≤u,v≤n , u≠v u \ne v u≠v
) — the towns connected by this road. It is guaranteed that there is no
more than one road between every two towns. It is guaranteed that you
can go from any town to any other town via roads.

输出格式:

Print n n n numbers, the i i i -th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town i i i . Separate numbers with spaces.

输入输出样例

输入样例#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

题意:
n 个点,m 条边,有k种不同的物品,求从每个点出发收集s个不同物品的最短距离;
我们从每种物品 bfs;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n, m, k, s;
vector<int>v[maxn];
vector<int>c[maxn];
int dis[100004][200];
bool vis[100004][200];
void bfs(int x) {
queue<int>q; ms(vis);
for (int i = 0; i < v[x].size(); i++) {
q.push(v[x][i]); dis[v[x][i]][x] = 0;
}
while (!q.empty()) {
int u = q.front(); q.pop(); vis[u][x] = 0;
for (int i = 0; i < c[u].size(); i++) {
int to = c[u][i];
if (dis[to][x] > dis[u][x] + 1) {
dis[to][x] = dis[u][x] + 1;
if (!vis[to][x]) {
q.push(to); vis[to][x] = 1;
}
}
}
}
} int main() {
//ios::sync_with_stdio(0);
cin >> n >> m >> k >> s;
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= n; i++) {
int x; rdint(x);
v[x].push_back(i);
}
for (int i = 1; i <= m; i++) {
int u, V; rdint(u); rdint(V);
c[V].push_back(u); c[u].push_back(V);
}
for (int i = 1; i <= k; i++)bfs(i);
for (int i = 1; i <= n; i++) {
sort(dis[i] + 1, dis[i] + 1 + k);
int ans = 0;
for (int j = 1; j <= s; j++)ans += dis[i][j];
cout << ans << " ";
}
return 0;
}

CF986A Fair的更多相关文章

  1. CF986A Fair【图论/BFS】

    [题意]: 有些公司将在Byteland举办公平的会议.Byteland的n个城镇,m条两镇之间的双向道路.当然,你可以使用道路从任一个城镇到达任何城镇. 有k种商品产自Byteland,并且每个城镇 ...

  2. NOIP前刷题记录

    因为本蒻实在太蒻了...对于即将到来的NOIP2018ssfd,所以下决心要把自己近期做过的题目(衡量标准为洛谷蓝题难度或以上)整理一下,归归类,简单地写一下思路,就当作自己复习了吧qwq 本随笔持续 ...

  3. NOIP刷题

    搜索 [NOIP2013]华容道 最短路+带剪枝的搜索,是一个思维难度比较大的题目. CF1064D Labyrinth 考虑贪心,用双向队列bfs [NOIP2017]宝藏 剪枝搜索出奇迹 题解:h ...

  4. LA 3231 - Fair Share

    You are given N processors and M jobs to be processed. Two processors are specified to each job. To ...

  5. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. java线程 公平锁 ReentrantLock(boolean fair)

    一.公平锁 1.为什么有公平锁 CPU在调度线程的时候是在等待队列里随机挑选一个线程,由于这种随机性所以是无法保证线程先到先得的(synchronized控制的锁就是这种非公平锁).但这样就会产生饥饿 ...

  7. Fair Scheduler 队列设置经验总结

    Fair Scheduler 队列设置经验总结 由于公司的hadoop集群的计算资源不是很充足,需要开启yarn资源队列的资源抢占.在使用过程中,才明白资源抢占的一些特点.在这里总结一下. 只有一个队 ...

  8. Fair Scheduler中的Delay Schedule分析

    延迟调度的主要目的是提高数据本地性(data locality),减少数据在网络中的传输.对于那些输入数据不在本地的MapTask,调度器将会延迟调度他们,而把slot分配给那些具备本地性的MapTa ...

  9. Hadoop学习之--Fair Scheduler作业调度分析

    Fair Scheduler调度器同步心跳分配任务的过程简单来讲会经历以下环节: 1. 对map/reduce是否已经达到资源上限的循环判断 2. 对pool队列根据Fair算法排序 3.然后循环po ...

随机推荐

  1. leetcode 204. Count Primes(线性筛素数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. c ...

  2. linux命令学习笔记(9):touch 命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. .命令格式: touch [选项]... 文件... .命令参数: -a 或--t ...

  3. poj 1845 Sumdiv(约数和,乘法逆元)

    题目: 求AB的正约数之和. 输入: A,B(0<=A,B<=5*107) 输出: 一个整数,AB的正约数之和 mod 9901. 思路: 根据正整数唯一分解定理,若一个正整数表示为:A= ...

  4. OIer应该知道的二进制知识

    计算机使用\(2\)进制,这是众所周知的.在学习\(OI\)的过程中,\(2\)进制也显得尤为重要.有时候,细节决定成败,所以我想总结一下容易被遗忘和误解的关于\(2\)进制的知识. 1.运算符 &a ...

  5. CCS V5 使用教程三:程序调试

    官网教程 新建调试工程 输入以下源码: #include <stdio.h> #include <c6x.h> ]; void main(void) { unsigned ; ...

  6. spring初始化顺序

    首先,Spring bean的默认加载顺序是怎么控制的 工程中有2个bean,A和B,其中必须先初始化A再初始化B,但是没有depend-on或者Order等方式去保证,只不过恰好刚好这么运行着没出事 ...

  7. JavaScript之使用JavaScript模仿oop编程

    第一, 首先,使用关键字function定义一个类 function Shape1(ax,ay) {//此时将function看成声明类的标志 ; ; var init=function () {// ...

  8. <正则吃饺子> :关于使用pd创建表时需要注意的地方

    公司项目使用pd设计数据库表.之前用过,但是年代比较久远了,有些细节忘记了,今天重新使用时候,生疏了,现在稍微记录下吧. 1.pd创建表的使用,可以直接从网上搜索,博文比较多,如 “pd 设计数据库表 ...

  9. 韩顺平循序渐进学JAVA从入门到精通 视频全套,需要的联系我

    0讲-开山篇.avi 10讲-访问修饰符.重载.覆盖.avi 11讲-约瑟夫问题.avi 12讲-多态.avi 13讲-抽象类.接口.avi 14讲-final.作业评讲.avi 15讲-作业.测试题 ...

  10. xgene:肿瘤相关基因 KRAS,,BRAF,,通路PI3K-AKT

    KRAS基因 一个是KRAS1,位于chr6 短臂上,是一个“假基因”,它不能被转录成RNA,故没有功能的 另一个是KRAS2,位于chr12 短臂上..是“真基因”,是能够转录.并且翻译成蛋白的,是 ...