RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0487

Connecting Two Barns

Platinum V 플래티넘 V
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.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.

Problem credits: Nick Wu

제약

SCORING

  • Test case 2 satisfies \(N \le 20\).
  • Test cases 3-5 satisfy \(N \le 10^3\).
  • Test cases 6-10 satisfy no additional constraints.

Problem credits: Nick Wu

입력 형식

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\).

출력 형식

Output \(T\) lines. The \(i\)th line should contain a single integer giving the
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
출력
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.

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2021-2022 > December > Silver

태그

평가 및 의견

Connecting Two Barns

개요
출제자 난이도 Platinum V 플래티넘 V 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Connecting Two Barns

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8