[题目链接]

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. build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing

    maven test项目时遇到一下错误 Some problems were encountered while building the effective model for cn.temptat ...

  2. 【分治】输出前k大的数

    描述 给定一个数组,统计前k大的数并且把这k个数从大到小输出. 输入第一行包含一个整数n,表示数组的大小.n < 100000.第二行包含n个整数,表示数组的元素,整数之间以一个空格分开.每个整 ...

  3. 開啟活動監視器 (SQL Server Management Studio)

    本主題描述如何開啟 [活動監視器] 來取得有關 SQL Server 處理序以及這些處理序如何影響目前 SQL Server 執行個體的資訊. 此外,本主題也描述如何設定 [活動監視器] 的重新整理間 ...

  4. MySQL-Transfer2.2发布

    http://dinglin.iteye.com/blog/1888640 Transfer 2.2发布.下载地址 版本说明 1.  基于版本 Percona-5.5.31 ,简单用法是先安装好官方或 ...

  5. Thinkphp5.0 的响应方式

    Thinkphp5.0 的响应方式 $res = config('default_return_type'); dump($res);//默认是html //修改为json \think\Config ...

  6. Mayor's posters POJ - 2528

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  7. [bzoj5101][POI2018]Powódź_并查集

    Powódź bzoj-5101 POI-2018 题目大意:在地面上有一个水箱,它的俯视图被划分成了$n$行$m$列个方格,相邻两个方格之间有一堵厚度可以忽略不计的墙,水箱与外界之间有一堵高度无穷大 ...

  8. Just a Hook-HDU1698(线段树求区间)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem Description In the game of DotA, Pudge’s meat ...

  9. AE After Effect 渲染如何输出设置

    各种输出设置值的对比情况. Microsoft Video1压缩方法情况(该模式下无法采用RGB+Alpha): 一 深度为"数千种颜色",缩放为1280×720(HDV/HDTV ...

  10. Java实现二叉搜索树及相关操作

    package com.tree; import com.tree.BitNode; /** * * 二叉搜索树:一个节点的左子节点的关键字小于这个节点.右子节点的关键字大于或等于这个父节点 * * ...