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

题目连接:

http://codeforces.com/contest/987/problem/D

Description

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

There are $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$ different types of goods. It costs $d(u,v)$ coins to bring goods from town $u$ to town $v$ where $d(u,v)$ is the length of the shortest path from $u$ to $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$ towns.

Sample Input

7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7

Sample Output

1 1 1 2 2 1 1

题意

有n个点,每个点有一种特产,有m条路,将k种物品移动到每个点最小消耗是多少。

There are N vertex, each vertex has one type goods. There are m roads. Print the mininum spend of each vertex.

题解:

因为货物种类很少,对货物做bfs,用优先队列记录每个货物到该点最短距离。

Because there is few type of goods.We use bfs to calculate the mininum distence to every vertex. In each vertex, we use priority queue to maintain the answer.

代码

#include <bits/stdc++.h>

using namespace std;

int n, m;
int s, k;
int x, y;
queue<int> q[110];
priority_queue<int, vector<int>, greater<int> > a[100010];
bool vis[100010];
vector<int> g[100010];
queue<pair<int, int> > p; int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr); cin >> n >> m >> s >> k;
for (int i = 1; i <= n; i++) {
cin >> x;
q[x].push(i);
}
for (int i = 1; i <= m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= s; i++) {
fill(vis, vis + 100010, 0);
while (!q[i].empty()) {
p.push(make_pair(q[i].front(), 0));
vis[q[i].front()] = 1;
q[i].pop();
}
while (!p.empty()) {
auto x = p.front();
p.pop();
a[x.first].push(x.second);
for (auto o:g[x.first]) {
if (!vis[o]) {
p.push(make_pair(o, x.second + 1));
vis[o] = 1;
}
}
}
}
for (int i = 1; i <= n; i++) {
int ans = 0;
for (int o = 1; o <= k; o++) {
ans += a[i].top();
a[i].pop();
}
cout << ans << " ";
}
}

Codeforces Round #485 (Div. 2) D. Fair的更多相关文章

  1. Codeforces Round #485 (Div. 2)

    Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...

  2. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  3. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  4. Codeforces Round #485 (Div. 2) C. Three displays

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

  5. Codeforces Round #485 (Div. 2) A. Infinity Gauntlet

    Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...

  6. Codeforces Round #485 (Div. 2)-B-High School: Become Human

    B. High School: Become Human time limit per test 1 second memory limit per test 256 megabytes input ...

  7. Codeforces Round #485 (Div. 2) C题求三元组(思维)

    C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. Codeforces Round #485 Div. 1 vp记

    A:对每种商品多源bfs一下每个点到该商品的最近距离,对每个点sort一下取前s个即可. #include<iostream> #include<cstdio> #includ ...

  9. Codeforces Round #436 (Div. 2)【A、B、C、D、E】

    Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...

随机推荐

  1. Grafana报警--通知渠道配置

    最近研究了prometheus+grafana的系统监控,使用grafana的报警功能,grafana支持很多种通知渠道,下文记录使用到的几种notification channels,分别是emai ...

  2. 3G设置linux路由-iptables配置

    1.如何区分iptables的PREROUTING和POSTROUTING链 (引自http://jingyan.baidu.com/article/aa6a2c143d84470d4c19c4cf. ...

  3. CAS 无锁队列

    队列是常用的数据结构,采用的FIFO(first in firstout)原则,新元素(等待进入队列的元素)总是被插入到尾部,而读取的时候总是从头部开始读取.在计算中队列一般用来做排队(如线程池的等待 ...

  4. failed to find global analyzer [uax_url_email]

    ES的默认分词设置是standard,这个在中文分词时就比较尴尬了,会单字拆分,比如我搜索关键词“清华大学”,这时候会按“清”,“华”,“大”,“学”去分词,然后搜出来的都是些“清清的河水”,“中华儿 ...

  5. [Solution] 985. Sum of Even Numbers After Queries

    Difficulty: Easy Question We have an array A of integers, and an array queries of queries. For the i ...

  6. C++ map 使用erase在windows下崩溃,在linux下没有问题的原因

    注意:此程序在win环境下会出现上述描述的问题:在mac环境下第一种方式是正常运行的.Map.erase有3个重载函数: void erase(iterator position); size_typ ...

  7. scapy基础之一 ----简单命令

    前言 scapy是python写的一个功能强大的交互式数据包处理程序,可用来发送.嗅探.解析和伪造网络数据包,常常被用到网络攻击和测试中.下面介绍简单命令. ls() List all availab ...

  8. paloalto防火墙接口使用方法及实例

    1.获取账号相关的唯一API KEY windows:https://x.x.x.x/api/?type=keygen&user=username&password=password ...

  9. sort排序在苹果与安卓端不一致问题

    一.问题 在使用sort排序时,若遇到相同数据或非数值数据时,会出现苹果手机与安卓手机排序不一致问题 var arr = [{ "id": "52", &quo ...

  10. vue element-ui 设置时间组件

    备注:设置开始时间小于结束时间 <!-- 开始时间 --> <div class="block"> <!-- <span class=" ...