D. Fair

http://codeforces.com/contest/987/problem/D

题意:

  n个城镇m条道路,(保证没有重边,两个城镇间可以到达),每个城镇拥有的特产ai(可能多个城镇有相同特产)。总共有k种不同特产。每个城镇举办展会需要至少s种特产,一份特产从一个城镇运到另一个城镇的花费为最短路(每次只能运一个)。求出在每个城镇举办展会的最小花费。

分析:

  bfs。

  如果直接枚举每个城镇作为举办展会,然后计算到每个特产的最短路,取出前s个,复杂度n^2。

  发现k<=100,所以可以反过来枚举,枚举每特产到每个城镇的最短路,多源bfs跑一下,最后枚举每个诚镇计算答案。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
const int INF = 1e9; struct Edge{
int to, nxt;
}e[N << ];
int head[N], En, dis[N][], q[N];
vector<int> vec[]; void add_edge(int u,int v) {
++En; e[En].to = v; e[En].nxt = head[u]; head[u] = En;
++En; e[En].to = u; e[En].nxt = head[v]; head[v] = En;
} void bfs(int x) {
int L = , R = ;
for (int sz = vec[x].size(), i = ; i < sz; ++i) {
q[++R] = vec[x][i];
dis[vec[x][i]][x] = ;
}
while (L <= R) {
int u = q[L++];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v][x] == INF) {
dis[v][x] = dis[u][x] + ;
q[++R] = v;
}
}
}
} int main() {
int n = read(), m = read(), k = read(), s = read();
for (int i = ; i <= n; ++i) vec[read()].push_back(i);
for (int i = ; i <= m; ++i) {
int u = read(), v = read();
add_edge(u, v);
}
for (int i = ; i <= k; ++i) {
for (int j = ; j <= n; ++j) dis[j][i] = INF;
bfs(i);
} for (int i = ; i <= n; ++i) {
int ans = ;
sort(dis[i] + , dis[i] + k + );
for (int j = ; j <= s; ++j) ans += dis[i][j];
printf("%d ",ans);
} return ;
}

CF 987 D. Fair的更多相关文章

  1. CF 987

    毒瘤啊啊啊啊啊 虽然排名还不错,331,但是B我没做出来...... 这是战绩: 可以看到我大发神威势如破竹的A了CDE,但是B把我卡了三次...不然我就能进前300了(还是很水). 逐一分析题目: ...

  2. codeforces 987 D. Fair

    D. Fair time limit per test 2 seconds memory limit per test 512 megabytes input standard input outpu ...

  3. [CF] 986 A. Fair

    http://codeforces.com/problemset/problem/986/A n个点的无向连通图,每个点有一个属性,求每个点到s个不同属性点的最短距离 依稀记得那天晚上我和Menteu ...

  4. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. CF 1083 B. The Fair Nut and Strings

    B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析:  建出trie树,给定的两个字符串就 ...

  6. CF 1083 A. The Fair Nut and the Best Path

    A. The Fair Nut and the Best Path https://codeforces.com/contest/1083/problem/A 题意: 在一棵树内找一条路径,使得从起点 ...

  7. CF D. Fair(思维+DFS)

    http://codeforces.com/contest/987/problem/D 题目大概: 给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产).有k种不同特产. 要求 ...

  8. CF 986A Fair——多源bfs

    题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...

  9. CF 986A Fair(多源BFS)

    题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...

随机推荐

  1. 使用Jwt为.Net Core SignalR保驾护航

    前言 SignalR Demo搭建起来之后,是没有相应的认证和授权功能的.于是乎,参考官方文档实现了相应的逻辑. 简单认证 首先使用稍微简单的方式实现.添加如下代码: services.AddAuth ...

  2. SQL server数据异地备份到OSS

    背景需求: 某部门需要将windows机器上的SQL server数据做一个异地备份,经过对现有的软硬件资源评估,决定使用阿里云的OSS存储 方法:利用SQLserver自带的维护计划做指定数据库的备 ...

  3. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. POJ 1182 食物链(经典带权并查集 向量思维模式 很重要)

    传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...

  5. POJ 2524 独一无二的宗教(裸并查集)

    题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K ...

  6. iOS dyld: Library not loaded 报错解决

    Xcode 用的是10.1 版本打的苹果包在 ios系统10.0 以上可以正常运行 但是系统9.3的手机安装后直接运行就崩溃 后来插上电脑联调 报错 dyld: Library not loaded: ...

  7. ssm多数据源的操作

    公司要求,需要使用两个数据库,一个mysql,一个oracle.所以需要配置两个数据库来进行操作. 1.首先,需要在jdbc.properties文件中将两个库的配置数据写入,不过一个写driver, ...

  8. Myeclipse报错:The word is not correctly spelled

    在eclipse下的Window--Preference输入spell,然后把第一个复选框“Enable spell checking"给去掉就可以了.

  9. Oracle之plsql及游标

    --1.赋值 --:= 赋值 declare var_name ) :='&请输入名字';--&是一个提示输入的特殊符号,会打开一个输入框 var_age ) :='&请输入年 ...

  10. Flume(2)-拓扑结构与Agent内部原理

    一. 拓扑结构 1. 串行模式 这种模式是将多个flume给顺序连接起来了,从最初的source开始到最终sink传送的目的存储系统.此模式不建议桥接过多的flume数量, flume数量过多不仅会影 ...