Raging River

시간 제한2초메모리 제한1024 MB

요약
두 강둑과 통나무 간선으로 이루어진 작은 그래프에서 P명이 순서대로 건너되 지나간 간선은 사라진다고 할 때, 최대한 많은 사람을 건너보내고 총 이동 시간을 최소화한다.
난이도

보통10점 중 7점

유형
그래프, BFS, 그리디, 최단 경로
정답자
아직 제출이 없습니다

문제

Sally and her friends are trying to cross safely from one bank of a raging river to another. Boulders are scattered within the river, with log planks connecting the banks of the river to some of the boulders, and some pairs of boulders to each other.

Sally begins by trying to cross the river first. She starts at the left bank and crosses one plank at a time, with the goal of reaching the right bank. Walking across a plank takes Sally one second. Each time she crosses a plank, that plank becomes unstable and collapses into the river (so that neither Sally nor her friends can use that plank again). After Sally has safely reached the right bank, another friend tries to cross the river, and so on, until everyone who is able to make it has crossed the river.

Given the graph of banks/boulders and planks and the number of people who need the cross the river, what is the smallest amount of total time (in seconds) required for everyone to cross the river safely? If it is impossible for all people to cross, compute the minimum number of people nn who must be left behind and print n people left behind.

입력

The first line of the input contains three integers PP, RR, and LL: the number of people PP who must cross the river, the number of boulders RR in the river, and the number of logs LL spanning boulders/river banks. These integers satisfy 1≤P≤101 \leq P \leq 10 and 0≤R≤1,0000 \leq R \leq 1\\, 000 and 0≤L≤1,0000 \leq L \leq 1\\, 000.

Then follows LL lines, each of which contains two integers E_1E\_1 and E_2E\_2 specifying the endpoints on one log. The values for E_1E\_1 and E_2E\_2 are in the range \[−2,R−1]\[-2,R-1], where −2-2 signifies the left river bank, −1-1 signifies the right river bank, and all other values indicate one of the boulders in the river.

You may assume that every log has two distinct endpoints, and that no two logs span the same pair of boulders/river banks. There is no guarantee that every boulder, or even the right river bank, is reachable from the left river bank.

출력

If it is possible for all PP people to reach the right bank, print a single integer, the minimum total time (in seconds) required for all people to cross.

If some people must be left behind at the left bank, instead print n people left behind, where the integer nn is the least number of people who must be left behind.

예제2

  1. 예제 1

    입력
    2 4 7
    -2 0
    0 -1
    -2 1
    1 0
    2 1
    2 3
    3 -1
    
    예상 출력
    6
    
  2. 예제 2

    입력
    3 2 5
    -2 0
    -2 1
    0 1
    1 -1
    0 -1
    
    예상 출력
    1 people left behind