[CF] 986 A. Fair
http://codeforces.com/problemset/problem/986/A
n个点的无向连通图,每个点有一个属性,求每个点到s个不同属性点的最短距离
依稀记得那天晚上我和Menteur-Hxy探讨这道题如何不可做的样子
直观想法当然是每个点出发bfs,找到s个就停止,但这最差是n^2的,不能接受!
解法是多源广搜,注意到货物种类非常小(<=100),所以可以求出每个点获得每种货物的最短距离。
做法是进行k次bfs,对于第i次,起点st是每个种类为i的点,广搜的性质决定了她们的平行关系。
这样就可以求出每种货物到每个点的距离,换言之,就是每个点获得每种货物的代价(最小)
然后对于每个点,答案就是最小的s个啦
无向图注意开两倍边
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue> using namespace std; const int MAXN=; inline int rd(){
int ret=,f=;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-:;
while(isdigit(c))ret=ret*+c-'',c=getchar();
return ret*f;
} struct Edge{
int next,to;
}e[MAXN<<];
int ecnt,head[MAXN];
inline void add(int x,int y){
e[++ecnt].to = y;
e[ecnt].next = head[x];
head[x] = ecnt;
} int dis[MAXN][],typ[MAXN];
//dis[i][j] - the minimum cost for town i to get the j-th goods int n,m,k,s; queue<int> Q;
void bfs(int x){
for(int i=;i<=n;i++) if(typ[i]==x) Q.push(i),dis[i][x]=;
while(!Q.empty()){
int top=Q.front();Q.pop();
for(int i=head[top];i;i=e[i].next){
int v=e[i].to;
if(dis[v][x]>dis[top][x]+){
dis[v][x]=dis[top][x]+;
Q.push(v);
}
}
}
} void init(){
memset(dis,0x3f,sizeof(dis));
} vector<int> V; int main(){
n=rd();m=rd();k=rd();s=rd();
init();
for(int i=;i<=n;i++) typ[i]=rd();
int x,y,w;
for(int i=;i<=m;i++){
x=rd();y=rd();
add(x,y);add(y,x);
}
for(int i=;i<=k;i++) bfs(i);
int ret=;
for(int i=;i<=n;i++){
V.clear();
for(int j=;j<=k;j++) V.push_back(dis[i][j]);
sort(V.begin(),V.end());
ret=;
for(int j=;j<s;j++) ret+=V[j];
printf("%d ",ret);
}
return ;
}
[CF] 986 A. Fair的更多相关文章
- CF 987 D. Fair
D. Fair http://codeforces.com/contest/987/problem/D 题意: n个城镇m条道路,(保证没有重边,两个城镇间可以到达),每个城镇拥有的特产ai(可能多个 ...
- Winniechen’s test1
https://winniechen.cn/wp-content/uploads/2018/08/Winniechens_test_1.rar 放水练习赛,主要考察最短路,DP,状态压缩等知识点 题解 ...
- 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 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- CF 986A Fair——多源bfs
题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...
- CF D. Fair(思维+DFS)
http://codeforces.com/contest/987/problem/D 题目大概: 给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产).有k种不同特产. 要求 ...
- CF 986A Fair(多源BFS)
题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
随机推荐
- poj1724【最短路】
题意: 给出n个城市,然后给出m条单向路,给出了每条路的距离和花费,问一个人有k coins,在不超过money的情况下从1到n最短路径路径. 思路: 我相信很多人在上面那道题的影响下,肯定会想想,在 ...
- Codeforces687A【未完继续....】
http://codeforces.com/problemset/problem/687/A
- Codeforces 378C
DFS连通块,思路就是搜到底,然后一个一个回溯(填上X)上来 #include <iostream> #include <stdio.h> #include <strin ...
- SpringSercurity基础
创建 spring 配置文件 spring-security.xml intercept-url 表示拦截页面 /* 表示的是该目录下的资源,只包括本级目录不包括下级目录 /** 表示的是该目 ...
- 如何让Android微博个人详情页滚动到顶部
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/103 个人详情页滑动到顶部 最近产品提了个新需求,需要实现 ...
- hdu1598 find the most comfortable road 枚举+最小生成树
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 210 #define IN ...
- 用jquery的animate动画函数做的网页效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- POJ3417(树上差分)
会卡vector. ; int n, m, Ans; ], to[maxn * ], tot; int vis[maxn], f[maxn]; int d[maxn], sum[maxn]; vect ...
- Hibernate Could not obtain transaction-synchronized Session for current thread问题处理
项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...
- 生产环境中nginx既做web服务又做反向代理
一.写对于初入博客园的感想 众所周知,nginx是一个高性能的HTTP和反向代理服务器,在以前工作中要么实现http要么做反向代理或者负载均衡.尚未在同一台nginx或者集群上同时既实现HTTP又实现 ...