• D. Fair


    D. Fair
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    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 ssdifferent 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 uuto 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 nnmmkkss in the first line of input (1n1051≤n≤1050m1050≤m≤1051skmin(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≤nuvu≠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 
    
    input
    Copy
    7 6 3 2
    1 2 3 3 2 2 1
    1 2
    2 3
    3 4
    2 5
    5 6
    6 7
    
    output
    Copy
    1 1 1 2 2 1 1 
    
    Note

    Let's look at the first sample.

    To hold a fair in town 11 you can bring goods from towns 11 (00 coins), 22 (11 coin) and 44 (11 coin). Total numbers of coins is 22.

    Town 22: Goods from towns 22 (00), 11 (11), 33 (11). Sum equals 22.

    Town 33: Goods from towns 33 (00), 22 (11), 44 (11). Sum equals 22.

    Town 44: Goods from towns 44 (00), 11 (11), 55 (11). Sum equals 22.

    Town 55: Goods from towns 55 (00), 44 (11), 33 (22). Sum equals 33.

    题意:

     有n个城市,m条道路,k种东西,使所有城市都公平需要使每个城市都有s种东西,每个城市可以生产一种东西,第i个城市能生产的东西的编号为ai,然后给你这个图,问你让每个城市都拥有s种不同的物品的最小花费是多少。

    思路

      先对于每种物品处理一下每个城市得到它需要的最小花费,dist[i,j]dist[i,j]代表第jj个城市得到第ii种物品的最小花费,然后对于每个城市排序一下对前s个求和就可以了。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10,inf=0x3f3f3f3f3f;
    int n,m,s,k,ans[maxn];
    vector<int> edge[maxn],color[maxn];
    int dis[107][maxn];
    void bfs(vector<int>color,int *dis){
        fill(dis+1,dis+1+n,-1);
        queue<int>q;
        for (auto u:color){
            dis[u]=0;
            q.push(u);
        }
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for (int v:edge[u])
                if(dis[v]==-1) dis[v]=dis[u]+1,q.push(v);
        }
    }
    int main(){
        scanf("%d%d%d%d",&n,&m,&k,&s);
        for (int i=1,tmp;i<=n;i++) scanf("%d",&tmp),color[tmp].push_back(i);
        for (int i=1,u,v;i<=m;i++){
            scanf("%d%d",&u,&v);
            edge[u].push_back(v);
            edge[v].push_back(u);
        }
        for (int i=1;i<=k;i++) bfs(color[i],dis[i]);
        for (int i=1;i<=n;i++){
            for (int j=1;j<=k;j++)
                ans[j]=dis[j][i];
            sort(ans+1,ans+1+k);
            printf("%d",accumulate(ans+1,ans+1+s,0));
            printf("%c",i==n?'
    ':' ');
        }
        return 0;
    }
    


  • 相关阅读:
    深度学习和神经网络的区别是什么
    各种数据类型范围
    排队接水
    最大整数
    马拉松接力赛
    合并果子
    统计学生信息(使用链表完成)
    删除数组中的元素(链表)
    求最大公约数(最小公倍数)
    十进制转化成八进制(一到十六进制)
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490327.html
Copyright © 2020-2023  润新知