CSUOJ 1010 Water Drinking(BFS)
来源:互联网 发布:全国人口普查数据查询 编辑:程序博客网 时间:2024/06/06 08:25
1010: Water Drinking
Description
The Happy Desert is full of sands. There is only a kind of animal called camel living on the Happy Desert. ‘Cause they live here, they need water here. Fortunately, they find a pond which is full of water in the east corner of the desert. Though small, but enough. However, they still need to stand in lines to drink the water in the pond.
Now we mark the pond with number 0, and each of the camels with a specific number, starting from 1. And we use a pair of number to show the two adjacent camels, in which the former number is closer to the pond. There may be more than one lines starting from the pond and ending in a certain camel, but each line is always straight and has no forks.
Input
There’re multiple test cases. In each case, there is a number N (1≤N≤100000) followed by N lines of number pairs presenting the proximate two camels. There are 99999 camels at most.
Output
For each test case, output the camel’s number who is standing in the last position of its line but is closest to the pond. If there are more than one answer, output the one with the minimal number.
Sample Input
1
0 1
5
0 2
0 1
1 4
2 3
3 5
5
1 3
0 2
0 1
0 4
4 5
Sample Output
1
4
2
题目大意:在一个沙漠中有一个水池,一些骆驼在排队喝水。骆驼排成了很多队伍,队伍的起点都是水池,求排在队伍最后但是离水池最近的骆驼编号。
解题思路:建图然后BFS,用一个数组记录某个骆驼是否在队尾,最后比较距离,输出距离最近且编号最小的骆驼即可。
代码如下:
#include <bits/stdc++.h>#define INF 1e9using namespace std;const int maxn = 100005;int head[maxn],cnt,d[maxn],n;bool en[maxn];struct Edge{ int to,next;}edge[maxn];void init(){ cnt = 0; memset(head,-1,sizeof(head)); memset(en,0,sizeof(en));}void add_edge(int from,int to){ edge[cnt].to = to; edge[cnt].next = head[from]; head[from] = cnt++;}queue<int> que;void bfs(){ while(que.size()) que.pop(); for(int i = 0;i <= n;i++) d[i] = INF; d[0] = 0; que.push(0); while(que.size()){ int u = que.front(); que.pop(); if(head[u] == -1){ en[u] = true; continue; } for(int i = head[u];~i;i = edge[i].next){ int v = edge[i].to; d[v] = d[u] + 1; que.push(v); } } return ;}int main(void){ int t,a,b; while(cin >> n){ init(); for(int i = 0;i < n;i++){ cin >> a >> b; add_edge(a,b); } bfs(); int minVal = INF; int pos = INF; for(int i = 1;i <= n;i++){ if(en[i] && minVal > d[i]){ minVal = d[i]; pos = i; } } cout << pos << endl; } return 0;}
- CSUOJ 1010 Water Drinking(BFS)
- csu oj 1010 Water Drinking(BFS)
- CSU1010-Water Drinking-BFS
- CSU 1010 water drinking
- COJ-1010-Water Drinking
- Water Drinking
- CSUOJ 1511 残缺的棋盘(BFS)
- Is Your Drinking Water Giving You Diabetes?
- CSUOJ 1224 ACM小组的古怪象棋(BFS)
- CSUOJ 1726 你经历过绝望吗?两次!(BFS + 优先队列)
- CSUOJ 1302 - Walking on Chessboard 暴力BFS
- CSUOJ 1336 Interesting Calculator(BFS+优先队列)
- CSUOJ 1843 Jumping Monkey BFS,状态压缩
- CSUOJ 1891 Full Tank? 最短路 bfs
- CSUOJ
- CSUOJ
- SHU1962 Water and Fire Maze(三维BFS)
- CSUOJ 1829 Dungeon 最短路 BFS 预处理 增加维数
- Python之构造函数dict报错TypeError:'dict' object is not callable
- Android插件化学习之路(五)之代理Activity
- 关于Stm32定时器+ADC+DMA进行AD采样的实现
- Problem Five:[双亲数/POI Zap(多组)]
- 【.NET】<% %>的使用
- CSUOJ 1010 Water Drinking(BFS)
- Git 强大的二分 debug 功能 - git bisect
- 信息搜集之外围信息搜集
- Android Studio各种情况下导入library的教程(图解)
- nyoj 19 擅长排列的小明
- Python的浅拷贝和深拷贝
- Java-常用面板
- C语言课设:中缀表达式转后缀表达式并求值(续)
- 指数运算