Mam takie zadanie Bitocja
Moje rozwiązanie z użyciem algorytmu Dijkstry:
#include <bits/stdc++.h>
#define ll long long
const ll INF = 1000000 * 100 + 1;
using namespace std;
class Bitocja{
int number_of_cities;
int built_ways;
int new_ways;
ll min_distance;
vector<vector<pair<int, int>>> graph;
public:
void add_way(int city_a, int city_b, int weight){
graph[city_a].push_back(make_pair(city_b, weight));
graph[city_b].push_back(make_pair(city_a, weight));
}
void delete_way(int city_a, int city_b){
graph[city_a].pop_back();
graph[city_b].pop_back();
}
Bitocja(){
int city_a, city_b, weight;
cin >> number_of_cities >> built_ways >> new_ways;
graph.resize(number_of_cities + 1);
for (int i = 0; i < built_ways; i++){
cin >> city_a >> city_b >> weight;
add_way(city_a, city_b, weight);
}
}
ll calculate_min_dist_between_first_and_last(){
priority_queue<pair<ll, ll>, vector<pair<ll, ll>> , greater<pair<ll, ll>>> pq;
vector<ll> dist(number_of_cities + 1, INF);
pq.push(make_pair(0, 1));
dist[1] = 0;
while (!pq.empty()){
ll curr_vertex = pq.top().second;
pq.pop();
for (pair<ll, ll> next : graph[curr_vertex]){
ll vertex = next.first;
ll weight = next.second;
if (dist[vertex] > dist[curr_vertex] + weight){
dist[vertex] = dist[curr_vertex] + weight;
pq.push(make_pair(dist[vertex], vertex));
}
}
}
return dist[number_of_cities];
}
bool is_good_way(ll distance){
return distance < min_distance;
}
void check_new_ways(){
min_distance = calculate_min_dist_between_first_and_last();
int city_a, city_b, weight;
for (int i = 0; i < new_ways; i++){
cin >> city_a >> city_b >> weight;
add_way(city_a, city_b, weight);
ll new_distance = calculate_min_dist_between_first_and_last();
if (is_good_way(new_distance)){
cout << 1 << '\n';
min_distance = new_distance;
}
else{
cout << 0 << '\n';
delete_way(city_a, city_b);
}
}
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
Bitocja bitocja;
bitocja.check_new_ways();
return 0;
}
Niestety za wolne, czytałem gdzieś że trzeba użyć Floyda-Warhalla, tylko jak go później zmieniać wraz z zmianami w grafie ? I gdy zrobimy zmiane którą trzeba będzie cofnąć a utworzą już się nowe połączenia, trzeba kopiować macierz ?