[题目链接]

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的更多相关文章

  1. Codeforces Round #485 (Div. 2) D. Fair

    Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...

  2. Codeforces Round #436 (Div. 2)【A、B、C、D、E】

    Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...

  3. Codeforces Round #272 (Div. 2) 题解

    Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...

  4. 【codeforces】Codeforces Round #277 (Div. 2) 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

  5. Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)

    这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...

  6. 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 ...

  7. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  8. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. 零基础入门学习Python(35)--图形用户界面入门:EasyGui

    知识点 EasyGui学习文档[超详细中文版] 1. 建议不要在IDLE上运行EasyGui EasyGui是运行在TKinter上并拥有自身的事件循环,而IDLE也是Tkinter写的一个应用程序并 ...

  2. python3.x Day4 模块!!

    json and pickle模块 用途是为了持久化信息,这种持久化方式可以和其他程序语言兼容,一般都支持json,json只能持久化数据,pickle是python特有的方式,可以持久化所有信息和数 ...

  3. NodeJs中数据库的使用

    另一遍通用的NODEJS数据库方法koa,express,node 通用方法连接MySQL 1.Node.js 连接 MySQL $ cnpm install mysql 连接mysql: var m ...

  4. scrapy爬取简书整站文章

    在这里我们使用CrawlSpider爬虫模板, 通过其过滤规则进行抓取, 并将抓取后的结果存入mysql中,下面直接上代码: jianshu_spider.py # -*- coding: utf-8 ...

  5. Linux备份-删除指定日期内文件

    #!/usr/bin/env bash source /etc/profile echo " *************** start filter ***************  &q ...

  6. 贪吃的九头龙(tyvj P1523)

    T2 .tyvj   P1523贪吃的九头龙 描述 传说中的九头龙是一种特别贪吃的动物.虽然名字叫“九头龙”,但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于 ...

  7. POJ1422 Air Raid

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8006   Accepted: 4803 Description Consi ...

  8. hdu - 1627 Krypton Factor (dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1627 给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的. 按照格式输出. ...

  9. hdu_1085_Holding Bin-Laden Captive!_201404261008

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. 【转载】Spring Boot【快速入门】2019.05.19

    原文出处:https://www.cnblogs.com/wmyskxz/p/9010832.html   Spring Boot 概述 Build Anything with Spring Boot ...