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. python中的BaseManager通信(一)文件三分

    可以在windows下单机运行 主部分(提供服务器) #mainfirst.py from multiprocessing.managers import BaseManager import Que ...

  2. CATransaction:原子化操作、批量操作、整体设置、自动添加

    Transactions are CoreAnimation's mechanism for batching multiple layer- tree operations into atomic ...

  3. 基于LNMP的小米电子商务网站平台

    项目参考:http://www.cnblogs.com/along21/p/7822228.html 基于LNMP的小米电子商务网站平台 1.环境 setenforce 0 #关闭selinux sy ...

  4. 链表推导式 【list comprehension】

    x for x in x 链表推导式 [list comprehension]链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要 ...

  5. struts2的动态方法调用(DMI)和通配符映射

    动态方法调用   1.Struts2默认关闭DMI功能,需要使用需要手动打开,配置常量 struts.enable.DynamicMethodInvocation = true 2.使用“!”方法,即 ...

  6. java alibaba fastJson 遍历数组json

    import java.util.*; import com.alibaba.fastjson.*; public class Test { public static void main(Strin ...

  7. 决策树 - 可能是CART公式最严谨的介绍

    目录 决策树算法 ID3算法[1] C4.5 改进[1] "纯度"度量指标:信息增益率 离散化处理 CART(分类与回归树,二叉) 度量指标 二值化处理 不完整数据处理 CART生 ...

  8. from表单提交之前数据判空

    在input标签中写onclick事件,不管返回是真是假都会继续提交表单. 使用onsubmit事件 <form action="login.html" method='po ...

  9. (Les16 执行数据库恢复)-重做日志文件恢复

    丢失重做日志文件         丢失了重做日志文件组中的某个成员,并且组中至少还有一个成员:             -不会影响实例的正常操作.             -预警日志中会收到一条信息, ...

  10. JavaScript变量类型检测总结

    JavaScript中的变量类型: 基本类型值:Undefined,Null,Boolean,Number和String. 按值访问(可直接操作保存在变量中的变量值): 复制规则:当复制基本类型值时: ...