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 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 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 uvv (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 
    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.

    bfs,直接搜就好了。

    #include <bits/stdc++.h>
    #define maxn 200005
    using namespace std;
    struct edge
    {
        int u=0,v=0,next=0;
    }edge[maxn];
    int head[maxn];
    int a[maxn];
    int cnt=0;
    void addedge(int u,int v)
    {
        edge[cnt].u=u;
        edge[cnt].v=v;
        edge[cnt].next=head[u];
        head[u]=cnt++;
    }
    bool goods[105];
    bool vis[maxn];
    struct data
    {
        int x=0,step=0;
    };
    int bfs(int s,int sum)
    {
        memset(goods,false,sizeof(goods));
        memset(vis,false,sizeof(vis));
        queue<data> q;
        data now,nex;
        int val=0,i;
        now.x=s;
        goods[a[now.x]]=true;
        int all=1;
        q.push(now);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(all==sum) return val;
            for(i=head[now.x];i!=-1;i=edge[i].next)
            {
                nex.x=edge[i].v;
                nex.step=now.step+1;
                if(!goods[a[nex.x]])
                {
                    all++;
                    val+=nex.step;
                    if(all==sum) return val;
                    q.push(nex);
                    goods[a[nex.x]]=true;
                    vis[nex.x]=true;
                }
            }
            for(i=head[now.x];i!=-1;i=edge[i].next)
            {
                nex.x=edge[i].v;
                if(!vis[nex.x])
                {   //val+=nex.step;
                    q.push(nex);
                    vis[nex.x]=true;
                }
            }
        }
    }
    int main()
    {
        int n,m,k,s,i;
        memset(head,-1,sizeof(head));
        cin>>n>>m>>k>>s;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int u,v;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        for(i=1;i<=n;i++)
        {   if(i!=1) printf(" ");
            printf("%d",bfs(i,s));
        }
        printf("
    ");
        return 0;
    }
    

      

  • 相关阅读:
    八数码问题及其扩展
    java注释
    康托展开和逆康托展开
    模线性方程组
    欧拉函数
    扩展欧几里德
    商城项目实战 | 2.2 Android 仿京东商城——自定义 Toolbar (二)
    商城项目实战 | 2.1 Android 仿京东商城——自定义 Toolbar (一)
    商城项目实战 | 1.1 Android 仿京东商城底部布局的选择效果 —— Selector 选择器的实现
    Android使用Path实现仿最新淘宝轮播广告底部弧形有锯齿的问题以及解决办法
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9147370.html
Copyright © 2011-2022 走看看