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. PhoneGap 介绍

    一.PhoneGap 是什么 1.PhoneGap 是一个用基于 HTML,CSS 和 JavaScript 的,创建移动跨平台移动应用程序的快速开发框架. 2.它使开发者能够利用 iPhone,An ...

  2. 死磕salt系列-salt grains pillar 配置

    grains 和 pillar 对比: Grains:存放静态数据,主要存储客户端的主机信息,重启grains会刷新. Pillar: 处理敏感数据, 处理差异性的文件. Grains数据系统 sal ...

  3. 【jQuery】学习jQuery插件的使用与写法(表单验证插件-validation)

    最新最全的插件可以从jQuery官方网站的插件板块下载,网站地址为:http://plugins.jquery.com/ Validation优点:内置验证规则:自定义验证规则:简单强大的验证信息提示 ...

  4. 查看WIFI连接的信号强度

    netsh wlan show interface (netsh wlan show interface) -match '^\s+Signal' -replace '\s+Signal\s+:\s+ ...

  5. Python 模块化 from .. import 语句资源搜索顺序 (三)

    接着上一篇文章最后的import子句资源搜索顺序,我们来写几个例子了解下. 例一. #test1.py x = 123 #test.py import test1 print(dir()) print ...

  6. 关于numpy mean函数的axis参数

    import numpy as np X = np.array([[1, 2], [4, 5], [7, 8]]) print np.mean(X, axis=0, keepdims=True) pr ...

  7. [转]TortoiseSVN客户端的安装

    TortoiseSVN是windows平台下Subversion的免费开源客户端. 一般我们都是先讲讲服务器的配置,然后再讲客户端的使用,但是在TortoiseSVN上,却可以反过来.因为,如果你的要 ...

  8. 页面QQ在线咨询、在线交谈代码

    页面QQ在线咨询.在线交谈代码 样式一: <a target="blank" rel="nofollow" href="tencent://me ...

  9. ASP.Net GridView 基础 属性和事件

    GridView 控件激发的事件: 我们后期重点看的是RowCommand.RowCreated.RowDataBound这三个事件.

  10. Android UI【android 仿微信、QQ聊天,带表情,可翻页,带翻页拖动缓冲】

    http://blog.csdn.net/lnb333666/article/details/8546497 如题,这是公司项目的一个功能模块,先上个效果图: 其次大致说说原理: 1,首先判断输入的字 ...