Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int N = 1e3 + 7;
- const int INF = 1e18;
- vector<pair<pair<int,int>,int>> krawedz;
- int odl[N];
- int pop[N];
- int n, m, q, a, b, c;
- void fb(int v)
- {
- for(int i = 1; i <= n; i++)
- {
- odl[i] = INF;
- pop[i] = 0;
- }
- odl[v] = 0;
- for(int i = 1; i <= n; i++)
- {
- for(auto xd: krawedz)
- {
- if(odl[xd.first.first] != INF && odl[xd.first.second] > odl[xd.first.first] + xd.second)
- {
- odl[xd.first.second] = odl[xd.first.first] + xd.second;
- pop[xd.first.second] = xd.first.first;
- }
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- cin >> n >> m >> q;
- for(int i = 1; i <= m; i++)
- {
- cin >> a >> b >> c;
- krawedz.push_back({{a,b},c});
- }
- for(int i = 0; i < q; i++)
- {
- cin >> a >> b;
- fb(a);
- /*
- cout << "odleglosc do b: " << odl[b] << ' ';
- cout << endl;
- */
- if(odl[b] == INF)
- {
- cout << "NIE\n";
- }
- else
- {
- cout << odl[b] << ' ';
- vector<int> wynik;
- wynik.push_back(b);
- int x = b;
- while(pop[x]!=a)
- {
- x = pop[x];
- wynik.push_back(x);
- }
- wynik.push_back(a);
- cout << wynik.size() << ' ';
- while(!wynik.empty())
- {
- cout<<wynik.back()<<' ';
- wynik.pop_back();
- }
- }
- cout << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement