CF 987 D. Fair
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的更多相关文章
- CF 987
毒瘤啊啊啊啊啊 虽然排名还不错,331,但是B我没做出来...... 这是战绩: 可以看到我大发神威势如破竹的A了CDE,但是B把我卡了三次...不然我就能进前300了(还是很水). 逐一分析题目: ...
- codeforces 987 D. Fair
D. Fair time limit per test 2 seconds memory limit per test 512 megabytes input standard input outpu ...
- [CF] 986 A. Fair
http://codeforces.com/problemset/problem/986/A n个点的无向连通图,每个点有一个属性,求每个点到s个不同属性点的最短距离 依稀记得那天晚上我和Menteu ...
- 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 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- CF D. Fair(思维+DFS)
http://codeforces.com/contest/987/problem/D 题目大概: 给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产).有k种不同特产. 要求 ...
- CF 986A Fair——多源bfs
题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...
- CF 986A Fair(多源BFS)
题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...
随机推荐
- python 打包文件
tarfile import tarfile tar = tarfile.open("sk_camera_6018.tar","w") tar.add(full ...
- 关于"XML 文档(2, 2)中有错误:不应有 <xml xmlns=''>"错误
XML文件名 <?xml version="1.0" encoding="utf-8"?> <Config xmlns:xsi="h ...
- 在Windos上安装Nginx
官网地址:http://nginx.org/en/download.html 1.下载 2.解压 3.启动 4.访问 打开cmd cd到nginx路径,使用命令关闭它 nginx.exe -s sto ...
- Maven搭建Spring MVC时使用jstl无效
1 Maven引入依赖jar包:jstl.jar和standard.jar <dependency> <groupId>javax.servlet</groupId> ...
- flashcache中内存与磁盘,磁盘与磁盘的io
flashcache中跟磁盘相关的读写分为以下两类: 1)磁盘跟内存的交互 2)磁盘跟磁盘之前的交互 比如说读不命中时就是直接从磁盘读,属于第1种情况,那读命中呢?也是属于第1种情况,不过这时候是从S ...
- docker 导出导入
容器导出 docker export -o myname.tar 容器id 容器导人 docker import myname.tar httpd:v1
- Docker 常用命令——容器
1.新建并启动容器 docker run [option] images [command][arg] #根据镜像新建容器并运行.如果本地没有镜像则从docker hub上拉取. --name ...
- TCP中的三次握手和四次挥手
三次握手:目的是同步连接双方的序列号和确认号 并交换 TCP窗口大小信息. 理论上跟通话一样: a: 你听的到吗? b: 我能听到.只需要两次就可以了,但建立连接阶段不是双向即时通信的,且最终的目的 ...
- GoogleMock初探(0)
在进行测试过程中,待测的类或者方法经常会依赖其他类或方法的实现.如果此时这些依赖还没有实现,则需要打桩.另外测试讲求独立,测试之间的互相依赖会导致测试最终混乱不堪. GoogleMock提供一套方法来 ...
- pyntho经典面试题
Python基础篇 1:为什么学习Python 2:通过什么途径学习Python 3:谈谈对Python和其他语言的区别 Python的优势: 4:简述解释型和编译型编程语言 5:Python的解释器 ...