ROADS - Roads

N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it: the road length and the toll that needs to be paid for the road (expressed in the number of coins). Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash. We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.

Input

The input begins with the number t of test cases. Then t test cases follow. The first line of the each test case contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way. The second line contains the integer N, 2 <= N <= 100, the total number of cities. The third line contains the integer R, 1 <= R <= 10000, the total number of roads. Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters : S is the source city, 1 <= S <= N D is the destination city, 1 <= D <= N L is the road length, 1 <= L <= 100. T is the toll (expressed in the number of coins), 0 <= T <= 100 Notice that different roads may have the same source and destination cities.

Output

For each test case, output a single line contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins. If such path does not exist, output -1.

Example

Input:
2
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
0
4
4
1 4 5 2
1 2 1 0
2 3 1 1
3 4 1 0

Output:
11
-1


Được gửi lên bởi:Jimmy
Ngày:2005-04-28
Thời gian chạy:7s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:Central European Olympiad in Informatics 98

hide comments
2020-08-01 06:24:55
bài ga' vl
2020-02-21 04:40:59 nguyen ngoc son
package bai3;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class main {

private static Scanner scanner;
private static int[][] arrs;
private static int[][] visited;
static int dx[] = {-1,-2,-2,-1,1,2,2,1};
static int dy[] = {-2,-1,1,2,2,1,-1,-2};
private static int kQ;

/*ham doc vao ma tran*/
public static void readMX() {
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs.length; j++) {
arrs[i][j] = scanner.nextInt();
}
}
}
/*ham in ra ma tran*/
public static void printMX() {
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs.length; j++){
System.out.print(arrs[i][j]+" ");
}
System.out.println();
}
}
/*ham dem tai vi tri x,y*/
public static int Count(int x,int y) {
int cnt = 0;
for(int k=0;k<8;k++) {
int tx = x + dx[k];
int ty = y + dy[k];
if(tx>=0 &&tx<arrs.length && ty>=0 &&ty<arrs.length && arrs[tx][ty]==1 && visited[tx][ty] == 0) {
visited[tx][ty] = 1;
cnt++;
}
}
return cnt;
}

public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("C:\\Users\\SVMC\\eclipse-workspace\\bai3\\src\\bai3\\input.txt"));
scanner = new Scanner(System.in);
int test_cases = scanner.nextInt();
// System.out.println(test_cases);
for (int test = 1; test <= test_cases; test++) {
kQ = 0;
int N = scanner.nextInt();
int M = scanner.nextInt();
arrs = new int[N][M];
visited = new int[N][M];
readMX();
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs.length; j++) {
if(arrs[i][j]==2) {
kQ += Count(i, j);
}
}
}
System.out.println("#"+test+" "+kQ);
}
}
}
2020-02-21 02:44:43 nguyen ngoc son

8 8
1 1 0 1 1 0 2 1
1 0 0 0 1 0 0 1
0 0 0 0 1 1 0 0
1 1 0 2 0 0 1 0
0 0 0 0 1 1 0 0
0 1 0 1 1 1 1 0
0 2 0 1 1 0 1 1
1 0 0 1 0 1 0 1

8 8
0 0 1 1 1 0 1 1
0 2 0 1 1 0 0 0
0 0 1 1 1 2 0 1
1 1 1 0 1 0 0 0
0 0 1 1 1 0 1 0
1 2 1 1 1 1 1 0
0 1 0 1 1 1 0 1
0 1 0 0 1 2 1 1

8 8
1 1 0 0 0 0 1 0
1 1 0 2 0 0 0 1
0 0 1 0 0 1 1 1
0 0 1 1 0 2 0 0
0 0 1 0 1 1 1 1
0 2 0 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ac code: https://bit.ly/2eGhgab . Khỏi cảm ơn
2019-03-25 10:37:12
làm bằng quay lui thì chạy quá thời gian T_T
2019-02-20 14:50:23
ai ngờ khó thật
2019-02-20 14:49:51
lúc đầu đọc cmt thấy đệ quy tưởng khó lắm :)))
2018-09-04 16:56:24
nhật hào sạch
2018-06-06 11:22:56
dijsktra

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.