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;
    }
     
  • 相关阅读:
    图像特征工程
    神经网络在多分类上的应用——数据预处理
    Robotics Lab3 ——图像特征匹配、跟踪与相机运动估计
    Robotics Lab2——相机模型,点云图拼接与深度测量
    Robotics Lab1 —— 基于颜色特征的目标识别与追踪实验
    【Ubuntu16.04】解决Qt安装包(.run文件)不能用./命令执行的问题
    【ROS系统】创建消息(msg)后使用rosmsg命令报错的解决办法
    【ROS系统】执行roslaunch命令启动launch文件提示Invalid roslaunch XML syntax错误的解决办法
    【ROS系统】解决找不到用户工作空间下的程序包的问题——E:No such package
    【UEFI+GPT/BIOS+MBR】两种模式在Windows系统下安装Ubuntu系统
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/9110231.html
Copyright © 2011-2022 走看看