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. Taro之百度地图显示定位点和信息

    由于没有后台数据就随机生成点来模拟了.具体代码如下. componentDidMount = () => { const { BMap, BMAP_STATUS_SUCCESS } = wind ...

  2. 接口测试(二) 优化项目分层及cookies值带入

    整个项目分层如图 然后上代码 #data_test.py from openpyxl import load_workbook import json import os class Date_tes ...

  3. EOS源码

    [EOS源码] 1.在 libraries/chain/include/eosio/chain/ 目录下. permission_level 定义如下:   account_name.permissi ...

  4. FPGA_VHDL 学习课堂笔记001

    FPGA_VHDL 学习课堂笔记   记录说明:本文档主要记录大学期间,老师FPGA授课课堂笔记. 代码语言:VHDL 编程软件:MAX+plus II FPGA硬件:FLE-843   03月05日 ...

  5. Golang学习---常用库

    1. 路由库:github.com/julienschmidt/httprouter 2. mysql驱动:github.com/go-sql-driver/mysql

  6. java利用反射动态加载方法

    @参考文章 根据特定字符串加载相应的方法,有人用if else,有人用switch.参数少了或情况少了还好,很多方法真要命,不要紧,java反射拯救你 import java.lang.reflect ...

  7. MyBatis-Plus学习笔记

    MybatisPlus的全局策略配置 注意:我们写的配置还要加到 MybatisSqlSessionFactoryBean 中 applicationContext.xml <!--定义Myba ...

  8. 探索未知种族之osg类生物---渲染遍历之Renderer::draw()简介

    我们今天进入上一节的遗留问题Renderer::draw()的探究. 1.从_drawQueue中取出其中一个sceneView对象.SceneView是对scene和view类的封装,通过他可以方便 ...

  9. 常用的三种json软件的使用

    从几个角度比较三种软件 1. json操作 2 反解 3 性能 易用性还没有列出. 可以根据个人喜好进行取舍. package json; import com.alibaba.fastjson.JS ...

  10. composer 镜像地址

    composer config -g repo.packagist composer https://packagist.composer-proxy.orgcomposer config -g re ...