(算法)Travel Information Center
题目:
Aps Island has many cities. In the summer, many travellers will come to the island and attend festive events in different cities. The festive events in Aps Island are crazy. Once it starts, it will never end. In the following sentences, the cities which have festive events are called festive cities.
At the beginning, only city No. 1 is festive city. If a new city becomes festive city, the government will tellthe information center about this news.
Everyday, the information center will receive many inquiries from travellers from different cities of this land. They want to know the closest festive city, and calculate the distance (If current city has festive event, the distance is 0).
Due to the growing number of the travellers, the information center is overloaded. The government wants to fix the problem by developing a system to handle the inquiries automatically.
As a fact, cities in Aps Island are connected with highways(bidirectional, length of every highway is 1). Any two cities are connected directly or indirectly, and there is ONLY one path between any 2 cities.
Input:
There are two integers in the first line, n (2<=n<=10^5) and m (1<=m<=10^5), n is the number of cities in the Aps Island and m is the number of queries.
The coming n-1 lines are the highways which connect two cities. In the line, there are two integers ai and bi (1<=ai,bi<=n,ai!=bi), representing two cities. Each line means the highway connecting the two cities.
Next m lines are inquiries from travellers or news from government. Each line has two integers qi andci (1<=qi<=2,1<=ci<=n). If qi=1, the government announces a new festive city ci. If qi=2, you have to find and print the shortest distance from the city ci to the closest festive city.
Output:
Results from each (qi = 2) Questions. Print every result with a new line.
C++
int main(){
// TODO: Implement your program
}
Sample Test
input
5 5
1 2
1 3
3 4
3 5
2 5
2 3
1 3
2 3
2 4
output
2
1
0
1
思路:
1、DFS
2、算法优化
代码:
1、DFS
#include<iostream>
#include<vector> using namespace std; struct Node{
vector<int> adjList;
}; void dfs(const vector<Node> &cities,vector<int> &dis,int x,int p){
vector<int> adj=cities[x].adjList;
for(int i=;i<adj.size();i++){
if(adj[i]==p)
continue;
if(dis[adj[i]]==- || dis[adj[i]]>dis[x]+){
dis[adj[i]]=dis[x]+;
dfs(cities,dis,adj[i],x);
}
}
} int main(){
int city_num;
int query_num;
int city_1,city_2; //input: two connected cities
int query,city; //input: query type and city
while(cin>>city_num && cin>>query_num){
if(city_num> && query_num>){
vector<Node> cities(city_num+);
vector<int> distances(city_num+,-);
// input information
for(int i=;i<city_num-;i++){
if(cin>>city_1 && cin>>city_2){
if(city_1> && city_1<=city_num && city_2> && city_2<=city_num){
cities[city_1].adjList.push_back(city_2);
cities[city_2].adjList.push_back(city_1);
}
else
return ;
}
} distances[]=;
dfs(cities,distances,,); for(int i=;i<query_num;i++){
cin>>query>>city;
if(query==){
distances[city]=;
dfs(cities,distances,city,);
}
else
cout<<distances[city]<<endl;
}
}
} return ;
}
2、算法优化
#include<iostream>
#include<vector> using namespace std; #define MIN_DISTANCE 1000000
typedef struct Node CityNode; /***** definition of data structure about each city *****/
struct Node{
int parent;
int depth;
bool isFestival;
vector<int> adjList;
Node():parent(-),depth(),isFestival(false){}
}; /***** function declaration *****/
// compute parent and depth of each node on the tree
void getParentAndDepth(vector<CityNode> &citites,int city_num);
// compute distance from the festival city by finding the nearear common parents
int getDistFromFesCity(vector<CityNode> &cities,int cur_city,int fes_city,vector<vector<int> > &distances); /***** main function *****/
int main(){
int city_num;
int query_num;
int city_1,city_2; //input: two connected cities
int query,city; //input: query type and city
while(cin>>city_num && cin>>query_num){
if(city_num> && query_num>){
vector<CityNode> cities(city_num);
vector<vector<int> > distances(city_num,vector<int>(city_num,));
// input information
for(int i=;i<city_num-;i++){
if(cin>>city_1 && cin>>city_2){
if(city_1> && city_1<=city_num && city_2> && city_2<=city_num){
cities[city_1-].adjList.push_back(city_2-);
cities[city_2-].adjList.push_back(city_1-);
}
else
return ;
}
} // compute parent,depth of each node on the tree
getParentAndDepth(cities,city_num); vector<int> festivalCity; //city who announced as festival city
vector<int> miniDist; // minimum distance of each query
festivalCity.push_back();
cities[].isFestival=true;
int dist; // find the nearest path from all festival cities
for(int i=;i<query_num;i++){
if(cin>>query && cin>>city){
int nearest=MIN_DISTANCE;
// if query==1, add to festival cities
if(query== && city> && city<=city_num){
festivalCity.push_back(city-);
cities[city].isFestival=true;
}
// if query==2, find the nearest festival city
else if(query== && city> && city<=city_num){
for(int k=;k<festivalCity.size();k++){
if(distances[city-][festivalCity[k]]!=)
dist=distances[city-][festivalCity[k]];
else
dist=getDistFromFesCity(cities,city-,festivalCity[k],distances);
if(dist<nearest)
nearest=dist;
}
miniDist.push_back(nearest);
}
else
return ;
}
} for(int i=;i<miniDist.size();i++)
cout<<miniDist[i]<<endl;
}
}
return ;
} void getParentAndDepth(vector<CityNode> &cities,int city_num){
vector<int> stk;
stk.push_back();
int node;
int v;
int count=;
while(!stk.empty() && count<city_num){
node=stk.back();
stk.pop_back();
for(int i=;i<cities[node].adjList.size();i++){
v=cities[node].adjList[i];
if(v== ||cities[v].parent!=-)
continue;
cities[v].parent=node;
cities[v].depth=cities[node].depth+;
stk.push_back(v);
count++;
}
}
} int getDistFromFesCity(vector<CityNode> &cities,int cur_city,int fes_city,vector<vector<int> > &distances){
int a=cur_city;
int b=fes_city; if(a==b)
return ;
int dist=;
while(cities[a].depth>cities[b].depth){
a=cities[a].parent;
dist++;
}
while(cities[a].depth<cities[b].depth){
b=cities[b].parent;
dist++;
}
while(a!=b){
a=cities[a].parent;
dist++;
b=cities[b].parent;
dist++;
} distances[cur_city][fes_city]=dist; return dist;
}
(算法)Travel Information Center的更多相关文章
- Oracle E-Business Suite Release 12.2 Information Center - Manage
Oracle E-Business Suite Maintenance Guide Release 12.2 Part No. E22954-14 PDF: http://docs.oracl ...
- 机器学习sklearn19.0聚类算法——Kmeans算法
一.关于聚类及相似度.距离的知识点 二.k-means算法思想与流程 三.sklearn中对于kmeans算法的参数 四.代码示例以及应用的知识点简介 (1)make_blobs:聚类数据生成器 sk ...
- ISP路由表分发中的AS与BGP
➠更多技术干货请戳:听云博客 摘要 本文面向,初级网络工程师,数据挖掘工程师,涉及EGP(外部网关协议; Exterior Gateway Protocol),IGP(内部网关协议; Interior ...
- Landsat 8 OLI_TIRS 卫星数字产品
产品描述 2013 年2月11日,美国航空航天局(NASA) 成功发射Landsat-8卫星.Landsat-8卫星上携带两个传感器,分别是OLI陆地成像仪(Operation ...
- “你什么意思”之基于RNN的语义槽填充(Pytorch实现)
1. 概况 1.1 任务 口语理解(Spoken Language Understanding, SLU)作为语音识别与自然语言处理之间的一个新兴领域,其目的是为了让计算机从用户的讲话中理解他们的意图 ...
- TCP/IP 详解常用术语
业务需要,最近看TCP/IP 这本书,专业名词太多了,总结一下,给后来着参考,直接使用. 后续会在读书时慢慢添加. ACK:(ACKnowledgment)TCP首部中的确认标志. ARP:地址解析协 ...
- Nginx学习笔记(反向代理&搭建集群)
一.前言 1.1 大型互联网架构演变历程 1.1.1 淘宝技术 淘宝的核心技术(国内乃至国际的 Top,这还是2011年的数据) 拥有全国最大的分布式 Hadoop 集群(云梯,2000左右节点,24 ...
- DHTML---HTML5
1. HTML概述 网页是网站的表现层,各种编程语言(如Java)构成后台的逻辑,我们将后台逻辑做好然后通过页面表达.同时通过网页来与后台进行交互.而Html是我们做网页的基础,由浏览器来解析. 1. ...
- Video for Linux Two API Specification Revision 2.6.32【转】
转自:https://www.linuxtv.org/downloads/legacy/video4linux/API/V4L2_API/spec-single/v4l2.html Video for ...
随机推荐
- 【Go命令教程】命令汇总
[Go命令教程]1. 标准命令详解 [Go命令教程]2. go build [Go命令教程]3. go install [Go命令教程]4. go get [Go命令教程]5. go clean [G ...
- WSAEventSelect模型编程 详解
转自:http://blog.csdn.net/wangjieest/article/details/7042108 WSAEventSelect模型编程 WSAEventSelect模型编程这个模型 ...
- 在ASP.NET Web API中使用OData的Containment
通常情况下,一个OData的EDM(Entity Data Model)在配置的时候定义了,才可以被查询或执行各种操作.比如如下: builder.EntitySet<SomeModel> ...
- 使用FTP发布和更新Windows Azure网站
在Windows Azure中,FTP的用户名和密码与管理门户的用户名和密码不一样,需要另外设置. →依次点击左侧的"网站",网站名称,右侧的"设置部署凭据", ...
- utf-8与utf-8(无BOM)的区别
BOM——Byte Order Mark,就是字节序标记 在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF.而FFFE ...
- ifeq endif
ifeq ($(PLATFORM_VERSION),4.4)$(info "________________________4.4"); LOCAL_CFLAGS += -DPLU ...
- SurfaceFlinger( 226): Permission Denial: can't access SurfaceFlinger
MODIFY_PHONE_STATE permission is granted to system apps only. For your information, there are 2 type ...
- SSD阵列卡方案优化:考虑使用RAID 50替代RAID 10
最近一直在研究RAID 50,因为牺牲一半的容量的RAID 10代价实在太大了,而且它提供的可用性也并不是百分百的,我们首先来看下RAID 10的可用性分析: 以同等容量的不同RAID方式作为案例分析 ...
- Git每次进入都需要输入用户名和密码的问题解决
解决方法: 在项目目录下输入以下命令: git config --global credential.helper store 使用git pull 的时候回提示再输下用户名和密码就行了.
- 实习医生风云第一至九季/全集Scrubs迅雷下载
本季看点:<实习医生风云>一批医学院的学生来到圣心医院开始他们的实习生涯,但是从第一天起就发现这里并不是想象中安详宁静的医学圣地,从医生到护士甚至门卫个个不同寻常.内科实习医生杰迪是个聪明 ...