CF#333(Div2) C. The Two Routes(最短路)

本文探讨了一个有趣的路径规划问题,涉及两个交通工具(火车与巴士)从起点到终点的旅行,且需避免途中相遇。通过构建两种类型的路线网络(铁路和公路),文章提出了一种高效的解决方案来寻找最短到达时间。

题目点我点我点我


C. The Two Routes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.

A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.

You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.

Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 4000 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively.

Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ nu ≠ v).

You may assume that there is at most one railway connecting any two towns.

Output

Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output  - 1.

Examples
input
4 2
1 3
3 4
output
2
input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output
-1
input
5 5
4 2
3 5
4 5
5 1
1 2
output
3
Note

In the first sample, the train can take the route  and the bus can take the route . Note that they can arrive at town 4 at the same time.

In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.




题目大意:给你n个点,m边的地图,这m边是u到v之间有铁路,然后,没有给出的任意2点之间是马路。问一个坐火车,一个坐巴士,两者不能在同一座城市相遇,从1点出发,最后2人都到达n点的时间。


解题思路:两者总会有一个能从1直接到n点的,故两者不能在同一座城市相遇这一条件可以无视,在另一个不能直接到n点的图上跑最短路。


/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")

int Map1[405][405],Map2[405][405];
int n,m;
int vis[405];

void dijkstra(int Map[405][405])
{
    int res[405];
    for(int i=1;i<=n;i++)
    {
        res[i] = Map[1][i];
        vis[i] = 0;
    }
    for(int i=1;i<=n;i++)
    {
        int mi = inf_int,v;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && res[j]<mi)
            {
                mi = res[j];
                v = j;
            }
        }
        vis[v] = 1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && res[j]>res[v]+Map[v][j])
            {
                res[j] = res[v] + Map[v][j];
            }
        }
    }
    if(res[n]==inf_int)printf("-1\n");
    else printf("%d\n",res[n]);
}


int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	n = read(), m = read();
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)Map1[i][j] = Map2[i][j] = 0;
            else Map1[i][j] = Map2[i][j] = inf_int;
        }
    }
    int flag = 0;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        a = read(), b = read();
        if(abs(a-b)==n-1)flag = 1;
        Map2[a][b] = Map2[b][a] = 1;
    }
	for (int i = 1 ; i <= n ; i ++ )
    {
        for (int j = 1 ; j <= n ; j++ )
        {
            if(Map2[i][j] == inf_int)
            {
                Map1[i][j] = Map1[j][i] = 1;
            }
        }
    }
	if(n*(n-1)/2==m)printf("-1\n");
	else if(flag)dijkstra(Map1);
	else dijkstra(Map2);
	return 0;
}





<think>嗯,用户问的是关于无法解析配置属性'spring.cloud.gateway.routes'的问题。首先,我需要确定这个问题的常见原因。通常,Spring Boot应用在启动时如果报这个错误,可能是因为配置属性没有正确加载或者拼写错误。 首先,我应该检查配置文件的格式是否正确。用户可能是在application.yml或者application.properties里配置了路由,但格式可能有误。比如在YAML文件中,缩进不正确会导致属性无法被识别。此外,属性名的拼写是否正确,比如中间的空格或者点号是否多余,比如'spring. cloud. gateway. routes'中的空格可能是个问题,正确的应该是'spring.cloud.gateway.routes'。 然后,考虑依赖是否缺失。Spring Cloud Gateway需要在项目中添加对应的starter依赖,比如spring-cloud-starter-gateway。如果用户没有正确引入这个依赖,相关的配置属性自然无法识别。这时候需要检查pom.xml或者build.gradle文件,确认依赖是否存在,版本是否兼容。 另外,Spring Boot和Spring Cloud的版本兼容性也可能导致这个问题。不同版本的Spring Cloud可能配置方式有所不同,比如旧版本和新版本的配置结构可能有差异。用户可能需要查看对应版本的官方文档,确认配置属性是否正确。 还有,配置的位置是否正确。有时候用户可能将路由配置放在了bootstrap.yml中,而应用没有启用bootstrap上下文,导致配置未被加载。或者配置被放在了错误的profile中,导致未激活。 还有可能,用户没有使用正确的Java类来启用网关功能。比如,主应用类上可能需要@EnableDiscoveryClient或者其他相关注解,或者配置类中需要定义RouteLocator bean。但如果是通过配置文件定义路由,可能不需要额外的注解,但需要确保应用能够扫描到配置。 另外,检查是否有多个配置文件冲突,比如同时存在application.yml和application.properties,可能导致配置覆盖或冲突。或者配置属性被环境变量覆盖,导致无法正确读取。 后,建议用户检查日志中的详细错误信息,通常Spring Boot会提示更具体的错误原因,比如未知的配置属性,或者哪个具体的属性无法绑定。这有助于更快定位问题。 总结下来,可能的解决步骤包括:检查拼写和格式,确认依赖,检查版本兼容性,确认配置位置,查看日志详情。用户可以根据这些方向逐一排查。</think>当你在使用 **Spring Cloud Gateway** 时遇到 `Cannot resolve configuration property 'spring.cloud.gateway.routes'` 错误,通常是因为以下原因之一: --- ### **1. 配置格式错误** Spring Cloud Gateway 的路由配置需要遵循特定格式,常见的错误包括: - **缩进问题**(YAML 文件对缩进敏感)。 - **缺少必要的字段**(如 `id`, `uri`, `predicates` 等)。 - **属性名拼写错误**(如 `predicates` 写成 `predicate`)。 #### 正确示例(YAML): ```yaml spring: cloud: gateway: routes: - id: my_route uri: http://example.org predicates: - Path=/api/** ``` #### 正确示例(Properties): ```properties spring.cloud.gateway.routes[0].id=my_route spring.cloud.gateway.routes[0].uri=http://example.org spring.cloud.gateway.routes[0].predicates[0]=Path=/api/** ``` --- ### **2. 依赖缺失** 确保项目中包含 **Spring Cloud Gateway 的依赖**(如 Maven 或 Gradle 配置未正确引入)。 #### Maven 依赖示例: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> ``` --- ### **3. Spring Boot/Cloud 版本不兼容** 不同版本的 Spring Cloud Gateway 可能有配置差异,例如: - **旧版本**(如 Hoxton)可能需要手动启用网关功能。 - **新版本**(如 2021.x+)可能调整了配置属性路径。 检查你的 `spring-boot-starter-parent` 和 `spring-cloud-dependencies` 版本是否兼容。 --- ### **4. 配置未被加载** - 确保路由配置位于 `application.yml` 或 `bootstrap.yml` 中。 - 如果使用 `@ConfigurationProperties`,需确认是否启用了配置扫描(如主类添加 `@EnableConfigurationProperties`)。 --- ### **5. IDE 误报** 某些 IDE(如 IntelliJ)可能误报配置错误,但实际应用可以正常运行。尝试以下操作: - 清理并重新构建项目。 - 重启 IDE。 - 运行应用,查看是否真的启动失败(IDE 静态检查可能有误)。 --- ### **排查步骤** 1. **检查依赖**:确认 `spring-cloud-starter-gateway` 已正确引入。 2. **验证配置格式**:使用在线 YAML 校验工具(如 [YAML Lint](https://yamllint.com/))。 3. **查看日志**:启动应用时,Spring Boot 会详细打印加载的配置属性。 4. **简化配置**:先配置一个简单的路由,逐步排除问题。 如果问题仍未解决,请提供完整的配置文件和依赖树,以便进一步分析!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值