Advertisement
Guest User

matt

a guest
May 24th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<vector>
  4. #include<map>
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned long long ull;
  9.  
  10. const int mod=1000000007;
  11. const int maxS=550;
  12.  
  13. struct state{
  14.   int f,w,node;
  15.   state(int f=0,int w=0,int node=0) :
  16.     f(f),w(w),node(node) {}
  17.   bool operator <(const state& r)const {
  18.     return f==r.f?(w==r.w?(node<r.node):w<r.w):f<r.f;
  19.   }
  20. };
  21.  
  22. vector<int> graph[15];
  23. map<state,int> M;
  24.  
  25. ull mat[maxS][maxS],pom[maxS][maxS],m3[maxS][maxS];
  26. int n,m,h,s,e,mf,mw,waste,cnt,k1,k2;
  27. int f[15],w[15];
  28.  
  29. void precompute(){
  30.   for(int i=0;i<n;++i){
  31.     for(int f=0;f<mf;++f){
  32.       for(int w=0;w<mw;++w){
  33.         M[state(f,w,i)]=cnt++;
  34.       }
  35.     }
  36.   }
  37. }
  38.  
  39. void create_matrix(){
  40.   for(int i=0;i<n;++i){
  41.     for(int j=0;j<graph[i].size();++j){
  42.        if(f[i]==1 && w[i]==1){
  43.          k1=(f[graph[i][j]]==1?0:1);
  44.          k2=(w[graph[i][j]]==1?0:1);
  45.              mat[M[state(0,0,i)] ][M[state(k1,k2,graph[i][j])]]=1;
  46.        }
  47.        else if(f[i]==1){
  48.          k1=(f[graph[i][j]]==1?0:1);
  49.          k2=(w[graph[i][j]]==1?0:1);
  50.             for(int w=1;w<mw;++w)
  51.               mat[M[state(0,w,i)] ][M[state(k1,k2*(w+1),graph[i][j])]]=1;
  52.        }
  53.        else if(w[i]==1){
  54.          k1=(f[graph[i][j]]==1?0:1);
  55.          k2=(w[graph[i][j]]==1?0:1);
  56.          for(int f=1;f<mf;++f)
  57.               mat[M[state(f,0,i)] ][M[state(k1*(f+1),k2,graph[i][j])]]=1;
  58.        }
  59.        else{
  60.          k1=(f[graph[i][j]]==1?0:1);
  61.          k2=(w[graph[i][j]]==1?0:1);
  62.          for(int f=1;f<mf;++f)
  63.             for(int w=1;w<mw;++w)
  64.               mat[M[state(f,w,i)] ][M[state(k1*(f+1),k2*(w+1),graph[i][j])]]=1;
  65.        }
  66.     }
  67.   }
  68.   for(int i=0;i<M.size();++i){ //addind bonus node and edges to calc walk <=h
  69.     mat[cnt][cnt]=1;
  70.     mat[i][cnt]=1;
  71.     cnt++;
  72.   }
  73. }
  74.  
  75. void make_equal(ull m1[maxS][maxS],ull m2[maxS][maxS]){
  76.   for(int i=0;i<cnt;++i)
  77.     for(int j=0;j<cnt;++j)
  78.       m1[i][j]=m2[i][j];
  79. }
  80.  
  81. void multiple(ull m1[maxS][maxS],ull m2[maxS][maxS]){
  82.   memset(m3,0,sizeof m3);
  83.  
  84.   for(int i=0;i<cnt;++i)
  85.     for(int j=0;j<cnt;++j)
  86.       for(int k=0;k<cnt;++k){
  87.         m3[i][j]+=(m1[i][k]*m2[k][j]);
  88.         m3[i][j]%=mod;
  89.       }
  90.   make_equal(m1,m3);
  91. }
  92.  
  93. void potentiate(){
  94.   bool done=false;
  95.   make_equal(pom,mat);
  96.   while(h){
  97.     if(h&1){
  98.       if(done==true) multiple(mat,pom);
  99.       else { done=true; make_equal(mat,pom);  }
  100.     }
  101.     multiple(pom,pom);
  102.     h>>=1;
  103.   }
  104. }
  105.  
  106. void solve(){
  107.   potentiate();
  108.   printf("%d",mat[M[state(0,0,s)]][M[state(0,0,e)]]);
  109. }
  110.  
  111. int main(){
  112. scanf("%d%d%d%d%d%d%d",&n,&m,&h,&s,&e,&mf,&mw);
  113. s--; e--;
  114. for(int i=0;i<n;++i)
  115.   scanf("%d%d%d",&f[i],&w[i],&waste);
  116. for(int i=0;i<m;++i){
  117.   int a,b;
  118.   scanf("%d%d",&a,&b);
  119.   a--; b--;
  120.   graph[a].push_back(b);
  121.   graph[b].push_back(a);
  122. }
  123. precompute();
  124. create_matrix();
  125.  
  126. solve();
  127.  
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement