首页 > 其他分享 >Shortest time(最短时间)

Shortest time(最短时间)

时间:2023-04-23 19:33:25浏览次数:33  
标签:city 短时间 int 城市 样例 40 vis time Shortest

这题怎么这么难啊(恼

 

有若干个城市,它们之间有道路连通,可以互相到达,从一个城市到另一个城市时间为1。现在给出起点城市A,终点城市B,和N条道路。
问从A到B最短时间。

input
    第一行A,B,N(A,B,N<=30),B为最大城市标号
    接下来N行,每行两个数x,y,表示城市x和城市y有道路相连。
output
    最短时间
__的题目:

 

 

 

 

样例

样例输入1

 

1 7 9
1 2
1 4
2 3
3 5
4 5
4 6
1 6
6 7
5 7

 

样例输出1

 

2

 

tips:城市1可以通过1——6——7到达城市7,花费最短时间为2.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a,b,n,d[40],vis[40];
 4 bool city[40][40];
 5 queue<int> q;
 6 void bfs()
 7 {
 8     q.push(a);
 9     vis[a]=1;
10     d[a]=0;
11     while(!q.empty())
12     {
13         int x=q.front();
14         q.pop();
15         vis[x]=0;
16         for(int i=1;i<=b;i++)
17         {
18             if(city[x][i]&&!vis[i])
19             {
20                 q.push(i);
21                 vis[i]=1;
22                 d[i]=d[x]+1;
23                 if(i==b)
24                 {
25                     cout << d[i];
26                     exit(0);
27                 }
28             }
29         }
30     }
31 }
32 int main()
33 {
34     cin >> a >> b >> n;
35     for(int i=1;i<=n;i++)
36     {
37         int x,y;
38         cin >> x >> y;
39         city[x][y]=1;
40         city[y][x]=1;
41     }
42     bfs();
43     return 0;
44 }

 

标签:city,短时间,int,城市,样例,40,vis,time,Shortest
From: https://www.cnblogs.com/nasia/p/17347499.html

相关文章

  • nginx日志中出现proxy_pass代理地址timeout
    背景:  开发发现测试环境业务异常,影响新需求上线,排查日志中发现nginx日志中出现proxy_pass代理地址timeout报错,直接使用代理地址curl没问题,重启nginx就好了。由于是开发自建自运维的nginx服务器,且出问题当天运维有对服务器进行审计操作,开发便把这口锅甩给我们运维人员!!!排查问题: ......
  • Hydro Tools:System.Runtime.InteropServices.COMException (0x80004005)
     在使用hydrotools的时候报了这个错误 然后看到一个solution 这个设置一下 rasterworkspace要选择它默认的图层layer不是gdb,只有vectorworkspace才是gdb ......
  • odoo中 py3o的打印报告中,报告的名字如果要取当天的日期或其它日期时,如果要导包,import
    odoo中py3o的打印报告中,报告的名字如果要取当天的日期或其它日期时,如果要导包,import timedate.这种在report的名字中,是请允许使用eval 这个函数(出于安全考虑)可以使用下面的来替代时间'orderrecap%s'%(time.strftime("%Y-%m-%d",time.localtime())) 还有一种方法是......
  • bootstrap日期插件datetimepicker的简单使用
    <!DOCTYPEHTML><html><head><linkhref="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css"rel="stylesheet"><linkrel="stylesheet"type="text/cs......
  • time&datetime&string相互转换
    time&datetime&string相互转换importdatetimeimporttime#日期时间字符串st="2017-11-2316:10:10"#当前日期时间dt=datetime.datetime.now()#当前时间戳sp=time.time()#1.把datetime转成字符串defdatetime_toString(dt):print("1.把datetime转成......
  • Python time 库常用函数
    time模块中时间表现的格式主要有三种:timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量struct_time时间元组,共有九个元素组。formattime格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式。使用time库前先用import导......
  • time.h的使用方法
    time_ttime(time_t*t)函数作用:获取当前时间并用time_t类型的指针t返回参数说明:t为指向time_t类型变量的指针,用于接收时间值返回值:返回当前的时间,以自1970年1月1日0时0分0秒以来的秒数表示示例代码:time_tcurrent_time;time(&current_time);printf("当前时间:%s",ctime(&cur......
  • 装了.Net 7.0后,工程框架用 net6 的 dotnet watch 出错临时解决方案 Could not load fi
    升级vs或者装了.Net7.0后,工程框架用net6的dotnetwatch出错‘Unhandledexception.System.IO.FileNotFoundException:Couldnotloadfileorassembly‘System.Runtime,Version=7.0.0.0’   临时解决方案:工程目录下建立global.json文件指定编译框架{"......
  • Java 计算两个 LocalDateTime 类型的变量之间差的小时数,保留4位小数
    为了计算两个LocalDateTime对象之间相差的小时数,并精确到小数点后4位,您可以使用Duration类。以下是一个示例:importjava.time.LocalDateTime;importjava.time.Duration;publicclassMain{publicstaticvoidmain(String[]args){LocalDateTimea=Loc......
  • MySQL 时间类型 date、datetime 和 timestamp 的用法与区别
    时间范围datetime和timestamp区别时间范围不一样,TIMESTAMP要小很多,且最大范围为2038-01-1903:14:07.999999,到期也不远了。datetime与时区无关、timestamp与时区有关。对于timestamp,它把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储。查询时,将其又转化为客户......