Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- #include <vector>
- #include <fstream>
- using namespace std;
- struct edges {
- int a;
- int b;
- int cost;
- };
- int main() {
- vector <edges> e;
- edges edge;
- const int INF = 32768;
- int n, v, t, c; size_t m=0;
- ofstream out("out.txt");
- ifstream in("in.txt");
- in >> n;
- // cout << "n=" << n << endl;
- vector<int> d (n, INF);
- for (int i=0; i<n; i++)
- {
- for (int j=0; j<n; j++)
- {
- in>>c;
- // cout << "c=" << c << endl;
- //d[i]=-c;
- if (c!=-32768) {
- edge.a=i; edge.b=j; edge.cost=-c;
- e.push_back(edge);
- // cout << "e=" << i << j << -c << endl;
- m++;}
- }
- }
- // cout << "m=" << m << endl;
- in >> v; in >> t;
- v=v-1; t=t-1;
- // cout << "v=" << v << endl;
- // cout << "t=" << t << endl;
- d[v] = 0;
- vector<int> p (n, -1);
- int w=1;
- for (int i=0; i<n-1; ++i) {
- for (int j=0; j<m; j++)
- if (d[e[j].b] > d[e[j].a] + e[j].cost) {
- d[e[j].b] = d[e[j].a] + e[j].cost;
- p[e[j].b] = e[j].a;
- }
- }
- if (d[t] == INF)
- out << "N";
- else {
- out << "Y" << endl;
- vector<int> path;
- for (int cur=t; cur!=-1; cur=p[cur]) {
- path.push_back (cur);
- for (int j=0; j<m; j++)
- {
- if ((e[j].a==p[cur]) and (e[j].b==cur))
- {
- w=w*(-e[j].cost);
- }
- }
- }
- reverse (path.begin(), path.end());
- // cout << "Path from " << v << " to " << t << ": ";
- for (size_t i=0; i<path.size(); i++)
- {
- // cout << path[i] << ' ';
- out << path[i]+1 << ' ';
- }
- /* for (size_t i=0; i<path.size()-1; i++)
- { w=w*(-e[path[i]-1].cost);
- cout << "cost "<< e[path[i]+1].cost << ' ';}*/
- out<<endl; out<<w;
- }
- //cout << "w=" << w << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement