Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces 602C】H - Approximating a Constant Range(dijk)

【CodeForces 602C】H - Approximating a Constant Range(dijk)

作者头像
饶文津
发布于 2020-05-31 15:31:22
发布于 2020-05-31 15:31:22
49600
代码可运行
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏
运行总次数:0
代码可运行

Description

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 yif 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 ≤ 400, 0 ≤ 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 ≤ n, u ≠ 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.

Sample Input

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

Hint

In the first sample, the train can take the route

and the bus can take the route

. Note that they can arrive at town4 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.

因为任意一对城市之间都有一条直通的路,要么是铁路要么是公路,因此1到n城市一定有铁路或公路,是铁路,就再去找公路的最短路,否则就找铁路的最短路。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<stdio.h>

const int maxn=0x7fff;
long long n,m,s,e,t[405][405],u[405][405],dist[405];
void dijk(int v0,long long r[][405])
{
    bool b[405];
    for(int i=1; i<=n; i++)
    {
        dist[i]=r[v0][i];
        b[i]=false;
    }
    dist[v0] = 0;
    b[v0] = true;
    for(int i=2; i<=n; i++)
    {
        long long mindis=maxn;
        int u = v0;
        for(int j=1; j<=n; j++)
            if((!b[j]) && dist[j]<mindis)
            {
                u = j;
                mindis = dist[j];
            }
        b[u]=true;
        for(int j=1; j<=n; j++)
            if((!b[j]) && r[u][j]<maxn)
                if(dist[u] + r[u][j] < dist[j])
                    dist[j] = dist[u] + r[u][j];
    }
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
        {
            t[i][j]=maxn;
            u[i][j]=1;
        }
    for(int i=0; i<m; i++)
    {
        scanf("%lld%lld",&s,&e);
        t[s][e]=t[e][s]=1;
        u[s][e]=u[e][s]=maxn;
    }
    if(u[1][n]==1)//road直达,铁路的最短路
        dijk(1,t);
    else
        dijk(1,u);
    if(dist[n]>=maxn)
        printf("-1\n");
    else
        printf("%lld\n",dist[n]);

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-12-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
最短路专题1 | CodeForces 601A - 混合Dijkstra算法
这个十一没有出去玩,花了一些时间在写之前提过的 markdown 编辑器,本文就是用这个编辑器写的 2333,今天准备写咱们的新专题 — 最短路。另外之前提过专题的题目主要使用 kuangbin 系列,现在改变主意了,专题题目全部使用 CodeForces 上的题目,原因主要是 POJ 等国内的 OJ 系统不能看源代码,而且题目质量稍微欠缺一些,然后没有区分度。
ACM算法日常
2019/10/09
5110
最短路专题1 | CodeForces 601A - 混合Dijkstra算法
【CodeForces 567E】President and Roads(最短路)
Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer.
饶文津
2020/06/02
7190
最短路专题2 | CodeForces 449B - SPFA算法
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city to (and vise versa) using the -th road, the length of this road is . Finally, there are k train routes in the country. One can use the -th train route to go from capital of the country to city (and vise versa), the length of this route is . J是A国的总统,这个国家有n个城市。1是首都,有m条公路连接这些城市。然后,有k个火车线。城市到首都1的距离是。
ACM算法日常
2019/10/21
7140
图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station. No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station. Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.
风骨散人Chiam
2020/10/28
3730
codeforces round #257 div2 C、D「建议收藏」
由于横切+竖切(或竖切+横切)会对分割的东西产生交叉份数。从而最小的部分不会尽可能的大。
全栈程序员站长
2022/07/10
2200
HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)
Destroying the bus stations                                                                                     Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                                            
Gxjun
2018/03/22
5460
数据结构实验C语言实现版
数据结构实验——顺序表的基本操作 /*-----顺序表的基本操作-----*/ #include<stdio.h> #include<stdlib.h> #define maxsize 1024 typedef char elemtype; typedef struct { elemtype list[maxsize]; int length; }sequenlist; void creatlist(sequenlist *L) { int n,i; char tmp; printf("请输入
荣仔_最靓的仔
2021/02/02
1K0
K - Highway Project  ZOJ - 3946 【 SPFA 求最小时间下最小距离】
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.
Lokinli
2023/03/09
2510
图的简单应用(C/C++实现)
存档: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define maxv 10//定义最大顶点数 4 typedef char elem;//图中顶点的数据类型 5 #include "graph.h" 6 void main() 7 { 8 elem v0; 9 int v; 10 mgraph g; 11 printf("1.初始化函数测试:\n"); 12 initial(g); 13
Angel_Kitty
2018/04/09
7300
图的简单应用(C/C++实现)
Highways「建议收藏」
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can’t reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.
全栈程序员站长
2022/08/10
4160
CF思维联系– Codeforces-987C - Three displays ( 动态规划)
ACM思维题训练集合 It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
风骨散人Chiam
2020/11/03
4350
CF思维联系– Codeforces-987C - Three displays ( 动态规划)
Codeforces Round #277.5 (Div. 2)-D「建议收藏」
题意:求该死的菱形数目。直接枚举两端的点。平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2.
全栈程序员站长
2022/07/08
3040
Codeforces Round #277.5 (Div. 2)-D「建议收藏」
HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2489    Accepted Submission(s): 759 Problem Description When the magic ball game turns up, Kimi immediately falls in it. The inter
ShenduCC
2018/04/27
6770
【CodeForces 602B】G - 一般水的题2-Approximating a Constant Range
Description When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of cons
饶文津
2020/05/31
5470
最短路专题(最全面详细解决最短路问题的一篇博文)
2.首先我们知道在给这个mapt初始化时在主对角线上的元素,及 i == j ,表示的是从自身到自身,那么我们自然要给其初始化为0, 其余的我们统统看成没有路,即为INF。
杨鹏伟
2020/09/11
4740
2019 ICPC 南京网络赛 H-Holy Grail
As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant with a powerful Noble Phantasm.When your servant launch her Noble Phantasm,it will construct a magic field,which is actually a directed graph consisting of n vertices and m edges.More specifically,the graph satisfies the following restrictions :
风骨散人Chiam
2020/10/28
5110
The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 XKC's basketball team
XKC , the captain of the basketball team , is directing a train of nn team members. He makes all members stand in a row , and numbers them 1 \cdots n1⋯n from left to right.
风骨散人Chiam
2020/10/28
3680
洛谷P1339 [USACO09OCT]热浪Heat Wave(最短路)
题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for good eating but are not so adept at creating creamy delicious dairy products. Farmer John is leading the charge to deliver plenty of ice cold nutritious milk
attack
2018/04/10
7910
Tricks Device (hdu 5294 最短路+最大流)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 124 Accepted Submission(s): 27
全栈程序员站长
2022/07/10
2330
J - 【黄色】这题真的是模板题 Gym - 102072J 【 SPFA 】
J - 【黄色】这题真的是模板题 Gym - 102072J  在看完其他出题人出的毒瘤题之后,良心出题人终于看不下去了,决定出一道模板题来送给大家一个AC,那么,你们能不能接住这个送来的AC呢? 给出一个nn个结点mm条边的带权有向图,若图中存在负权回路直接输出"-1",否则输出点ss到每个点的最短路的长度,如果ss点不连通,输出"NoPath"。 Input 第一行输入三个值n,m,s(2≤n≤1000,m≤105,1≤s≤N)n,m,s(2≤n≤1000,m≤105,1≤s≤N) 接下来m
Lokinli
2023/03/09
1940
推荐阅读
相关推荐
最短路专题1 | CodeForces 601A - 混合Dijkstra算法
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档