iscsi

SRM 525 525

Nov 30th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. class Rumor {
  2. public:
  3.    int getMinimum( string knowledge, vector <string> graph )
  4.     {
  5.         int n=knowledge.size();
  6.         vector<pair<int,int> > f;
  7.         const pair<int,int> PINF(500,500);
  8.         deque<int > q;
  9.         int act,aw,bw,best=500;
  10.         REP(iii,1<<n)
  11.         {
  12.             f.clear();
  13.             f.resize(n,PINF);
  14.             REP(i,n)
  15.             {
  16.                 if(knowledge[i]=='Y')
  17.                 {
  18.                     f[i].first=0,f[i].second=0;
  19.                     q.push_back(i);
  20.                 }
  21.             }  
  22.             while(q.size())
  23.             {
  24.                 act=q.front();
  25.                 q.pop_front();
  26.                 aw=f[act].first+1;
  27.                 bw=f[act].second+1;
  28.                 if(aw==bw)
  29.                 {
  30.                     (iii&(1<<act))?++aw:++bw;
  31.                 }
  32.                 REP(i,n)
  33.                 {
  34.                     if(graph[act][i]=='Y' && (f[i].first>aw || f[i].second>bw))
  35.                     {
  36.                         f[i].first=min(f[i].first,aw);
  37.                         f[i].second=min(f[i].second,bw);
  38.                         q.push_back(i);
  39.                     }
  40.                 }
  41.             }
  42.             int mmax=0;
  43.             REP(i,n)
  44.             mmax=max(mmax,max(f[i].first,f[i].second));
  45.             if(best>mmax)
  46.                 best=mmax;
  47.         }
  48.         return best==500?-1:best;
  49.    }
  50. };
  51.  
Advertisement
Add Comment
Please, Sign In to add comment