D. Fair(多源bfs)

40 篇文章 0 订阅
26 篇文章 0 订阅

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 

题目大概:

给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产)。有k种不同特产。

要求每个城镇需要其他城镇运输特产到自己的城镇,每个城镇必须拥有s种特产,那么在城镇满足s种特产后,需要的最短路径是多长,最短路指的是特产运输过来走过的边的数量。

思路:

一读完题,最暴力的思想是,对每个点进行一次bfs,算出每个点的最短路,但是数据是1e5,n2,就是1e10了。明显超时。

那么发现特产最多100种,可以计算每种特产到每个城镇的最短路,lis【i】【j】计算出所有j特产到所有i的最短距离,这样的话是1e7,还可以勉强接受。

然后把所有到i城镇的特产的最短路 排序,取前s个就是i点的最短路径了。

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
int e[maxn];
int vis[maxn];
int lis[maxn][120];
vector<int>F[120];
vector<int>G[maxn];
struct poin
{
    int x,d;
};
int ans=0;
int s;
void bfs(int x)
{
    queue<poin>Q;
    for(int i=0;i<F[x].size();i++)
    {
        int v=F[x][i];
        poin q;
        q.x=v;q.d=0;
        Q.push(q);
    }
    while(!Q.empty())
    {
        poin u=Q.front();Q.pop();
        lis[u.x][x]=min(u.d,lis[u.x][x]);
        for(int i=0;i<G[u.x].size();i++)
        {
            int v=G[u.x][i];
            if(vis[v])continue;
            vis[v]=1;
            poin q;
            q.x=v;
            q.d=u.d+1;
            Q.push(q);
        }
    }
}
int main()
{
    int n,m,k;
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&e[i]);
        F[e[i]].push_back(i);
    }
    int u,v;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    memset(lis,0x3f3f3f3f,sizeof(lis));
    for(int i=1;i<=k;i++)
    {
        memset(vis,0,sizeof(vis));
        bfs(i);
    }

    for(int i=1;i<=n;i++)
    {
        sort(lis[i]+1,lis[i]+k+1);
        long long sum=0;
        for(int j=1;j<=s;j++)
        {
            sum+=lis[i][j];
        }
        printf("%I64d ",sum);
    }

    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值