A7804 | Quarrel
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Friends Alex and Bob live in Bertown. In this town there are $n$ crossroads, some of them are connected by bidirectional roads of equal length. Bob lives in a house at the crossroads number $1$ , Alex — in a house at the crossroads number $n$ .
One day Alex and Bob had a big quarrel, and they refused to see each other. It occurred that today Bob needs to get from his house to the crossroads $n$ and Alex needs to get from his house to the crossroads $1$ . And they don't want to meet at any of the crossroads, but they can meet in the middle of the street, when passing it in opposite directions. Alex and Bob asked you, as their mutual friend, to help them with this difficult task.
Find for Alex and Bob such routes with equal number of streets that the guys can follow these routes and never appear at the same crossroads at the same time. They are allowed to meet in the middle of the street when moving toward each other (see Sample 1). Among all possible routes, select such that the number of streets in it is the least possible. Until both guys reach their destinations, none of them can stay without moving.
The guys are moving simultaneously with equal speeds, i.e. it is possible that when one of them reaches some of the crossroads, the other one leaves it. For example, Alex can move from crossroad $1$ to crossroad $2$ , while Bob moves from crossroad $2$ to crossroad $3$ .
If the required routes don't exist, your program should output -1.
One day Alex and Bob had a big quarrel, and they refused to see each other. It occurred that today Bob needs to get from his house to the crossroads $n$ and Alex needs to get from his house to the crossroads $1$ . And they don't want to meet at any of the crossroads, but they can meet in the middle of the street, when passing it in opposite directions. Alex and Bob asked you, as their mutual friend, to help them with this difficult task.
Find for Alex and Bob such routes with equal number of streets that the guys can follow these routes and never appear at the same crossroads at the same time. They are allowed to meet in the middle of the street when moving toward each other (see Sample 1). Among all possible routes, select such that the number of streets in it is the least possible. Until both guys reach their destinations, none of them can stay without moving.
The guys are moving simultaneously with equal speeds, i.e. it is possible that when one of them reaches some of the crossroads, the other one leaves it. For example, Alex can move from crossroad $1$ to crossroad $2$ , while Bob moves from crossroad $2$ to crossroad $3$ .
If the required routes don't exist, your program should output -1.
输入格式
The first line contains two integers $n$ and $m$ ( $2<=n<=500,1<=m<=10000$ ) — the amount of crossroads and the amount of roads. Each of the following $m$ lines contains two integers — the numbers of crossroads connected by the road. It is guaranteed that no road connects a crossroads with itself and no two crossroads are connected by more than one road.
输出格式
If the required routes don't exist, output -1. Otherwise, the first line should contain integer $k$ — the length of shortest routes (the length of the route is the amount of roads in it). The next line should contain $k+1$ integers — Bob's route, i.e. the numbers of $k+1$ crossroads passed by Bob. The last line should contain Alex's route in the same format. If there are several optimal solutions, output any of them.
输入输出样例
输入 #1
2 1 1 2
输出 #1
1 1 2 2 1
输入 #2
7 5 1 2 2 7 7 6 2 3 3 4
输出 #2
-1
输入 #3
7 6 1 2 2 7 7 6 2 3 3 4 1 5
输出 #3
6 1 2 3 4 3 2 7 7 6 7 2 1 5 1
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted