1. 程式人生 > >987D (bfs,設定超級源點,對每個物品進行暴力求解)

987D (bfs,設定超級源點,對每個物品進行暴力求解)

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 s

s 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 n

ntowns.

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 n

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

ExamplesInput
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
Output
2 2 2 2 3 
Input
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
Output
1 1 1 2 2 1 1 

題意:有n個城市,m條路,保證任意城市都相通,保證任意兩個城市之間都只有1條路徑。現在,要在某一個城市舉辦一場盛會,每個城市都會生產1種商品(不同城市之間生產的商品可能相同)共有k種不同的商品,現在,舉辦盛會需要s種不同的商品。每種商品都需要走到相應的城市去取。分別輸出在n個城市舉辦盛會需要走的路(路徑以單位路徑來算例如,如果1----2----3,那麼,1到3要走的路為2)

思路:剛看到這道題時,總不能對每個節點進行bfs一次吧,找到s個物品;看看n個範圍,時間複雜度會是n*n,當你觀察k的範圍為(1~100),看看能不能讓物品去找節點,看看到每個節點的花費,對每個物品進行暴力,最後取 到這個節點花費小的s個物品;這中間就應該設定超級源點了;

程式碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 100010
#include<vector>
#include<queue>
int a[Max];
int n,m,k,s;

vector<int> v[110];       // 超級源點i,v[i]中存當前生產的物品為i的所有結點; 
vector<int> G[Max];       // 存圖; 
vector<int> d[Max];      // d[i]中存的是 每一個物品到結點i的花費,最後取前k個1; 
int book[Max];

struct node
{
	int x;
	int step;
};

void init()
{
	int i,j;
	for(i = 0;i<=100;i++)
		v[i].clear();
	for(i = 0;i<=n;i++)
	{
		G[i].clear();
		d[i].clear();
	}
		
}
void bfs(int s)
{
	int i,j;
	memset(book,0,sizeof(book));
	queue<node >q;
	for(i = 0;i<v[s].size();i++)
	{
		int k = v[s][i];
		book[k] = 1;
		node star;
		star.x = k;
		star.step = 0;
		d[star.x].push_back(0);
		q.push(star);
	}
	while(!q.empty())
	{
		node star = q.front();
		q.pop();
		int k = star.x;
		for(i = 0;i<G[k].size();i++)
		{
			int tt = G[k][i];
			if(!book[tt])
			{
				book[tt] = 1;
				d[tt].push_back(star.step + 1);
				q.push(node{tt,star.step+1});
			}
		}
	}
}

int main()
{
	int i,j;
	while(~scanf("%d%d%d%d",&n,&m,&k,&s))
	{
		init();
		for(i = 1;i <= n; i ++)
		{
			scanf("%d",&a[i]);
			v[a[i]].push_back(i);
		}
		int x,y;
		for(i = 1;i <= m;i ++)
		{
			scanf("%d%d",&x,&y);
			G[x].push_back(y);
			G[y].push_back(x);
		}
		for(i = 1;i<=k;i++)
			bfs(i);
		for(i = 1;i<=n;i++)
		{
			sort(d[i].begin(),d[i].begin()+k);
			int sum = 0;
			for(j = 0;j<d[i].size();j++)
			{
				if(j==s) break;
				sum += d[i][j];
			}
			if(i!=1) printf(" "); 
			printf("%d",sum);
		}
		printf("\n");
	}
	return 0;
}