70 lines
1.8 KiB
C++
70 lines
1.8 KiB
C++
#include <stdio.h>
|
|
int N, M;
|
|
unsigned short edges[1050000][3] = {0};
|
|
// 边是否顺序? 按开始点排序
|
|
// 时间限制最大是多少? 超过unsigned short
|
|
int head[66000] = {0};
|
|
unsigned short start = 0, terminal = 0;
|
|
int time_limit = 0;
|
|
int path[1000] = {0};
|
|
int current_cheap = 2147483647;
|
|
|
|
int dfs(unsigned short cur, int cur_time, int cur_cost, int path_ptr) {
|
|
path[path_ptr] = cur;
|
|
if (cur_time > time_limit) {
|
|
return 0;
|
|
}
|
|
|
|
if (cur_cost > current_cheap) {
|
|
return 0;
|
|
}
|
|
|
|
if (cur == terminal) {
|
|
if (cur_cost < current_cheap) {
|
|
current_cheap = cur_cost;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
for (int i = head[cur]; i < head[cur + 1]; i++) {
|
|
for (int j = 0; j < path_ptr; j++) {
|
|
if (path[j] == edges[i][0]) {
|
|
continue;
|
|
}
|
|
}
|
|
dfs(edges[i][0], cur_time + edges[i][1], cur_cost + edges[i][2], path_ptr + 1);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
scanf("%d %d", &N, &M);
|
|
head[0] = 0;
|
|
int next_empty_edge = 0;
|
|
int current_start = 0;
|
|
for (int i = 0; i < M; i++) {
|
|
int from, to, time, cost;
|
|
scanf("%d %d %d %d", &from, &to, &time, &cost);
|
|
if (from != current_start) {
|
|
for (int j = current_start + 1; j <= from; j++) {
|
|
head[j] = next_empty_edge;
|
|
}
|
|
current_start = from;
|
|
// head[from] = next_empty_edge;
|
|
}
|
|
edges[next_empty_edge][0] = to;
|
|
edges[next_empty_edge][1] = time;
|
|
edges[next_empty_edge][2] = cost;
|
|
next_empty_edge++;
|
|
}
|
|
head[++current_start] = next_empty_edge;
|
|
scanf("%hu %hu %d", &start, &terminal, &time_limit);
|
|
|
|
dfs(start, 0, 0, 0);
|
|
if (current_cheap == 2147483647) {
|
|
printf("-1\n");
|
|
return 0;
|
|
}
|
|
printf("%d\n", current_cheap);
|
|
return 0;
|
|
} |