Sheryl's ACM

一条默默划水的咸鱼

导航

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

posted on 2018-06-10 02:41  EDGsheryl  阅读(176)  评论(0编辑  收藏  举报