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 << " ";
    }
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9162079.html

时间: 2024-11-09 08:24:49

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

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

Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/problem/E Description Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to gene

【思维】Codeforces Round #485 (Div. 2) B. High School: Become Human(对数)

题目链接:http://codeforces.com/contest/987/problem/B 在运算的时候取对数就好了 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define ll long long 6 #define eps 1e-6 7 8 int main() 9 { 10 ll x,y; 11 double xx; 12 scanf("%lld %lld",&x,&y);

Codeforces Round #485 (Div. 2)

A. Infinity Gauntlet time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power G

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

C. Three displays It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem. There are nn displays placed along a road, and the ii-th

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 standard input output standard output Year 2118. Androids are in mass production for decades now, and they do all the work for humans. But androids ha

Codeforces Round #485 Div. 1 vp记

A:对每种商品多源bfs一下每个点到该商品的最近距离,对每个点sort一下取前s个即可. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define int long long #d

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

Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张卡片上都写有一个数,两个人每人选一个数,每人可以拿的卡片必须写有是自己选的数,问能否选择两个数使得两个人每人拿的卡片数一样多并且能拿光卡片.[就是看输入是不是只有两种数字] //:第一遍我看成字符串包含有选的数字也能拿,,这样写着居然过了..水题水题.. 1 #include<cstdio> 2

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿