CodeForces 987D Fair BFS

本文介绍了一个涉及图论和最短路径算法的问题,即如何在不同的城镇间选择最优路径以运输货物,确保至少包含指定种类的商品,并使总花费最小。通过进行多次广度优先搜索(BFS),针对每种商品计算所有城镇到生产该商品城镇的距离。

A. 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.


这道题对我这个大菜鸡还是很挑战智商的,竟然1A了,使我非常震惊。。。总之,做了k次bfs,每次让k类型的点全部入队,然后计算每一个点到这个类型的距离,之后再把这个距离加入这个点的vector里。做完bfs对每个点的bfs做个排序,取最小的s个数加起来就是结果。


#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

int n,m,k,s,head[100005],dist[100005],t[100005];
vector<int> type[105];
vector<int> vec[100005];
bool vis[100005];

struct Edge{
    int to,next;
}edge[200010];
int e=0;
void addedge(int a,int b)
{
    edge[e].to=b;
    edge[e].next=head[a];
    head[a]=e++;
}

struct pos{
    int p;
    int dis;
};

void bfs(int i)
{
    queue<pos> q;
    for (int j=0; j<type[i].size(); j++)
    {
        pos u; u.p=type[i][j]; u.dis=0;
        q.push(u); vis[u.p]=true; dist[u.p]=0;
    }
    while (!q.empty())
    {
        pos u=q.front(); q.pop();
        //cout<<"u="<<u.p<<" t="<<u.dis<<endl;
        for (int j=head[u.p]; j!=-1; j=edge[j].next)
        {
            if (vis[edge[j].to]) continue;
            pos v; v.p=edge[j].to; v.dis=u.dis+1;
            dist[v.p]=v.dis; vis[v.p]=true;
            q.push(v);
        }
    }
}

int main()
{
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for (int i=1; i<=n; i++)
    {
        scanf("%d",&t[i]);
    }
    memset(head,-1,sizeof(head));
    for (int i=1; i<=m; i++)
    {
        int a,b; scanf("%d%d",&a,&b);
        addedge(a,b);
        addedge(b,a);
    }
    for (int i=1; i<=n; i++)
    {
        type[t[i]].push_back(i);
    }
    for (int i=1; i<=k; i++)
    {
        memset(vis,false,sizeof(vis));
        memset(dist,0x3f,sizeof(dist));
        bfs(i);
        for (int i=1; i<=n; i++)
        {
            vec[i].push_back(dist[i]);
        }
    }
    for (int i=1; i<=n; i++)
    {
        sort(vec[i].begin(),vec[i].end());
        long long sum=0;
        for (int j=0; j<s; j++)
        {
            sum+=vec[i][j];
        }
        cout<<sum<<' ';
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值