题目:

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的更多相关文章

  1. 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 ...

  2. 机器学习sklearn19.0聚类算法——Kmeans算法

    一.关于聚类及相似度.距离的知识点 二.k-means算法思想与流程 三.sklearn中对于kmeans算法的参数 四.代码示例以及应用的知识点简介 (1)make_blobs:聚类数据生成器 sk ...

  3. ISP路由表分发中的AS与BGP

    ➠更多技术干货请戳:听云博客 摘要 本文面向,初级网络工程师,数据挖掘工程师,涉及EGP(外部网关协议; Exterior Gateway Protocol),IGP(内部网关协议; Interior ...

  4. Landsat 8 OLI_TIRS 卫星数字产品

      产品描述           2013 年2月11日,美国航空航天局(NASA) 成功发射Landsat-8卫星.Landsat-8卫星上携带两个传感器,分别是OLI陆地成像仪(Operation ...

  5. “你什么意思”之基于RNN的语义槽填充(Pytorch实现)

    1. 概况 1.1 任务 口语理解(Spoken Language Understanding, SLU)作为语音识别与自然语言处理之间的一个新兴领域,其目的是为了让计算机从用户的讲话中理解他们的意图 ...

  6. TCP/IP 详解常用术语

    业务需要,最近看TCP/IP 这本书,专业名词太多了,总结一下,给后来着参考,直接使用. 后续会在读书时慢慢添加. ACK:(ACKnowledgment)TCP首部中的确认标志. ARP:地址解析协 ...

  7. Nginx学习笔记(反向代理&搭建集群)

    一.前言 1.1 大型互联网架构演变历程 1.1.1 淘宝技术 淘宝的核心技术(国内乃至国际的 Top,这还是2011年的数据) 拥有全国最大的分布式 Hadoop 集群(云梯,2000左右节点,24 ...

  8. DHTML---HTML5

    1. HTML概述 网页是网站的表现层,各种编程语言(如Java)构成后台的逻辑,我们将后台逻辑做好然后通过页面表达.同时通过网页来与后台进行交互.而Html是我们做网页的基础,由浏览器来解析. 1. ...

  9. 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 ...

随机推荐

  1. 《Go学习笔记 . 雨痕》流程控制(if、switch、for range、goto、continue、break)

    Go 精简(合并)了流控制语句,虽然某些时候不够便捷,但够用. if...else... 条件表达式值必须是布尔类型,可省略括号,且左花括号不能另起一行. func main() { x := 3 i ...

  2. win10企业版永久激活2017怎么用

    Win10正式版永久激活用命令和密钥即可工具原料:电脑+win10win10企业版永久激活方法如下:1."WIN+R"打开运行对话框,输入命令slmgr.vbs -xpr,点击确定 ...

  3. Creating Reusable XAML User Controls with Xamarin Forms

    In the previous post on making fancy layouts with Xamarin Forms we saw how you can design a Dashboar ...

  4. hdu 4865 Peter&#39;s Hobby

    Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. 使用Coding4Fun工具包

    Coding4Fun是一款很受WP开发者喜爱的开源类库,对于开发者来说,Coding4Fun上手很简单.只要从CodePlex下载Coding4Fun工具包,下载完成后,解压文件到一个文件夹中,里面有 ...

  6. Linux isql

    登录Linux环境,执行 isql -s 命令,进入isql命令页面 进入后页面如下: 选择Query-Language回车,选项可进行数据库操作 进入后,选择要操作的数据库,回车 进行入数据库后的数 ...

  7. IntelliJ IDEA2018.1、2017.3激活

    IntelliJ IDEA2018.1.2017.3破解教程 http://idea.java.sx/ 简单快捷!! ————————————————————————————————————————  ...

  8. 自定义各式各样的圆形ProgressBar

         上面三个图分别是 开始时的样子,走进度时候的样子,最后完成的样子 这是我在两个大神的Demo基础上修改后的结果,我们先来看看自定义view是怎么做到的. 1.自己写一个类继承View类,然后 ...

  9. 基于Vue、Bootstrap的Tab形式的进度展示

    最近基于Vue.Bootstrap做了一个箭头样式的进度展示的单页应用,并且支持了对于一个本地JS文件的检索,通过这个单页应用,对于Vue的理解又深入了一些.在这里把主要的代码分享出来. 本单页应用实 ...

  10. Ubuntu 查找文件的方法

    1. whereis+文件名 用于程序名的搜索,搜索结果只限于二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s),如果省略参数,则返回所有信息. 2. find / -name ...