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张卡片,每张 ...
随机推荐
- Android studio使用android:style/Theme.Dialog报错:You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
查找原因是在activity java代码部分继承了compatactivity public class DialogActivity extends AppCompatActivity 但是在An ...
- windows 下用命令来操作定时任务
cmd下定时执行命令可以使用 at 命令 或者 schtasks命令.at 语法:at +时间+运行程序schtasks语法:schtasks /creat /tn 设定定时运行的名字 /tr “运行 ...
- Unix/Linux系统的发展史
Unix/Linux系统相信是学编程的人都认识这两个系统.我们知道Unix要钱,而Linux免费,而且这两者之间的发展史是什么样的,是不是两者就是同一个东西呢? 我将会以时间的发展过程来一步步的给大家 ...
- ceph mimc版本ceph-deploy安装与配置
系统环境centos7.6 内核5.0.9 YUM源配置 [root@k8s-sys-10-82-4-200 ceph-cluster]# cat /etc/yum.repos.d/ceph.repo ...
- poj2828(线段树查找序列第k小的值)
题目链接:https://vjudge.net/problem/POJ-2828 题意:有n个人,依次给出这n个人进入队列时前面有多少人p[i],和它的权值v[i],求最终队列的权值序列. 思路:基本 ...
- 关于socket
使用socket常用的操作就是读写,recv和send,与read.write对应,但多了一个flag位可以设定阻塞等,一些细节以后再探.目前使用时发现read往往不能将数据完整读出,可能是调用时内核 ...
- WEB-INF目录下的jsp怎么引用外部文件:js,css等
在项目中,为了安全.我们通常会将jsp文件放在WEB-INF下面,对于放在WEB-INF下面的js或是css等资源文件.我们通常可以通过相对路径来引用,而如果是放在WEB-INF之外的js 或是 cs ...
- vue的表单输入绑定
1.单行绑定 <p>{{logintype}}</p> <input type="text" v-model="logintype" ...
- redis哨兵集群
Sentinel 哨兵 修改src下的sentinel.conf文件 , 配置端口 :port:随便 daemonize yes 配置主服务器的ip 和端口 我们把监听的端口修改成7000,并且 ...
- 52-2018 蓝桥杯省赛 B 组模拟赛(一)java
最近蒜头君喜欢上了U型数字,所谓U型数字,就是这个数字的每一位先严格单调递减,后严格单调递增.比如 212212 就是一个U型数字,但是 333333, 9898, 567567, 313133131 ...