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张卡片,每张 ...
随机推荐
- ipad忘记了锁屏密码,已经越狱了
ipad忘记了锁屏密码,已经越狱了, 已经需要连接itunes了...要是恢复的话,好像就不能越狱了耶... 我叫什么好咧 | 浏览 3330 次 问题暂时关闭 推荐于2016-07-23 11: ...
- unittest框架(惨不忍睹低配版)
根据我上个随笔的unittest框架优化得来,虽然对于smtp模块还是有点迷糊,不过还是勉强搭建运行成功了,还是先上代码: #login_test.py import requests class L ...
- JSONObject optString
JSONObject optString 获取后台返回的{"state":1001,"data":"数据保存成功"} 用optString ...
- Go语言编程读书笔记:Go channel(2)
单向channel 概念 单向channel是只能用于发送或者接收数据,channel本身必然是同时支持读写,否则根本没法用.假如一个channel只能读,那么肯定只会是空的,因为你没有机会向里面写数 ...
- 关于处理java.lang.IllegalArgumentException at java.sql.Date.getHours异常
https://segmentfault.com/a/1190000016937108
- Ubuntu开机时提示“piix4_smbus 0000:00:07.3: SMBus Host controller not enabled”
问题描述:Ubuntu开机时提示“piix4_smbus 0000:00:07.3: SMBus Host controller not enabled” 版本:Ubuntu 18.04 VMw ...
- python——获取文件列表
"""-------------------------------------------------------- <<获取文件列表>> () ...
- 关于fftshift----将零频率的分量移到频谱的中心
fftshift 作用:将零频点移到频谱的中间 用法: Y=fftshift(X) Y=fftshift(X,dim) 描述:fftshift移动零频点到频谱中间,重新排列fft,fft2和fftn的 ...
- python基础(17)继承类和面向对象初识
1.继承类 class Lm: money = 1000000 house = 5 def driver(self): print('会开车') class Mcb(Lm): def about_me ...
- yum 安装时遇到“UnicodeDecodeError: 'ascii' codec”的问题
今天新安装了一个6.9系统,配置好本地yum源后,用yum安装时报了以上的错误信息,在/etc/yum.repos.d/目录下多出了TTT的一个目录 (手动问号),在百度上查了一些文档. 解决方法:1 ...