consolidate all repos to one for archive

This commit is contained in:
2025-01-28 13:46:42 +01:00
commit a6610fbc7a
5350 changed files with 2705721 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,295 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <array>
struct island
{
int L;
int R;
int time;
};
#define LEFT 0
#define FROM_LEFT 1
#define ACROSS 2
#define RIGHT 3
#define FROM_RIGHT 4
#define MAX_NUM 10000000
std::vector<std::array<int, 5>> get_line_combinations(std::vector<island> &line)
{
std::vector<std::array<int, 5>> comp(line.size(), std::array<int, 5>({0}));
if (line.size() == 0)
{
return comp;
}
// going across the line
std::sort(line.begin(), line.end(), [](const island &a, const island &b)
{ return a.time < b.time; });
for (size_t i = 0; i < line.size(); i++)
{
if (i == 0)
{
comp[i][ACROSS] = line[i].time + line[i].L + line[i].R;
}
else
{
comp[i][ACROSS] = comp[i - 1][ACROSS] + line[i].time;
}
}
// going back
std::sort(line.begin(), line.end(), [](const island &a, const island &b)
{ return a.L < b.L; });
std::vector<bool> Lused(line.size(), false);
int most_right_island = 0;
int Lmin = MAX_NUM;
int Lval = 0;
int dist = 0;
int Lindex = 0;
int FLmin = 0;
std::vector<bool> Rused(line.size(), false);
int most_left_island = line.size();
int Rmin = MAX_NUM;
int Rval = 0;
int Rindex = 0;
int FRmin = 0;
int tmp = 0;
for (size_t i = 0; i < line.size(); i++)
{
if (i == 0)
{
for (size_t j = 0; j < line.size(); j++)
{
Lval = line[j].L + line[j].L + line[j].time;
if (Lval < Lmin)
{
Lmin = Lval;
Lindex = j;
FLmin = line[j].L + line[j].time;
}
Rval = line[j].R + line[j].R + line[j].time;
if (Rval < Rmin)
{
Rmin = Rval;
Rindex = j;
FRmin = line[j].R + line[j].time;
}
}
}
else
{
for (size_t j = 0; j < line.size(); j++)
{
if (!Lused[j])
{
if (j < most_right_island)
{
Lval = comp[i - 1][LEFT] + line[j].time;
tmp = comp[i - 1][FROM_LEFT] + line[j].time;
}
else
{
dist = line[j].L - line[most_right_island].L;
Lval = comp[i - 1][LEFT] + dist + dist + line[j].time;
tmp = comp[i - 1][FROM_LEFT] + dist + line[j].time;
}
if (Lval < Lmin)
{
Lindex = j;
Lmin = Lval;
FLmin = tmp;
}
}
if (!Rused[j])
{
if (j > most_left_island)
{
Rval = comp[i - 1][RIGHT] + line[j].time;
tmp = comp[i - 1][FROM_RIGHT] + line[j].time;
}
else
{
dist = line[j].R - line[most_left_island].R;
Rval = comp[i - 1][RIGHT] + dist + dist + line[j].time;
tmp = comp[i - 1][FROM_RIGHT] + dist + line[j].time;
}
if (Rval < Rmin)
{
Rindex = j;
Rmin = Rval;
FRmin = tmp;
}
}
}
}
if (Rindex < most_left_island)
{
most_left_island = Rindex;
}
Rused[Rindex] = true;
comp[i][RIGHT] = Rmin;
Rmin = MAX_NUM;
comp[i][FROM_RIGHT] = FRmin;
if (Lindex > most_right_island)
{
most_right_island = Lindex;
}
Lused[Lindex] = true;
comp[i][LEFT] = Lmin;
Lmin = MAX_NUM;
comp[i][FROM_LEFT] = FLmin;
}
return comp;
}
void print_deb(std::vector<int> &RESULT, std::vector<std::array<int, 2>> &A)
{
for (size_t i = 0; i < RESULT.size(); i++)
{
std::cout << RESULT[i] << " ";
}
std::cout << std::endl
<< std::endl;
for (size_t i = 0; i < A.size(); i++)
{
std::cout << A[i][0] << " " << A[i][1] << std::endl;
}
std::cout << std::endl;
}
void increse(std::vector<std::array<int, 2>> &A)
{
for (size_t i = 0; i < A.size(); i++)
{
A[i][0]++;
A[i][1]++;
}
}
void print_line(std::vector<island> &line)
{
for (size_t i = 0; i < line.size(); i++)
{
std::cout << line[i].L << " " << line[i].time << " " << line[i].R << std::endl;
}
std::cout << std::endl;
}
void print_comp(std::vector<std::array<int, 5>> &comp)
{
for (size_t i = 0; i < comp.size(); i++)
{
std::cout << comp[i][LEFT] << " " << comp[i][FROM_LEFT] << " " << comp[i][ACROSS] << " " << comp[i][RIGHT] << " " << comp[i][FROM_RIGHT] << std::endl;
}
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
std::ifstream in(argv[1]);
if (!in)
{
std::cout << "Cannot open file.\n";
return 1;
}
// N = visina
int N;
// sirina
int M;
// stevilo otokov
int K;
in >> N >> M >> K;
M--;
std::vector<std::vector<island>> map(N, std::vector<island>());
int x, y, t;
for (size_t i = 0; i < K; i++)
{
in >> x >> y >> t;
x--;
y--;
map[x].emplace_back(island{y, M - y, t});
}
in.close();
std::vector<int> RESULT(K + 1, MAX_NUM);
std::vector<std::array<int, 2>> A(K + 1, {MAX_NUM, MAX_NUM});
RESULT[0] = 0;
A[0][0] = 0;
A[0][1] = M;
std::vector<std::array<int, 2>> B = A;
int num_of_islands = 0;
std::vector<std::array<int, 5>> comp = get_line_combinations(map[0]);
num_of_islands = comp.size();
for (size_t i = 0; i < comp.size(); i++)
{
RESULT[i + 1] = std::min(RESULT[i + 1], comp[i][FROM_LEFT]);
B[i + 1][0] = std::min(A[i + 1][0], comp[i][LEFT]);
B[i + 1][1] = std::min(A[i + 1][1], comp[i][ACROSS]);
}
increse(B);
A = B;
int prev = 0;
int tmp_left = 0;
int tmp_right = 0;
int tmp2, tmp3;
for (size_t i = 1; i < map.size(); i++)
{
comp = get_line_combinations(map[i]);
num_of_islands += comp.size();
for (size_t j = 1; j <= num_of_islands && comp.size() > 0; j++)
{
tmp_left = MAX_NUM;
tmp_right = MAX_NUM;
for (size_t k = 0; k < j && k < comp.size(); k++)
{
prev = j - (k + 1);
RESULT[j] = std::min({RESULT[j], A[prev][0] + comp[k][FROM_LEFT], A[prev][1] + comp[k][FROM_RIGHT]});
tmp2 = std::min({A[j][0], A[prev][0] + comp[k][LEFT], A[prev][1] + comp[k][ACROSS]});
if (tmp2 < tmp_left)
{
tmp_left = tmp2;
}
tmp3 = std::min({A[j][1], A[prev][0] + comp[k][ACROSS], A[prev][1] + comp[k][RIGHT]});
if (tmp3 < tmp_right)
{
tmp_right = tmp3;
}
}
B[j][0] = tmp_left;
B[j][1] = tmp_right;
}
increse(B);
A = B;
}
for (size_t i = 1; i < RESULT.size(); i++)
{
std::cout << RESULT[i] << " ";
}
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,27 @@
CC=g++
CFLAGS= -std=c++23
INCLUDE_DIR= include
SRC_DIR= src
# Get all cpp files from src directory
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
all: main run
main: main.cpp $(SRCS)
$(CC) $(CFLAGS) -O3 -I$(INCLUDE_DIR) $(SRCS) main.cpp -o main
debug: main.cpp $(SRCS)
$(CC) $(CFLAGS) -I$(INCLUDE_DIR) $(SRCS) -g main.cpp -o main
run: main
./main test/testni_primer2.txt
zip:
zip -r main.zip main.cpp $(INCLUDE_DIR) $(SRC_DIR) makefile
clean:
rm main

Binary file not shown.

View File

@@ -0,0 +1,6 @@
4 4 5
1 3 1
1 2 9
2 3 5
3 2 6
4 3 2

View File

@@ -0,0 +1 @@
3 10 17 26 36

View File

@@ -0,0 +1,13 @@
10 10 12
2 2 11
1 5 4
3 3 55
3 2 3
3 7 22
4 5 5
6 2 3
7 7 2
8 2 2
8 9 6
9 5 7
9 9 11

View File

@@ -0,0 +1 @@
6 14 20 32 44 56 69 82 95 110 140 195

View File

@@ -0,0 +1,19 @@
50 70 18
1 5 1
1 8 2
3 3 3
3 2 55
6 2 11
8 9 4
9 4 3
15 15 5
13 4 13
17 20 17
30 5 22
30 6 133
30 7 44
30 30 12
44 44 34
45 7 55
48 5 87
49 37 64

View File

@@ -0,0 +1 @@
5 10 24 38 51 71 94 118 170 210 254 309 364 469 569 674 788 921

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff