kej

Граф в виде списка смежности

kej
Apr 28th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. using namespace std;
  5. struct graph
  6. {
  7.     int x;
  8.     int y;
  9.     double weight;
  10. };
  11. struct node
  12. {
  13.     graph G;
  14.     node* next;
  15. };
  16. void push (node* &p, graph G)
  17. {
  18.     node *temp;
  19.     temp = new node;
  20.     temp->G = G;
  21.     temp->next =p;
  22.     p= temp;
  23.    
  24.    
  25. }
  26. void show (node* &p)
  27. {
  28.     while (p)
  29.     {
  30.         cout<<p->G.x <<" "<< p->G.y << " "<<p->G.weight<<endl;
  31.                       p = p->next;
  32.     }
  33. }
  34. void read (node* &p,ifstream &file)
  35. {
  36.     p = NULL;
  37.     graph g;
  38.     while (file >> g.x >>g.y >> g.weight)
  39.     {
  40.         push(p, g);
  41.     }
  42. }
  43. void write (node* p,ofstream &out)
  44. {
  45.     while (p)
  46.     {
  47.         out<<p->G.x <<" "<< p->G.y << " "<<p->G.weight<<endl;
  48.                p = p->next;
  49.     }
  50. }
  51. bool IJustSpentACheck (node* &p,int x,int y)
  52. {
  53.     while (p)
  54.     {
  55.         if ((p ->G.x == x && p->G.y == y)||(p ->G.x == y && p->G.y == x ))
  56.         {
  57.             if (p->G.weight > 0 )
  58.                 return true;
  59.            
  60.         }
  61.         p=p->next;
  62.     }
  63.     return false;
  64. }
  65. int main()
  66. {
  67.     node *list;
  68.     ifstream f ("input.txt");
  69.     ofstream out ("output.txt");
  70.     read(list, f);
  71.     write(list, out);
  72.     int x,y;
  73.     cout<<"Enter x: ";
  74.     cin>>x;
  75.     cout<<"Enter y: ";
  76.     cin>>y;
  77.  
  78.     if (IJustSpentACheck(list, x, y))
  79.         cout<<"Yes";
  80.     else
  81.         cout<<"No";
  82.    
  83.    
  84. }
Add Comment
Please, Sign In to add comment