zoukankan      html  css  js  c++  java
  • 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 << " ";
        }
    }
    
    
  • 相关阅读:
    go语言之goroute协程
    Vue中Computed和Watch的用法及区别
    php判断复选框是否被选中的方法
    基于workerman的实时推送
    织梦引入公共模板
    织梦快速建站首页模板
    golang解决中文乱码的方法
    Vue项目中使用可视化图表echarts
    解决for循环中异步请求顺序不一致的问题
    layui多图上传实现删除功能的方法
  • 原文地址:https://www.cnblogs.com/EDGsheryl/p/9162079.html
Copyright © 2011-2022 走看看