[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连试试水的深浅..... ...
随机推荐
- 用ffmpeg切割音频文件
ffmpeg -i audio.wav -f segment -segment_time -c copy audio%02d.wav "-segment_time 60" 表示每6 ...
- centos6 文件管理
一.文件属性 权限位: - 表示文件 d 表示目录 l 表示软连接 b 表示接口存储设备文件 c 表示串行端口设备 文件的时间属性 [root@web02 ~]# ll /etc/passwd ### ...
- 模板 求GCD&LCM
求最大公倍数 int GCD(int a,int b) { ) return b; else return GCD(b,a%b); } 求最小公倍数 int LCM(int a,int b) { re ...
- S3C2440的内存情况在NAND FLASH或者NOR FLASH启动的情况下
1,从NANDFLASH启动时,在ARM上电时,ARM会自动把NANDFLASH前4K的内容拷贝到S3C2440内部SRAM中,同时把SRAM的地址映射到0X00000000.ARM上电后会从SRAM ...
- 添物零基础到架构师(基础篇) - JavaScript
JavaScript是什么? JavaScript是web开发必须学习的,ECMAScript是其规则来源. JavaScript的历史 Developed by Brendan Eich of Ne ...
- 587. Erect the Fence
Problem statement: There are some trees, where each tree is represented by (x,y) coordinate in a two ...
- MTK平台释疑android M 配置中断相关问题
1.使用老方法(android L)配置中断,调用request_irq函数时出错,错误代码 -22 Dear Customer: 您好! 如电话沟通,贵司可以在发过来的code基础上做下面的修改再 ...
- 仪仗队(bzoj 2190)
Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是 ...
- android开发里跳过的坑——onActivityResult在启动另一个activity的时候马上回调
该问题是由于被启动的activity的launchMode为singleTask模式,该模式下不可以使用onActivityResult,要使用onActivityResult,被启动的activit ...
- Linux下汇编语言学习笔记55 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...