Codeforces Round #333 (Div. 2) C. The Two Routes flyod
C. The Two Routes
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/602/problem/C
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 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 ≤ 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
- 4 2
1 3
3 4
Sample Output
- 2
HINT
题意
给你一个完全图,里面的边不是火车道就是汽车道,然后任意时刻,火车和汽车都不能相遇在除了1,n的其他点
每条边的边权值都是1,然后问你最小时间使得两种车都能到达n点
题解:
因为是完全图,那么总有一种车可以只花费1就能从起点走到终点
然后剩下那个车跑一发最短路就好了~
代码:
- #include<iostream>
- #include<stdio.h>
- #include<queue>
- using namespace std;
- int mp1[][];
- int mp2[][];
- const int inf = 1e7+;
- int main()
- {
- int n,m;
- scanf("%d%d",&n,&m);
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++)
- if(i==j)mp1[i][j]=mp2[i][j]=;
- else mp1[i][j]=mp2[i][j]=inf;
- for(int i=;i<=m;i++)
- {
- int x,y;scanf("%d%d",&x,&y);
- mp1[x][y]=mp1[y][x]=;
- }
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++)
- if(i!=j)
- {
- if(mp1[i][j]==)mp2[i][j]=inf;
- else mp2[i][j]=;
- }
- int flag = mp1[][n];
- if(flag == inf)
- {
- for(int k=;k<=n;k++)
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++)
- mp1[i][j]=min(mp1[i][j],mp1[i][k]+mp1[k][j]);
- if(mp1[][n]==inf)return puts("-1");
- else printf("%d\n",mp1[][n]);
- }
- else
- {
- for(int k=;k<=n;k++)
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++)
- mp2[i][j]=min(mp2[i][j],mp2[i][k]+mp2[k][j]);
- if(mp2[][n]==inf)return puts("-1");
- else printf("%d\n",mp2[][n]);
- }
- }
Codeforces Round #333 (Div. 2) C. The Two Routes flyod的更多相关文章
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分
B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Round #333 (Div. 2) A. Two Bases 水题
A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并
D. Acyclic Organic Compounds You are given a tree T with n vertices (numbered 1 through n) and a l ...
- Codeforces Round #333 (Div. 2)
水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...
- Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈
题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...
- Codeforces Round #333 (Div. 2) B
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
随机推荐
- db2数据库sql报错信息
sqlcode sqlstate 说明 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互关系的引用 +098 ...
- Java Error和Exception区别
Error和Exception都继承自Throwable: 二者不同之处: Exception: 1.可以是可被控制(checked)或者不可控制(unchecked): 2.表示一个由程序员导致的错 ...
- C#发送Email邮件(实例:QQ邮箱和Gmail邮箱)
下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用: using System.Net.Mail; using System.Text; using System.Net; ...
- codeforces 675E Trains and Statistic 线段树+贪心统计
分析:这个题刚看起来无从下手 但是我们可以先简化问题,首先可以固定起点i,求出i+1到n的最小距离 它可以到达的范围是[i+1,a[i]],贪心的想,我们希望换一次车可以到达的距离尽量远 即:找一个k ...
- SQL 语句记录
1.创建一个table @"create table rockTB(myId integer primary key autoincrement not null, time varchar ...
- memcpy、memmove、memset及strcpy函数实现和理解
memcpy.memmove.memset及strcpy函数实现和理解 关于memcpy memcpy是C和C++ 中的内存拷贝函数,在C中所需的头文件是#include<string.h> ...
- pcduino+opencv实现人脸追踪摄像头
Pcduino是一款兼容Arduino接口的mini pc,A8架构1Ghz的CPU,计算能力不俗,用来跑OpenCV刚刚好.这里就用他们实现一个可以跟随人脸移动的摄像头. 硬件清单: 1.Pcdui ...
- 在Lua里写unity游戏笔记
gameobject.GetComponent<Transform>(); 翻译成Lua: gameObject:GetComponent (luanet.ctype (Transform ...
- Aggregation(1):Blending、Bagging、Random Forest
假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ...
- htmlcss笔记--标签默认值样式重置css reset(2)
1.内联,内嵌,行内元素: 默认可以继续跟同类型标签显示: 由内容撑开宽度,内容多宽,会有多宽: span,内嵌标签部支持宽高,即使加了宽高也不支持, 也不支持上下内外边距,但是支持左右内外边距:内上 ...