寒假特訓——I - 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
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
Input
There are 44 integers nn , mm , kk , ss in the first line of input (1≤n≤1051≤n≤105 , 0≤m≤1050≤m≤105 , 1≤s≤k≤min(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
In the next mm lines roads are described. Each road is described by two integers uu vv (1≤u,v≤n1≤u,v≤n , u≠vu≠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
Input5 5 4 3Output
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
2 2 2 2 3Input
7 6 3 2Output
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
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 .
Fair
思路:
大體思路就是先儲存路線和生產地,再就是用bfs找到每一個產品生產地到其他城市最短距離,最後sort進行排序,
求出前s個得和即可。 bfs
第一次進入while,目的是找到生產x產品所有的生產地,第二次,是對每一個生產地進行往後延生,就是找到與這個地方相鄰得
地方,距離再加1. 這個最短路bfs的思路要好好整理,以後還可以用到的 就是用bfs,對它附近的城市距離加+1,這個vector的處理要註意學習!!!!!
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> #include <algorithm> #include <queue> using namespace std; const int maxn=1e5+1000; vector<int>exa[maxn]; int mov[maxn][110],n; void bfs(int x) { queue<int>que; int i,u,v; que.push(x+n); while(!que.empty()) { u=que.front();que.pop(); for(i=0;i<exa[u].size();i++) { v=exa[u][i]; if(mov[v][x]==0) { mov[v][x]=mov[u][x]+1; que.push(v); } } } } int main() { int m,k,s,a,u,v; scanf("%d %d %d %d",&n,&m,&k,&s); for(int i=1;i<=n;i++) { scanf("%d",&a); exa[a+n].push_back(i); } for(int i=1;i<=m;i++) { scanf("%d%d",&u,&v); exa[u].push_back(v); exa[v].push_back(u); } for(int i=1;i<=k;i++) bfs(i); for(int i=1;i<=n;i++) sort(mov[i]+1,mov[i]+k+1); for(int i=1;i<=n;i++) { int cnt=0; for(int j=1;j<=s;j++) cnt+=mov[i][j]-1; i==n?printf("%d\n",cnt):printf("%d ",cnt); } return 0; }
寒假特訓——I - Fair