zoukankan      html  css  js  c++  java
  • Fair

    Some company is going to hold a fair in Byteland. There are nn towns in Byteland and mm two-way roads between towns. Of course, you can reach any town from any other town using roads.

    There are kk types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least ss different types of goods. It costs d(u,v)d(u,v) coins to bring goods from town uu to town vv where d(u,v)d(u,v) is the length of the shortest path from uu to vv . 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 nn towns.

    Input

    There are 44 integers nn , mm , kk , ss in the first line of input (1n1051≤n≤105 , 0m1050≤m≤105 , 1skmin(n,100)1≤s≤k≤min(n,100) ) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.

    In the next line there are nn integers a1,a2,,ana1,a2,…,an (1aik1≤ai≤k ), where aiai is the type of goods produced in the ii -th town. It is guaranteed that all integers between 11 and kk occur at least once among integers aiai .

    In the next mm lines roads are described. Each road is described by two integers uu vv (1u,vn1≤u,v≤n , uvu≠v ) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.

    Output

    Print nn numbers, the ii -th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town ii . Separate numbers with spaces.

    Examples
    Input
    Copy
    5 5 4 3
    1 2 4 3 2
    1 2
    2 3
    3 4
    4 1
    4 5
    Output
    Copy
    2 2 2 2 3 
    题解:商品的个数最多只有100,所以枚举商品。用BFS构造最短路,注意的是起始距离变成了1。
    #pragma warning(disable:4996)
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define mem(arr, in) memset(arr, in, sizeof(arr))
    using namespace std;
    
    const int maxn = 100205;
    const int INF = 1e9 + 7;
    
    int n, m, k, s;
    int use[maxn], d[maxn][105], co[maxn];
    
    vector<int> v[maxn];
    
    void minroad(int st) {
        queue<int> q;
        q.push(st + n);
        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (int i = 0; i < v[u].size(); i++) {
                int to = v[u][i];
                if (!d[to][st]) {
                    d[to][st] = d[u][st] + 1;
                    q.push(to);
                }
            }
        }
    }
    
    int main()
    {
        while (scanf("%d %d %d %d", &n, &m, &k, &s) != EOF) {
            for (int i = 1; i <= n; i++) scanf("%d", co + i), v[co[i] + n].push_back(i);
            for (int i = 1; i <= m; i++) {
                int a, b;
                scanf("%d%d", &a, &b);
                v[a].push_back(b);
                v[b].push_back(a);
            }
            for (int i = 1; i <= k; i++) minroad(i);
            for (int i = 1; i <= n; i++) {
                sort(d[i] + 1, d[i] + k + 1);
                int ans = 0;
                for (int j = 1; j <= s; j++) ans += d[i][j];
                printf("%d ", ans - s);
            }
            printf("
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    leetcode 264: Ugly Number II
    leetcode 260: Single Number III
    leetcode 241: Different Ways to Add Parentheses
    清远市技术学院大学城网
    英德市职业技术学校大学城网
    清远市田家炳中学大学城网
    清远市清城区清城中学大学城网
    清远市第一中学大学城网
    当前最热的技术
    Python 学习视频
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/9110231.html
Copyright © 2011-2022 走看看