Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. struct edges {
  9.     int a;
  10.     int b;
  11.     int cost;
  12. };
  13.  
  14. int main() {
  15.     vector <edges> e;
  16.     edges edge;
  17.     const int INF = 32768;
  18.     int n, v, t, c; size_t m=0;
  19.     ofstream out("out.txt");
  20.     ifstream in("in.txt");
  21.     in >> n;
  22.    // cout << "n=" << n << endl;
  23.     vector<int> d (n, INF);
  24.     for (int i=0; i<n; i++)
  25.             {
  26.                for (int j=0; j<n; j++)
  27.                {
  28.                    in>>c;
  29.                  //  cout << "c=" << c << endl;
  30.                    //d[i]=-c;
  31.                    if (c!=-32768) {
  32.                    edge.a=i; edge.b=j; edge.cost=-c;
  33.                    e.push_back(edge);
  34.                  //  cout << "e=" << i << j << -c << endl;
  35.                    m++;}
  36.                }
  37.             }
  38.    // cout << "m=" << m << endl;
  39.     in >> v; in >> t;
  40.     v=v-1; t=t-1;
  41.   //  cout << "v=" << v << endl;
  42.   //  cout << "t=" << t << endl;
  43.     d[v] = 0;
  44.     vector<int> p (n, -1);
  45.     int w=1;
  46.     for (int i=0; i<n-1; ++i) {
  47.         for (int j=0; j<m; j++)
  48.                 if (d[e[j].b] > d[e[j].a] + e[j].cost) {
  49.                     d[e[j].b] = d[e[j].a] + e[j].cost;
  50.                     p[e[j].b] = e[j].a;
  51.                     }
  52.     }
  53.  
  54.     if (d[t] == INF)
  55.         out << "N";
  56.     else {
  57.         out << "Y" << endl;
  58.         vector<int> path;
  59.         for (int cur=t; cur!=-1; cur=p[cur]) {
  60.             path.push_back (cur);
  61.             for (int j=0; j<m; j++)
  62.             {
  63.                 if ((e[j].a==p[cur]) and (e[j].b==cur))
  64.                 {
  65.                     w=w*(-e[j].cost);
  66.                 }
  67.             }
  68.         }
  69.         reverse (path.begin(), path.end());
  70.  
  71.     //  cout << "Path from " << v << " to " << t << ": ";
  72.         for (size_t i=0; i<path.size(); i++)
  73.         {
  74.          //   cout << path[i] << ' ';
  75.             out << path[i]+1 << ' ';
  76.             }
  77.  
  78.      /*   for (size_t i=0; i<path.size()-1; i++)
  79.         { w=w*(-e[path[i]-1].cost);
  80.           cout << "cost "<< e[path[i]+1].cost << ' ';}*/
  81.           out<<endl; out<<w;
  82.     }
  83.     //cout << "w=" << w << endl;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement