아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Railroad Management

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

요약
각 역이 정확히 C_i량의 화차를 역 D_i로 보낼 때, 어떤 순서로든 모든 배송이 가능하도록 하는 최소 초기 화차 총량을 구한다.
난이도

보통10점 중 7점

유형
그래프, 그리디, DFS, 정렬
정답자
아직 제출이 없습니다

문제

You are in charge of the management of a railroad network. The network consists of N\mathbf{N} stations. Each station ii needs to ship goods to exactly one other station D_i\mathbf{D\_i}. Station ii will send exactly one shipment, in a train with exactly C_i\mathbf{C\_i} railroad cars.

You get all the shipment information well in advance, so you plan on saving on railroad cars by reusing them. If station ii sends nn railroad cars to station D_i\mathbf{D\_i}, then D_i\mathbf{D\_i} can add those railroad cars to its supply to use for its own shipment if it did not already happen.

Formally, you must give an initial supply of railroad cars to each station (some stations may get 00) and provide an order for the shipments so that, by the time station ii must ship, the number of railroad cars between its initial supply and any previous shipments that arrived at ii must be at least the number it needs for its own shipment C_i\mathbf{C\_i}. You cannot send more than C_i\mathbf{C\_i} cars in a shipment out of station ii, even if the station has more than C_i\mathbf{C\_i} available.

For example, suppose that station 11 sends a train carrying exactly 33 railroad cars to station 44. Now, if station 44 needs 22 cars, it could reuse 22 of the cars it received from station 11. And if station 44 needs to send 55 cars, it can reuse all 33 cars received from station 11 and add 22 of its own supply. Note that when station 44 needs to send 22 cars, it cannot send all 33 it received from station 11.

Given the shipment information, what is the minimum number of railroad cars you need to distribute for the stations' initial supplies, such that you can do all shipments in some order?

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case consists of 33 lines. The first line contains a single integer N\mathbf{N}, the number of stations in the network. The second line contains N\mathbf{N} integers D_1,D_2,…,D_N\mathbf{D\_1}, \mathbf{D\_2}, \dots, \mathbf{D\_N} and the third and last line contains N\mathbf{N} integers C_1,C_2,…,C_N\mathbf{C\_1}, \mathbf{C\_2}, \dots, \mathbf{C\_N}. These represent that station ii must send a train of exactly C_i\mathbf{C\_i} railroad cars to station D_i\mathbf{D\_i}.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum number of railroad cars you need to distribute among the stations so that all shipments can be performed.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 1≤D_i≤N1 \le \mathbf{D\_i} \le \mathbf{N}, for all ii.
  • D_i≠i\mathbf{D\_i} \ne i, for all ii.
  • 1≤C_i≤1091 \le \mathbf{C\_i} \le 10^9, for all ii.

힌트

In Sample Case #1 one optimal way is to do the shipments in increasing order of departure station. That requires sending 44 cars to station 11. But after that, each station receives enough cars for its shipment, for a total of 44 overall. Since no cars arrive at station 11, it definitely needs the initial 44, so this is also the minimum possible.

In Sample Case #2 one minimal way is to supply 11 car to station 33 and 22 cars each to stations and 22 and 44, for a total of 55. Then, we can start with the shipment 3→43 \to 4 which gets one additional car to station 44. This makes station 44 have the 33 cars it needs to ship 4→14 \to 1. Station 11 now has 33 cars which is enough to do 1→21 \to 2 with a single car, taking the total at station 22 to 33 cars, enough to do the final shipment 2→32 \to 3. Notice that the shipment 1→21 \to 2 cannot bring extra cars to station 22, even though there are cars available and it would be helpful to do so. There are other ways to do all shipments with 55 initial cars, but no way to do it with less.

In Sample Case #3, one optimal starting number of cars is 33 cars at stations 11 and 44 and 22 cars at stations 55 and 77.

예제1

  1. 예제 1

    입력
    3
    4
    2 3 4 3
    4 3 2 1
    4
    2 3 4 1
    1 3 1 3
    7
    3 5 2 5 3 7 6
    3 4 6 3 5 1 2
    
    예상 출력
    Case #1: 4
    Case #2: 5
    Case #3: 10