Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <deque>
- #include <utility>
- #include <algorithm>
- #include <cmath>
- #include <random>
- #include <functional>
- #define pb push_back
- #define mp make_pair
- #define all(_x) _x.begin(),_x.end()
- #define get(_type, _x) \
- _type _x; \
- cin >> _x;
- #define read(_v) for(int i=0;i<_v.size();++i)cin>>_v[i]
- using namespace std;
- typedef long long int ll;
- typedef string str;
- typedef vector<int> vint;
- typedef vector<ll> vll;
- typedef vector<str> vstr;
- typedef pair<int,int> pii;
- typedef pair<ll,ll> pll;
- /*
- (
- ( )\ )
- )\ ( ( ( (()/(
- (((_) ` ) ` ) ` ) ( )\))( ))\ /(_))
- )\___ /(/( /(/( /(/( )\((_)()\ /((_|_))
- ((/ __((_)_\((_)_\ ((_)_\ ((_)(()((_|_)) | _ \
- | (__| '_ \) '_ \) | '_ \) _ \ V V / -_)| /
- \___| .__/| .__/ | .__/\___/\_/\_/\___||_|_\
- |_| |_| |_|
- _____________ ________________
- ______ /_ / / /_ ___/__ __/
- ___ _ /_ / / /_____ \__ /
- / /_/ / / /_/ / ____/ /_ /
- \____/ \____/ /____/ /_/
- _______________
- ___ __ \_ __ \
- __ / / / / / /
- _ /_/ // /_/ /
- /_____/ \____/
- _______________
- ____ _/__ __/
- __ / __ /
- __/ / _ /
- /___/ /_/
- */
- struct ded{
- vector<int>langs;
- double len;
- };
- struct lang{
- vector<int>deds;
- double len;
- };
- int main()
- {
- int n,k;
- cin>>n>>k;
- vector<ded>deds(n);
- vector<lang>langs(k);
- for(int i=0;i<n;++i)deds[i].len=1000000000;
- for(int i=0;i<k;++i)langs[i].len=1000000000;
- for(int i=0;i<n;++i){
- int z;
- cin>>z;
- for(int j=0;j<z;++j){
- int L;
- cin>>L;
- deds[i].langs.pb(L-1);
- langs[L-1].deds.pb(i);
- }
- }
- double matrix[n][n];
- for (int start=0;start<n;++start){
- priority_queue<pair<double,int> > pq_deds;
- priority_queue<pair<double,int> >pq_langs;
- deds[start].len=0;
- pq_deds.push(mp(0.0,start));
- while((!pq_deds.empty())||(!pq_langs.empty())){
- //cout<<pq_deds.size()<<" - "<<pq_langs.size()<<endl;
- if((!pq_deds.empty())&&(pq_langs.empty()||pq_deds.top().first>pq_langs.top().first)){
- int current_ded = pq_deds.top().second;
- double current_len = - pq_deds.top().first;
- pq_deds.pop();
- if(deds[current_ded].len<current_len)continue;
- for(int i=0;i<deds[current_ded].langs.size();++i){
- if(current_len+(langs[deds[current_ded].langs[i]].deds.size()+0.0000001)/2-0.5<langs[deds[current_ded].langs[i]].len){
- langs[deds[current_ded].langs[i]].len=current_len+(langs[deds[current_ded].langs[i]].deds.size()+0.0000001)/2-0.5;
- //cout<<langs[deds[current_ded].langs[i]].len<<endl;
- pq_langs.push(mp(-langs[deds[current_ded].langs[i]].len,deds[current_ded].langs[i]));
- }
- }
- }else{
- int current_lang = pq_langs.top().second;
- double current_len = - pq_langs.top().first;
- pq_langs.pop();
- //cout<<"LANG POPPED"<<endl;
- if(langs[current_lang].len<current_len)continue;
- for(int i=0;i<langs[current_lang].deds.size();++i){
- //cout<<current_len+(langs[current_lang].deds.size()+0.0000001)/2<<endl;
- if(current_len+(langs[current_lang].deds.size()+0.0000001)/2-0.5<deds[langs[current_lang].deds[i]].len){
- deds[langs[current_lang].deds[i]].len=current_len+(langs[current_lang].deds.size()+0.0000001)/2-0.5;
- pq_deds.push(mp(-deds[langs[current_lang].deds[i]].len,langs[current_lang].deds[i]));
- }
- }
- }
- //cout<<pq_deds.size()<<" - "<<pq_langs.size()<<endl;
- }
- for(int i=0;i<deds.size();++i)matrix[start][i]=deds[i].len;
- for(int i=0;i<deds.size();++i)deds[i].len=1000000000;
- for(int i=0;i<langs.size();++i)langs[i].len=1000000000;
- }
- for (int i=0;i<n;++i){
- for(int j=0;j<n;j++){
- if(matrix[i][j]!=1000000000)
- {matrix[i][j]+=matrix[i][j]==0?0:1;cout<<matrix[i][j]<<' ';}else{cout<<-1<<' ';}
- }
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement