[Codeforces Round 486A] Fair
[题目链接]
https://codeforces.com/contest/986/problem/A
[算法]
用dist(i,j)表示第i种食物运到第j个城市需要的最小代价
将所有特产为第i中食物的城市加入队列 , 进行广度优先搜索BFS
显然 , 对于每个城市 , 答案为到该城市代价前s小的食物代价和
时间复杂度 : O(k(m + n))
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + ;
const int MAXK = ;
const int inf = 1e9; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , m , k , s , tot;
int value[MAXN],head[MAXN];
int dist[MAXK][MAXN];
vector< int > a[MAXK]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
} int main()
{ read(n); read(m); read(k); read(s);
for (int i = ; i <= n; i++)
{
int x;
read(x);
a[x].push_back(i);
}
for (int i = ; i <= m; i++)
{
int u , v;
read(u); read(v);
addedge(u,v);
addedge(v,u);
}
for (int i = ; i <= k; i++)
{
queue< int > q;
while (!q.empty()) q.pop();
for (int j = ; j <= n; j++) dist[i][j] = inf;
for (unsigned j = ; j < a[i].size(); j++)
{
dist[i][a[i][j]] = ;
q.push(a[i][j]);
}
while (!q.empty())
{
int cur = q.front();
q.pop();
for (int j = head[cur]; j; j = e[j].nxt)
{
int v = e[j].to;
if (dist[i][cur] + < dist[i][v])
{
dist[i][v] = dist[i][cur] + ;
q.push(v);
}
}
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= k; j++) value[j] = dist[j][i];
sort(value + ,value + k + );
int ans = ;
for (int j = ; j <= s; j++) ans += value[j];
printf("%d ",ans);
}
printf("\n"); return ; }
[Codeforces Round 486A] Fair的更多相关文章
- Codeforces Round #485 (Div. 2) D. Fair
Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...
- Codeforces Round #436 (Div. 2)【A、B、C、D、E】
Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...
- Codeforces Round #272 (Div. 2) 题解
Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...
- 【codeforces】Codeforces Round #277 (Div. 2) 解读
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- MapReduce架构与执行流程
一.MapReduce是用于解决什么问题的? 每一种技术的出现都是用来解决实际问题的,否则必将是昙花一现,那么MapReduce是用来解决什么实际的业务呢? 首先来看一下MapReduce官方定义: ...
- Java 集合相关
对整个体系做一个记录,并不涉及详细应用 Object类 1. 重写toString方法 System.out.println可以打印任何对象在于Object类拥有一个方法 public String ...
- Hadoop入坑记
Hadoop入坑记 按照官方给的伪分布式配置 etc/hadoop/core-site.xml: <configuration> <property> <name> ...
- manacher模板整理
//p[]为最长回文半径长度,id为当前最靠右端回文串的中心点(多个取最靠左),mx为id对应的回文串的最右端坐标+1void manacher(char *s,int len){ p[] = ; , ...
- LaTex/Overleaf使用笔记
1. 添加网页引用 @misc{ r1, author = "Wikipedia", title = "Binary decision diagram --- { ...
- 全文检索(AB-1)-相关领域
信息检索 认知科学 管理科学
- Spring & Java
Spring & Java https://spring.io/ Spring Boot https://www.shiyanlou.com/courses/1152 Spring Boot入 ...
- mysql启服务的时候报1067错误解决办法
网上百度了半天没有有效的办法,推荐一个万能的办法: 1.看日志: mysql安装目录下 data文件 -> 后缀为.err 的文件就是日志文件 打开它 : 可以看到错误信息 2.看错 ...
- Flask(1):基本示例、配置文件、路由、请求和响应、模板渲染
Flask的特点: - pip install flask - 短小精悍.可扩展性强的 web框架 注意:上下文管理机制 - 依赖 wsgi:werkzeug Flask的简单示例: from fla ...
- HTML5调用传感器的资料汇总
都可以调用:devicetemperature(温度).devicepressure(压力).devicehumidity(湿度).devicelight(光).devicenoise(声音).dev ...