Guest User

Untitled

a guest
May 20th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <ctime>
  7. #include <cctype>
  8. #include <cstdlib>
  9. #include <cassert>
  10. #include <functional>
  11. #include <iomanip>
  12. #include <string>
  13. #include <cstring>
  14. #include <map>
  15. #include <set>
  16. #include <vector>
  17. #include <queue>
  18.  
  19. using namespace std;
  20.  
  21. int s, f;
  22.  
  23. vector <pair <int, int> > g[3000];
  24.  
  25. queue < pair<int, int> > bfs;
  26.  
  27. int used[3000];
  28.  
  29. unsigned long long d[3000];
  30.  
  31.  
  32. int ans = 1e9;
  33.  
  34. int m[3000][3000];
  35.  
  36. /*
  37. string sum(const string &a, const string &b)
  38. {
  39.     int flag = 0;
  40.  
  41.     string s1 = "";
  42.  
  43.     while(a.size() != b.size())
  44.     {
  45.         if(a.size() > b.size())
  46.             b = '0' + b;
  47.         else
  48.             a = '0' + a;
  49.     }
  50.    
  51.     for(int i = 0; i <= a.size(); i++)
  52.         s1 += '0';
  53.    
  54.     for(int i = max(a.size(), b.size()) - 1; i >= 0; i--)
  55.     {
  56.         int a1, b1;
  57.            
  58.         a1 = int(a[i]) - int('0');
  59.         b1 = int(b[i]) - int('0');
  60.        
  61.         s1[i + 1] = char( ((a1 + b1 + flag) % 10) + '0' );
  62.            
  63.         //cerr << a1 << " " << b1 << " " << s1[i + 1] << endl;
  64.            
  65.         flag = (a1 + b1 + flag) / 10;
  66.        
  67.     }  
  68.    
  69.     s1[0] = char(flag + '0');
  70.    
  71.     //cerr << a << "+" << b << "=" << s1 << endl;
  72.     return s1;
  73. }
  74. */
  75. unsigned long long rec(int x)
  76. {
  77.     if(d[x] == 0)
  78.     {
  79.    
  80.         if(x == s)
  81.         {
  82.             return(1);
  83.         }  
  84.  
  85.         unsigned long long res = 0;
  86.    
  87.         for(int i = 0; i < g[x].size(); i++)
  88.         {
  89.             if(used[g[x][i].first] == used[x] - g[x][i].second)
  90.             {
  91.                 res += rec(g[x][i].first); 
  92.             }
  93.         }
  94.  
  95.         d[x] = res;
  96.         return res;
  97.     }
  98.     else
  99.         return(d[x]);
  100. }
  101. int main ()
  102. {
  103.     int n, m;
  104.    
  105.     cin >> n >> m;
  106.    
  107.     for(int i = 0; i <= 2000; i++)
  108.         used[i] = 1e9;
  109.    
  110.     for(int i = 0; i < m; i++)
  111.     {
  112.         int a, b, c;
  113.         scanf("%d%d%d", &a, &b, &c);
  114.        
  115.         g[a].push_back(make_pair(b, c));
  116.         g[b].push_back(make_pair(a, c));
  117.     }
  118.    
  119.     cin >> s >> f;
  120.    
  121.     bfs.push( make_pair(s, 0) );
  122.     used[s] = 0;
  123.    
  124.     while(!bfs.empty())
  125.     {
  126.         pair<int, int> t;
  127.        
  128.         t = bfs.front();
  129.        
  130.         if(t.first == f)
  131.         {
  132.             ans = min(ans, t.second);
  133.         }
  134.        
  135.         bfs.pop();
  136.        
  137.         for(int i = 0; i < g[t.first].size(); i++)
  138.         {
  139.             if(used[ g[t.first][i].first ] >= t.second + g[t.first][i].second)
  140.             {
  141.                 bfs.push(make_pair(g[t.first][i].first, g[t.first][i].second + t.second));
  142.                
  143.                 used[g[t.first][i].first] = g[t.first][i].second + t.second;
  144.             }
  145.         }
  146.    
  147.     }
  148.     //for(int i = 0; i < 2000; i++)
  149.     //  used[i] = 0;
  150.    
  151.     if(ans == 1e9)
  152.     {
  153.         cout << 0;
  154.         return 0;
  155.     }
  156.    
  157.     cout << rec(f);
  158.    
  159.     return 0;
  160. }
Add Comment
Please, Sign In to add comment