Codeforces Round #485 (Div. 2) D. Fair
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的更多相关文章
- Codeforces Round #485 (Div. 2)
Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #485 (Div. 2) E. Petr and Permutations
Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...
- Codeforces Round #485 (Div. 2) C. Three displays
Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...
- Codeforces Round #485 (Div. 2) A. Infinity Gauntlet
Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...
- 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 ...
- Codeforces Round #485 (Div. 2) C题求三元组(思维)
C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #485 Div. 1 vp记
A:对每种商品多源bfs一下每个点到该商品的最近距离,对每个点sort一下取前s个即可. #include<iostream> #include<cstdio> #includ ...
- Codeforces Round #436 (Div. 2)【A、B、C、D、E】
Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...
随机推荐
- Day08 - Ruby比一比:String的+=与concat串接
前情提要: 在第七天我们透过比较Symbol和String,发现字串比符号多了更多方法!为了活用string method,今天我们接续前文,来探讨一题跟字串有关的题目: Ruby经典面试题目#08( ...
- servlet的继承关系
一.servlet的继承关系 1.servlet程序是sun公司开发用于web资源技术,任何一个类只需要实现了servlet接口,那么就可以成为servlet程序 2.继承体系: ---------- ...
- Java框架spring Boot学习笔记(八):@PropertySource,@Value注解
获取配置文件 忽略配置文件不存在时报错
- jquery花式图片库——jqFancyTransitions
http://www.html580.com/3785 https://yq.aliyun.com/ziliao/4390 使用方法调用插件js文件: <script src="js/ ...
- AS3语法和UNITY C#语法的异同
AS3 UNITY Sprite a = new Sprite(); trace(a.paent); 此时a.parent为null,还未AddChild到屏幕上, 一般用这个来判断在不在屏幕上 ...
- php下kafka实践
Kafka是一种高吞吐的分布式发布订阅消息系统 kafka安装和简单测试 安装kafka 下载 wget https://www-us.apache.org/dist/kafka/2.1.1/kafk ...
- 2. 2A03简介
2A03简介 1.CPU 1.1 内部寄存器 1.累加寄存器A(Accumulator):8位寄存器,用于同算术逻辑单元(ALU)共同完成各种算术逻辑运算,它既为ALU提供原始操作数又担任存放ALU运 ...
- 移值UCOS2到M4核与M3核的区别
之前移值过ucos2到stm32f2系列的单片机,这个单片机是属于arm的m3内核的.最近在学习永磁同步电机的控制,对于这个电机的控制,有比较多的数学计算,甚至于还有浮点的运算.所以用到了stm32f ...
- springmvc webservlet 异步请求总结
1:每次请求会启动一个新线程 上边在debug状态下, 每次请求一次,生成一个新的 thread 在此已经是245了 出现一个现象在debug模式下, 每次请求生成的线程,自动在红框那个位置停了下来 ...
- 关于TCP窗口大小
窗口字段 TCP Window字段用于接收端通知发送端:接收端当前能够接收的字节数(即当前允许发送端发送的字节数).在TCP Header中占有16bit长度,如下所示 0 1 2 3 0 1 2 3 ...