Codeforces D. Fair 多源BFS求最短路
D. Fairtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output
Some company is going to hold a fair in Byteland. There are?n
?towns in Byteland and? m?two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are?k
?types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least? sdifferent types of goods. It costs? d(u,v)?coins to bring goods from town? u?to town? v?where? d(u,v)?is the length of the shortest path from? uto? v. 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
?towns.
InputThere are?4
?integers? n,? m,? k,? s?in the first line of input ( 1≤n≤105,? 0≤m≤105,? 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
?integers? a1,a2,…,an?( 1≤ai≤k), where? ai?is the type of goods produced in the? i-th town. It is guaranteed that all integers between? 1?and? k?occur at least once among integers? ai.
In the next?m
?lines roads are described. Each road is described by two integers? u? v?( 1≤u,v≤n,? u≠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.
OutputPrint?n
?numbers, the? i-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town? i. Separate numbers with spaces.
ExamplesinputCopy5 5 4 3 1 2 4 3 2 1 2 2 3 3 4 4 1 4 5 outputCopy2 2 2 2 3 inputCopy7 6 3 2 1 2 3 3 2 2 1 1 2 2 3 3 4 2 5 5 6 6 7 outputCopy1 1 1 2 2 1 1 NoteLet's look at the first sample.
To hold a fair in town?1
?you can bring goods from towns? 1?( 0?coins),? 2?( 1?coin) and? 4?( 1?coin). Total numbers of coins is? 2.
Town?2
: Goods from towns? 2?( 0),? 1?( 1),? 3?( 1). Sum equals? 2.
Town?3
: Goods from towns? 3?( 0),? 2?( 1),? 4?( 1). Sum equals? 2.
Town?4
: Goods from towns? 4?( 0),? 1?( 1),? 5?( 1). Sum equals? 2.
Town?5
: Goods from towns?5?(0),?4?(1),?3?(2). Sum equals?3.
題意:
有n個城市,m條路,保證任意城市都相通,保證任意兩個城市之間都只有1條路徑。現在,要在某一個城市舉辦一場盛會,每個城市都會生產1種商品(不同城市之間生產的商品可能相同)共有k種不同的商品,現在,舉辦盛會需要s種不同的商品。每種商品都需要走到相應的城市去取。分別輸出在n個城市舉辦盛會需要走的路(路徑以單位路徑來算例如,如果1----2----3,那么,1到3要走的路為2)
思路:
特產最多100種,可以計算每種特產到每個城鎮的最短路,mov【i】【j】計算出所有j特產到所有i的最短距離,
這樣的話是1e7,還可以勉強接受。
然后把所有到i城鎮的特產的最短路 排序,取前s個就是i點的最短路徑了。
直接將所有生產i型商品的城市一次性全加入隊列,然后bfs#include<iostream> #include<cstdio> #include<vector> #include<queue> #include "algorithm" using namespace std; const int MAX=1e6+5; int n,m,k,s; int good[MAX],mov[MAX][105]; //mov[i][j]記錄以生產j商品的城市為舉辦地,到i城市的最小花費 vector<int> E[MAX]; void bfs(int x) {queue<int> Q;int i,u,v;Q.push(x+n);while(!Q.empty()){u=Q.front();Q.pop();for(i=0;i<E[u].size();i++){v=E[u][i];if(mov[v][x]==0){mov[v][x]=mov[u][x]+1;Q.push(v);}}} } int main() {int i,j;int u,v,cnt;scanf("%d%d%d%d",&n,&m,&k,&s);for(i=1;i<=n;i++){scanf("%d",&good[i]);E[good[i]+n].push_back(i);//如果我們直接用一個for循環將生產good[i]型商品的城市加入隊列的話,就會超時,所以,我們借用這個vector來處理!}for(i=0;i<m;i++){scanf("%d%d",&u,&v);E[u].push_back(v);E[v].push_back(u);}for(i=1;i<=k;i++) bfs(i);for(i=1;i<=n;i++){cnt=0;sort(mov[i]+1,mov[i]+k+1);for(j=1;j<=s;j++)cnt+=mov[i][j]-1;printf("%d ",cnt);}return 0; }
總結
以上是生活随笔為你收集整理的Codeforces D. Fair 多源BFS求最短路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计蒜客 Reversion Count
- 下一篇: codeforces B. High S