Bus Pass BFS搜索

                    Bus Pass

题目抽象:给出nz个点组成的连通图,给出每个点的连接顶点。给出nr条路径。要求找出某点,使得到达每条路径上的每个顶点的距离的最大值最小。

分析:以每条路径上的顶点为起点,bfs搜索。具体见代码。

  1 /********************************
  2 please don‘t hack me!! /(ToT)/~~
  3                 __------__
  4               /~          ~  5              |    //^\\//^\|
  6            /~~\  ||  T| |T|:~  7           | |6   ||___|_|_||:|
  8            \__.  /      o  \/‘
  9             |   (       O   )
 10    /~~~~\    `\  \         /
 11   | |~~\ |     )  ~------~` 12  /‘ |  | |   /     ____ /~~~) 13 (_/‘   | | |     /‘    |    ( |
 14        | | |     \    /   __)/  15        \  \ \      \/    /‘ \   ` 16          \  \|\        /   | |\___|
 17            \ |  \____/     | |
 18            /^~>  \        _/ <
 19           |  |         \        20           |  | \        \         21           -^-\  \       |        )
 22                `\_______/^\______/
 23 ************************************/
 24
 25 #include <iostream>
 26 #include <cstdio>
 27 #include <cstring>
 28 #include <cmath>
 29 #include <algorithm>
 30 #include <string>
 31 #include <vector>
 32 #include <set>
 33 #include <map>
 34 #include <queue>
 35 #include <stack>
 36 #include <cstdlib>
 37 #include <sstream>
 38 using namespace std;
 39 typedef long long LL;
 40 const LL INF = 0x5fffffff;
 41 const double EXP = 1E-8;
 42 const LL MOD = (LL)1E9+7;
 43 const int MS = 10000;
 44
 45 int nz, nr;
 46 int cnt[MS];
 47 int edges[MS][11];
 48 int dis[MS];
 49 int reach[MS];   //可以bfs防止重复访问某节点,也可以判断是否存在标号为i的地区
 50 int cur;         //reach[i] == cur. 表示地区i在第cur+ 1站访问到。
 51
 52 void bfs(int x) {
 53     int val = 1;  // 层次
 54     queue<int> que[2];   //  层次扩展的滚动队列
 55     int a = 0, b = 1;
 56     if (reach[x] < cur) {
 57         reach[x] = cur;
 58         que[b].push(x);
 59         dis[x] = max(dis[x],val);
 60         val++;
 61     }
 62     while (!que[b].empty()) {
 63         swap(a,b);
 64         while (!que[a].empty()) {
 65             int u = que[a].front();
 66             que[a].pop();
 67             for (int i = 0; i < cnt[u]; i++) {
 68                 int v = edges[u][i];
 69                 if (reach[v] < cur) { //如果在本站中访问了,那么reach[v] == cur.
 70                     reach[v] = cur;
 71                     dis[v] = max(dis[v],val);
 72                     que[b].push(v);
 73                 }
 74             }
 75         }
 76         val++;
 77     }
 78 }
 79
 80 int main() {
 81     int T;
 82     scanf("%d",&T);
 83     while (T--) {
 84         scanf("%d%d",&nz,&nr);
 85         int id,x;
 86         for (int i = 0; i < nz; i++) {
 87             scanf("%d",&id);
 88             scanf("%d",&cnt[id]);
 89             for (int j = 0; j < cnt[id]; j++) {
 90                 scanf("%d",&edges[id][j]);
 91             }
 92         }
 93         memset(dis,0,sizeof(dis));
 94         memset(reach,-1,sizeof(reach));
 95         cur = 0;
 96         for (int i = 0; i < nr; i++) {
 97             int n;
 98             scanf("%d",&n);
 99             for (int j = 0; j < n; j++) {
100                 scanf("%d",&x);
101                 bfs(x);
102                 cur++;
103             }
104         }
105         int minv = INF;
106         int center = 0;
107         for (int i = 0; i < MS; i++) {
108             //因为题目中并没有说编号是连续的,地区的编号可能是随意的。
109             //我们可以用reach[i] == cur -1 来判断是否存在编号为i的地区。
110             if (reach[i] == cur -1 && dis[i] < minv) {
111                 minv = dis[i];
112                 center = i;
113             }
114         }
115         printf("%d %d\n",minv,center);
116     }
117     return 0;
118 }
时间: 2024-10-10 15:33:02

Bus Pass BFS搜索的相关文章

Zoj 2913 Bus Pass BFS

题意: 给你一张图,和一些指定的点,找一个点使得这些指定的点到这个点的距离的最大值最小 对每一个指定的点都做一遍BFS,更新到达每个点的距离,取较大值,然后扫一遍所有的点,找出最小即可. 注意:不同于走格子,因为方向比较多,所以要在扩展节点的时候就更新vis数组,不然有可能导致某个点的距离因为重复更新而不是最小值. 并且不要漏了一开始就更新起点的vis #include <cstdio> #include <vector> #include <cstring> #inc

ZOJ 2913 Bus Pass (最近的最远BFS HDU2377)

题意  在所有城市中找一个中心满足这个中心到所有公交站点距离的最大值最小 输出最小距离和满足最小距离编号最小的中心 最基础的BFS  对每个公交站点BFS  dis[i]表示编号为i的点到所有公交站点距离的最大值  bfs完所有站点后  dis[i]最小的点就是要求的点咯 #include<cstdio> #include<cstring> #include<queue> #include<set> using namespace std; typedef

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

Bus Pass

ZOJ Problem Set - 2913 Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The wa

ZOJ2913Bus Pass(BFS+set)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

HDU 2377 Bus Pass spfa

Problem Description You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus system works in your country (and also in t

hdu 2377 Bus Pass

Bus Pass Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 667    Accepted Submission(s): 271 Problem Description You travel a lot by bus and the costs of all the seperate tickets are starting to

hiho_1139_二分+bfs搜索

题目 给定N个点和M条边,从点1出发,寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径的路径中最长边长度的最小值. 题目链接:二分     最小化最大值,考虑采用二分搜索.对所有的边长进行排序,二分,对每次选择的边长,判断是否存在一条路径满足路径上所有的边长度均小于等于该边长,且路径中的边的个数小于等于K.     在判断路径是否存在的时候,使用BFS搜索,因为BFS用于寻找最短(边的个数最少)路径. 实现 #include<stdio.h> #include<string.h

hdu1181 bfs搜索之变形课

原题地址 这道题数据据说比较水,除了第一组数据是Yes以外,其余都是No,很多人抓住这点就水过了.当然了,我觉得那样过了也没什么意思.刷oj刷的是质量不是数量.这道题从题目上来看是个不错的 搜索题,解法多种多样,有 dfs,bfs,并查集,dijkstra算法都能解决. 题目分析: 题目中给了很多字符串,但是关心的只是字符串的第一个和最后一个字符.咋看起来,貌似是要建立一个个字符间的"映射",其实不然,这其实可以转化为一张26*26的有向图.有最多26个结点,原先的字符间映射关系就转化