日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CF986A Fair

發(fā)布時(shí)間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CF986A Fair 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

Some company is going to hold a fair in Byteland. There are n n n towns in Byteland and m m m two-way roads between towns. Of course, you can reach any town from any other town using roads.

There are k k k 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 s different types of goods. It costs d(u,v) d(u,v) d(u,v) coins to bring goods from town u u u to town v v v where d(u,v) d(u,v) d(u,v) is the length of the shortest path from u u u to v v 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 n n towns.

輸入輸出格式

輸入格式:

There are 4 4 4 integers n n n , m m m , k k k , s s s in the first line of input ( 1≤n≤105 1 \le n \le 10^{5} 1n105 , 0≤m≤105 0 \le m \le 10^{5} 0m105 , 1≤s≤k≤min(n,100) 1 \le s \le k \le min(n, 100) 1skmin(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 n integers a1,a2,…,an a_1, a_2, \ldots, a_n a1?,a2?,,an? ( 1≤ai≤k 1 \le a_{i} \le k 1ai?k ), where ai a_i ai? is the type of goods produced in the i i i -th town. It is guaranteed that all integers between 1 1 1 and k k k occur at least once among integers ai a_{i} ai? .

In the next m m m lines roads are described. Each road is described by two integers u u u v v v ( 1≤u,v≤n 1 \le u, v \le n 1u,vn , u≠v u \ne v uv ) — 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.

輸出格式:

Print n n n numbers, the i i i -th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town i i i . Separate numbers with spaces.

輸入輸出樣例

輸入樣例#1: 復(fù)制 5 5 4 3 1 2 4 3 2 1 2 2 3 3 4 4 1 4 5 輸出樣例#1: 復(fù)制 2 2 2 2 3 輸入樣例#2: 復(fù)制 7 6 3 2 1 2 3 3 2 2 1 1 2 2 3 3 4 2 5 5 6 6 7 輸出樣例#2: 復(fù)制 1 1 1 2 2 1 1
題意:
n 個(gè)點(diǎn),m 條邊,有k種不同的物品,求從每個(gè)點(diǎn)出發(fā)收集s個(gè)不同物品的最短距離;
我們從每種物品 bfs;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 200005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-4 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline ll rd() {ll x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x; }ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; }/*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans; } */int n, m, k, s; vector<int>v[maxn]; vector<int>c[maxn]; int dis[100004][200]; bool vis[100004][200]; void bfs(int x) {queue<int>q;ms(vis);for (int i = 0; i < v[x].size(); i++) {q.push(v[x][i]); dis[v[x][i]][x] = 0;}while (!q.empty()) {int u = q.front(); q.pop(); vis[u][x] = 0;for (int i = 0; i < c[u].size(); i++) {int to = c[u][i];if (dis[to][x] > dis[u][x] + 1) {dis[to][x] = dis[u][x] + 1;if (!vis[to][x]) {q.push(to); vis[to][x] = 1;}}}} }int main() {//ios::sync_with_stdio(0);cin >> n >> m >> k >> s;memset(dis, 0x3f, sizeof(dis)); for (int i = 1; i <= n; i++) {int x; rdint(x);v[x].push_back(i);}for (int i = 1; i <= m; i++) {int u, V; rdint(u); rdint(V);c[V].push_back(u); c[u].push_back(V);}for (int i = 1; i <= k; i++)bfs(i);for (int i = 1; i <= n; i++) {sort(dis[i] + 1, dis[i] + 1 + k);int ans = 0;for (int j = 1; j <= s; j++)ans += dis[i][j];cout << ans << " ";}return 0; }

?



轉(zhuǎn)載于:https://www.cnblogs.com/zxyqzy/p/10273636.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的CF986A Fair的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。