Gym 101170I Iron and Coal(BFS + 思维)题解
题意:有一个有向图,有些点是煤,有些点是铁,但不会同时有铁和煤。现在我要从1出发,占领可以到达的点。问最少占领几个点能同时拥有一个煤和一个铁(1不用占领)。
思路:思路很秀啊。我们从1往外bfs,得到所有点到1的最短路dis[i][0],然后从所有煤跑bfs得到所有点到煤的最短路dis[i][1],我们再从所有铁跑bfs得到所有点到铁的最短路dis[i][2],那么dis[i][0] + dis[i][1] + dis[i][2]就是以i为分界点分别前往煤和铁的占领的最小距离。那么答案就是min{ dis[i][0] + dis[i][1] + dis[i][2] }。
代码:
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + 10;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, m, k;
int iron[maxn], coal[maxn], vis[maxn];
vector<int> G[maxn], g[maxn];
int dis[maxn][3];
struct node{
int x;
int step;
};
void bfs1(){
for(int i = 1; i <= n; i++) vis[i] = 0;
queue<node> q;
while(!q.empty()) q.pop();
vis[1] = 1;
dis[1][0] = 0;
node a, b;
a.x = 1, a.step = 0;
q.push(a);
while(!q.empty()){
a = q.front();
q.pop();
for(int i = 0; i < G[a.x].size(); i++){
int v = G[a.x][i];
if(vis[v]) continue;
vis[v] = 1;
dis[v][0] = a.step + 1;
b.x = v, b.step = a.step + 1;
q.push(b);
}
}
}
void bfs2(){
for(int i = 1; i <= n; i++) vis[i] = 0;
queue<node> q;
while(!q.empty()) q.pop();
node a, b;
for(int i = 1; i <= n; i++){
if(iron[i]){
vis[i] = 1;
dis[i][1] = 0;
a.x = i, a.step = 0;
q.push(a);
}
}
while(!q.empty()){
a = q.front();
q.pop();
for(int i = 0; i < g[a.x].size(); i++){
int v = g[a.x][i];
if(vis[v]) continue;
vis[v] = 1;
dis[v][1] = a.step + 1;
b.x = v, b.step = a.step + 1;
q.push(b);
}
}
}
void bfs3(){
for(int i = 1; i <= n; i++) vis[i] = 0;
queue<node> q;
while(!q.empty()) q.pop();
node a, b;
for(int i = 1; i <= n; i++){
if(coal[i]){
vis[i] = 1;
dis[i][2] = 0;
a.x = i, a.step = 0;
q.push(a);
}
}
while(!q.empty()){
a = q.front();
q.pop();
for(int i = 0; i < g[a.x].size(); i++){
int v = g[a.x][i];
if(vis[v]) continue;
vis[v] = 1;
dis[v][2] = a.step + 1;
b.x = v, b.step = a.step + 1;
q.push(b);
}
}
}
int main(){
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= n; i++){
iron[i] = coal[i] = 0;
G[i].clear();
g[i].clear();
memset(dis[i], INF, sizeof(dis[i]));
}
for(int i = 1; i <= m; i++){
int u;
scanf("%d", &u);
iron[u] = 1;
}
for(int i = 1; i <= k; i++){
int u;
scanf("%d", &u);
coal[u] = 1;
}
for(int i = 1; i <= n; i++){
int p, u, v;
scanf("%d", &p);
u = i;
while(p--){
scanf("%d", &v);
G[u].push_back(v);
g[v].push_back(u);
}
}
bfs1();
bfs2();
bfs3();
int ans = INF;
for(int i = 1; i <= n; i++){
if(dis[i][0] == INF || dis[i][1] == INF || dis[i][2] == INF) continue;
ans = min(ans, dis[i][0] + dis[i][1] + dis[i][2]);
}
if(ans < INF) printf("%d\n", ans);
else printf("impossible\n");
return 0;
}
Gym 101170I Iron and Coal(BFS + 思维)题解的更多相关文章
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- Gym - 101572D Distinctive Character bfs 思维
题目传送门 题目大意: 给出n个01串,让你构造一个字符串,使这个字符串和这些字符串中相似程度最高 尽可能低.如果两个字符串对应位置相同,则相似程度加一. 思路: 每一个01串更改自己的一部分后,都可 ...
- Codeforces Gym101170I:Iron and Coal(建多幅图+多次BFS)***
题目链接 题意 有n个点,其中有m个点是铁矿,k个点是煤,从1号点出发,你可以派一些士兵跑向不同的点,问占领至少一个铁矿和一个煤的时候,最少需要占领多少个点. 思路 建两幅图,其中一幅是正向边,一幅是 ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- Gym - 100187E E - Two Labyrinths —— bfs
题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Gym 101617J Treasure Map(bfs暴力)
http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...
- 逃离迷宫(BFS)题解
Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...
随机推荐
- ES6在工作中会用到的核心知识点讲解
一.var, let, const 谈到ES6,估计大家首先肯定会想到var,let,const 咱就先谈谈这三者的区别 var a = 3; { var a = 4; } console.log(a ...
- Java并发组件二之CyclicBarriar
使用场景: 多个线程相互等待,直到都满足条件之后,才能执行后续的操作.CyclicBarrier描述的是各个线程之间相互等待的关系. 使用步骤: 正常实例化:CyclicBarrier sCyclic ...
- Docker逃逸
初识Docker逃逸 - FreeBuf网络安全行业门户 https://www.freebuf.com/articles/container/242763.html
- 三路握手 三报文握手 TIME_WAIT three way handshake three message handshake
UNIX网络编程卷1:套接字联网API(第3版) 第2章 传输层:TCP.UDP和SCTP 2.4 TCP TCP不能被描述成100%可靠的协议 数次重传失败,则放弃 数据的可靠递送和故障的可靠通知 ...
- MonkeyRunner使用
#-*- coding:utf-8 –*- from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage #连接 ...
- 理解前端模块概念:CommonJs与ES6Module
前言 现代前端开发每时每刻都和模块打交道.例如,在项目中引入一个插件,或者实现一个供全局使用组件的JS文件.这些都可以称为模块. 在设计程序结构时,不可能把所有代码都放在一起.更为友好的组织方式时按照 ...
- DP中的树上边/点覆盖问题
目录 树上边覆盖问题 例:luoguP2016 战略游戏 简述题意: Solution: Code 树上点覆盖问题 简述题意 Solution Code: 树上边覆盖问题 例:luoguP2016 战 ...
- 非Windows系统 如何解压带中文密码和中文文件名的zip压缩文件
数据科学交流群,群号:189158789 ,欢迎各位对数据科学感兴趣的小伙伴的加入! 一.安装unar软件包: Linux(Debian系列): apt install unarLinux(RedHa ...
- ts中提示“绑定元素“routes”隐式具有“any”类型”
思考单独的.ts和typescript框架在vscode层的差异,想想只有launch.json和tsconfig.json,一个个属性过去后,发现是tsconfig.json配置项"noI ...
- 寻找最小字符串,IP地址——解题报告
寻找最小字符串 题目 思路 在寻找最小字符串的时候需要用到的函数有strcmp和strcpy,首先先输入输入字符串,先假设第一个字符串为最小的字符串min,再然比较接下来的字符串,当接下来的字符串比m ...