A997 | Connecting Two Barns--Silver
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Farmer John's farm consists of a set of $N$ fields $(1 \leq N \leq 10^5)$,
conveniently numbered $1 \ldots N$. Between these fields are $M$ bi-directed
paths $(0 \leq M \leq 10^5)$, each connecting a pair of fields.
The farm contains two barns, one in field 1 and the other in field $N$. Farmer
John would like to ensure that there is a way to walk between the two barns
along some series of paths. He is willing to build up to two new paths to
accomplish this goal. Due to the way the fields are situated, the cost of
building a new path between fields $i$ and $j$ is $(i-j)^2$.
Please help Farmer John determine the minimum cost needed such that barns $1$
and $N$ become reachable from each-other.
conveniently numbered $1 \ldots N$. Between these fields are $M$ bi-directed
paths $(0 \leq M \leq 10^5)$, each connecting a pair of fields.
The farm contains two barns, one in field 1 and the other in field $N$. Farmer
John would like to ensure that there is a way to walk between the two barns
along some series of paths. He is willing to build up to two new paths to
accomplish this goal. Due to the way the fields are situated, the cost of
building a new path between fields $i$ and $j$ is $(i-j)^2$.
Please help Farmer John determine the minimum cost needed such that barns $1$
and $N$ become reachable from each-other.
输入格式
Each input test case contains $T$ sub-cases ($1\le T\le 20$), all of which
must be solved correctly to solve the input case.
The first line of input contains $T$, after which $T$ sub-test cases follow.
Each sub-test case starts with two integers, $N$ and $M$. Next, $M$ lines
follow, each one containing two integers $i$ and $j$, indicating a path
between two different fields $i$ and $j$. It is guaranteed that there is at
most one path between any two fields, and that the sum of $N+M$ over all sub-
test cases is at most $5 \cdot 10^5$.
must be solved correctly to solve the input case.
The first line of input contains $T$, after which $T$ sub-test cases follow.
Each sub-test case starts with two integers, $N$ and $M$. Next, $M$ lines
follow, each one containing two integers $i$ and $j$, indicating a path
between two different fields $i$ and $j$. It is guaranteed that there is at
most one path between any two fields, and that the sum of $N+M$ over all sub-
test cases is at most $5 \cdot 10^5$.
输出格式
Output $T$ lines. The $i$th line should contain a single integer giving the
minimum cost for the $i$th sub-test case.
minimum cost for the $i$th sub-test case.
输入输出样例
输入 #1
2 5 2 1 2 4 5 5 3 1 2 2 3 4 5
输出 #1
2 1
In the first sub-test case, it is optimal to connect fields 2 and 3 with a
path, and fields 3 and 4 with a path.
In the second sub-test case, it is optimal to connect fields 3 and 4 with a
path. No second path is needed.
path, and fields 3 and 4 with a path.
In the second sub-test case, it is optimal to connect fields 3 and 4 with a
path. No second path is needed.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted