Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>
  5. #include <queue>
  6. #include <stack>
  7. #include <deque>
  8. #include <utility>
  9. #include <algorithm>
  10. #include <cmath>
  11. #include <random>
  12. #include <functional>
  13.  
  14.  
  15. #define pb push_back
  16. #define mp make_pair
  17. #define all(_x) _x.begin(),_x.end()
  18. #define get(_type, _x) \
  19.     _type _x; \
  20.     cin >> _x;
  21. #define read(_v) for(int i=0;i<_v.size();++i)cin>>_v[i]
  22.  
  23. using namespace std;
  24.  
  25. typedef long long int ll;
  26. typedef string  str;
  27. typedef vector<int> vint;
  28. typedef vector<ll> vll;
  29. typedef vector<str> vstr;
  30. typedef pair<int,int> pii;
  31. typedef pair<ll,ll> pll;
  32. /*
  33.                                           (
  34.    (                                       )\ )
  35.    )\                          (  (     ( (()/(
  36.  (((_) `  )  `  )    `  )   (  )\))(   ))\ /(_))
  37.  )\___ /(/(  /(/(    /(/(   )\((_)()\ /((_|_))
  38. ((/ __((_)_\((_)_\  ((_)_\ ((_)(()((_|_)) | _ \
  39.  | (__| '_ \) '_ \) | '_ \) _ \ V  V / -_)|   /
  40.   \___| .__/| .__/  | .__/\___/\_/\_/\___||_|_\
  41.       |_|   |_|     |_|
  42.  
  43.  
  44.  
  45. _____________  ________________
  46. ______  /_  / / /_  ___/__  __/
  47. ___ _  /_  / / /_____ \__  /
  48. / /_/ / / /_/ / ____/ /_  /
  49. \____/  \____/  /____/ /_/
  50.  
  51.                    _______________
  52.                    ___  __ \_  __ \
  53.                    __  / / /  / / /
  54.                    _  /_/ // /_/ /
  55.                    /_____/ \____/
  56.  
  57.                                _______________
  58.                                ____  _/__  __/
  59.                                 __  / __  /
  60.                                __/ /  _  /
  61.                                /___/  /_/
  62.  
  63.  
  64.  
  65. */
  66. struct ded{
  67.     vector<int>langs;
  68.     double len;
  69. };
  70.  
  71. struct lang{
  72.     vector<int>deds;
  73.     double len;
  74. };
  75.  
  76. int main()
  77. {
  78.     int n,k;
  79.     cin>>n>>k;
  80.     vector<ded>deds(n);
  81.     vector<lang>langs(k);
  82.     for(int i=0;i<n;++i)deds[i].len=1000000000;
  83.     for(int i=0;i<k;++i)langs[i].len=1000000000;
  84.     for(int i=0;i<n;++i){
  85.         int z;
  86.         cin>>z;
  87.         for(int j=0;j<z;++j){
  88.             int L;
  89.             cin>>L;
  90.             deds[i].langs.pb(L-1);
  91.             langs[L-1].deds.pb(i);
  92.         }
  93.     }
  94.     double matrix[n][n];
  95.     for (int start=0;start<n;++start){
  96.         priority_queue<pair<double,int> > pq_deds;
  97.         priority_queue<pair<double,int> >pq_langs;
  98.         deds[start].len=0;
  99.         pq_deds.push(mp(0.0,start));
  100.         while((!pq_deds.empty())||(!pq_langs.empty())){
  101.             //cout<<pq_deds.size()<<" - "<<pq_langs.size()<<endl;
  102.             if((!pq_deds.empty())&&(pq_langs.empty()||pq_deds.top().first>pq_langs.top().first)){
  103.  
  104.                 int current_ded = pq_deds.top().second;
  105.                 double current_len = - pq_deds.top().first;
  106.                 pq_deds.pop();
  107.                 if(deds[current_ded].len<current_len)continue;
  108.                 for(int i=0;i<deds[current_ded].langs.size();++i){
  109.                     if(current_len+(langs[deds[current_ded].langs[i]].deds.size()+0.0000001)/2-0.5<langs[deds[current_ded].langs[i]].len){
  110.                         langs[deds[current_ded].langs[i]].len=current_len+(langs[deds[current_ded].langs[i]].deds.size()+0.0000001)/2-0.5;
  111.                         //cout<<langs[deds[current_ded].langs[i]].len<<endl;
  112.                         pq_langs.push(mp(-langs[deds[current_ded].langs[i]].len,deds[current_ded].langs[i]));
  113.                     }
  114.                 }
  115.             }else{
  116.  
  117.                 int current_lang = pq_langs.top().second;
  118.                 double current_len = - pq_langs.top().first;
  119.                 pq_langs.pop();
  120.                 //cout<<"LANG POPPED"<<endl;
  121.                 if(langs[current_lang].len<current_len)continue;
  122.                 for(int i=0;i<langs[current_lang].deds.size();++i){
  123.                     //cout<<current_len+(langs[current_lang].deds.size()+0.0000001)/2<<endl;
  124.                     if(current_len+(langs[current_lang].deds.size()+0.0000001)/2-0.5<deds[langs[current_lang].deds[i]].len){
  125.                         deds[langs[current_lang].deds[i]].len=current_len+(langs[current_lang].deds.size()+0.0000001)/2-0.5;
  126.                         pq_deds.push(mp(-deds[langs[current_lang].deds[i]].len,langs[current_lang].deds[i]));
  127.  
  128.                     }
  129.                 }
  130.             }
  131.             //cout<<pq_deds.size()<<" - "<<pq_langs.size()<<endl;
  132.         }
  133.         for(int i=0;i<deds.size();++i)matrix[start][i]=deds[i].len;
  134.         for(int i=0;i<deds.size();++i)deds[i].len=1000000000;
  135.         for(int i=0;i<langs.size();++i)langs[i].len=1000000000;
  136.  
  137.     }
  138.     for (int i=0;i<n;++i){
  139.         for(int j=0;j<n;j++){
  140.  
  141.             if(matrix[i][j]!=1000000000)
  142.             {matrix[i][j]+=matrix[i][j]==0?0:1;cout<<matrix[i][j]<<' ';}else{cout<<-1<<' ';}
  143.         }
  144.         cout<<endl;
  145.     }
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement