codeforce-601A. The Two Routes(最短路)
题意:
给你N个点表示N个站,有汽车和火车,汽车只能走公路,火车只能走铁路。
然后给你M条双向路,代表这两个点之间有铁路连接。
然后告诉你如果两个点之间没有铁路,那么就是公路连接。
问你汽车和火车都到达目的地所要的最小时间是多少(两种交通工具不能同时到达同一个城市除了目的地)。
如果有一种交通工具不能到达就输出-1;
思路:
如果两两点之间都是铁路,那么就没有公路,那么汽车不能到达,这时-1;
如果不是这种情况那么要么有铁路连接起点终点,要么有公路,所以两者间必有一个为1,
所以求另一个的最短路就行(肯定不会相撞)。(求两次最短路也行)。
复杂度N2;
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<vector>
6 #include<queue>
7 #include<cstdio>
8 #include<string.h>
9 void dj(int n);
10 const int V=99999999;
11 typedef struct pp
12 {
13 int x;
14 int y;
15 } ss;
16 using namespace std;
17 int flag[500];
18 int jj[500][500];
19 int d[500];
20 int main(void)
21 {
22 int n,i,j,k,p,q,N,M;
23 while(scanf("%d %d",&N,&M)!=EOF)
24 {
25 memset(flag,0,sizeof(flag));
26 for(i=0; i<450; i++)
27 {
28 for(j=0; j<450; j++)
29 {
30 jj[i][j]=V;
31 }
32 }
33 for(i=0; i<M; i++)
34 {
35 scanf("%d %d",&p,&q);
36 jj[p][q]=1;
37 jj[q][p]=1;
38 }
39 dj(N);
40 int ss=d[N];
41 for(i=0; i<450; i++)//求汽车的路径
42 {
43 for(j=0; j<450; j++)
44 {
45 if(jj[i][j]==V)
46 {
47 jj[i][j]=1;
48 }
49 else jj[i][j]=V;
50 }
51 }
52 dj(N);
53 int vv=d[N];
54 int uu=max(vv,ss);
55 if(uu>=V)
56 {
57 printf("-1\n");
58 }
59 else printf("%d\n",uu);
60
61 }
62 return 0;
63 }
64
65 void dj(int n)//最短路N2算法
66 {
67 fill(d,d+n+1,V);
68 fill(flag,flag+n+1,0);
69 d[1]=0;
70 int i,j,k,p,q;
71 while(true)
72 {
73 int l=-1;
74 for(i=1; i<=n; i++)
75 {
76 if(flag[i]==0&&(l==-1||d[i]<d[l]))
77 {
78 l=i;
79 }
80 }
81 if(l==-1)
82 {
83 break;
84 }
85 flag[l]=1;
86 for(i=1; i<=n; i++)
87 {
88 d[i]=min(d[i],d[l]+jj[l][i]);
89 }
90 }
91
92 }
codeforce-601A. The Two Routes(最短路)的更多相关文章
- [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路
14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...
- codeforces 601A The Two Routes(最短路 flody)
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ACM学习历程—CodeForces 601A The Two Routes(最短路)
题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...
- CodeForces - 601A The Two Routes
http://codeforces.com/problemset/problem/601/A 这道题没想过来, 有点脑筋急转弯的感觉了 本质上就是找最短路径 但是卡在不能重复走同一个点 ----> ...
- The Two Routes CodeForces - 601A(水最短路)
一个完全图 1和n肯定有一条路 不是公路就是铁路 另= 另一个跑遍最短路即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, ...
- CodeForces 602C The Two Routes(最短路)
Description In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. T ...
- Codeforces 601A:The Two Routes 宽搜最短路径
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforce Gym 100570B ShortestPath Query (最短路SPFA)
题意:询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同,无重边且边权为正. 题解:因为路径的合法性和边的颜色有关, 所以在做spfa的时候,把边丢到队列中去,松弛的时候注意判断一下颜 ...
- [USACO14OPEN] Dueling GPS's[最短路建模]
题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...
随机推荐
- 在VS2008环境下编写C语言DLL,并在C++和C#项目下调用 (转载)
1.编写DLL a)文件--打开--新建项目--Win32,右侧Win32项目,填写好项目名称,点击"下一步", 应用程序类型选择:"DLL(D)",附加选项: ...
- 云原生PaaS平台通过插件整合SkyWalking,实现APM即插即用
一. 简介 SkyWalking 是一个开源可观察性平台,用于收集.分析.聚合和可视化来自服务和云原生基础设施的数据.支持分布式追踪.性能指标分析.应用和服务依赖分析等:它是一种现代 APM,专为云原 ...
- 13 — springboot集成mybatis-plus — 更新完毕
1.mybatis-plus需要掌握的知识 1).mybatis-plus是什么? 不写了,老衲一般都是直接进官网 mybatis-plus官网地址:https://baomidou.com/guid ...
- Vue相关,Vue生命周期及对应的行为
先来一张经典图 生命钩子函数 使用vue的朋友们知道,生命周期函数长这样- mounted: function() { } // 或者 mounted() { } 注意点,Vue的所有生命周期函数都是 ...
- [Windows编程]模块遍历
模块遍历 整体思路 1.创建进程快照 2.遍历首次模块 3.继续下次遍历 4.模块信息结构体 相关API的调用 创建进程快照API HANDLE WINAPI CreateToolhelp32Snap ...
- Linux基础命令---wget下载工具
wget wget是一个免费的文件下载工具,可以从指定的URL下载文件到本地主机.它支持HTTP和FTP协议,经常用来抓取大量的网页文件. 此命令的适用范围:RedHat.RHEL.Ubuntu.Ce ...
- Hibernate持久化标志符生成策略
generator子元素定义持久化标识符的生成策略,为持久化类对应的数据库表的主键找到了赋值方法,HIbernate默认将使用assigned的持久化标识符生成策略.关系型数据库的主键定义方式:(1) ...
- zabbix实现对主机和Tomcat监控
#:在tomcat服务器安装agent root@ubuntu:~# apt install zabbix-agent #:修改配置文件 root@ubuntu:~# vim /etc/zabbix/ ...
- my43_mysql内存相关概念
相关参数 read_buffer_size https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_re ...
- Spring Boot中使用Servlet与Filter
在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...