Guest User

Untitled

a guest
May 20th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 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. string bad = "kslrhgigbrktbtgbtbrt";
  22.  
  23. int s, f;
  24.  
  25. vector <pair <int, int> > g[2000];
  26.  
  27. queue < pair<int, int> > bfs;
  28.  
  29. int used[2000];
  30.  
  31. int ans = 1e9;
  32.  
  33. int m[2000][2000];
  34.  
  35. string sum(string a, string b)
  36. {
  37.     int flag = 0;
  38.  
  39.     string s1 = "";
  40.  
  41.     while(a.size() != b.size())
  42.     {
  43.         if(a.size() > b.size())
  44.             b = '0' + b;
  45.         else
  46.             a = '0' + a;
  47.     }
  48.    
  49.     for(int i = 0; i <= a.size(); i++)
  50.         s1 += '0';
  51.    
  52.     for(int i = max(a.size(), b.size()) - 1; i >= 0; i--)
  53.     {
  54.         int a1, b1;
  55.            
  56.         a1 = int(a[i]) - int('0');
  57.         b1 = int(b[i]) - int('0');
  58.        
  59.         s1[i + 1] = char( ((a1 + b1 + flag) % 10) + '0' );
  60.            
  61.         //cerr << a1 << " " << b1 << " " << s1[i + 1] << endl;
  62.            
  63.         flag = (a1 + b1 + flag) / 10;
  64.        
  65.     }  
  66.    
  67.     s1[0] = char(flag + '0');
  68.    
  69.     //cerr << a << "+" << b << "=" << s1 << endl;
  70.     return s1;
  71. }
  72.  
  73. string rec(int x)
  74. {
  75.     cerr << "!" << x << " " << used[x] << endl;
  76.    
  77.     if(x == s)
  78.     {
  79.         return("1");
  80.     }  
  81.  
  82.     string res = "0000";
  83.    
  84.     for(int i = 0; i < g[x].size(); i++)
  85.     {
  86.         cerr << g[x][i].first << "  next - " << used[g[x][i].first] << "  cost - " << g[x][i].second << endl;  
  87.        
  88.         if(used[g[x][i].first] == used[x] - g[x][i].second)
  89.         {
  90.             res = sum(res, rec(g[x][i].first));
  91.         }
  92.     }
  93.    
  94.     return res;
  95. }
  96.  
  97. int main ()
  98. {
  99.     int n, m;
  100.    
  101.     cin >> n >> m;
  102.    
  103.     for(int i = 0; i < 2000; i++)
  104.         used[i] = 239;
  105.    
  106.     for(int i = 0; i < m; i++)
  107.     {
  108.         int a, b, c;
  109.         scanf("%d%d%d", &a, &b, &c);
  110.        
  111.         g[a].push_back(make_pair(b, c));
  112.         g[b].push_back(make_pair(a, c));
  113.     }
  114.    
  115.     cin >> s >> f;
  116.    
  117.     bfs.push( make_pair(s, 0) );
  118.     used[s] = 0;
  119.    
  120.     while(!bfs.empty())
  121.     {
  122.         pair<int, int> t;
  123.        
  124.         t = bfs.front();
  125.        
  126.         if(t.first == f)
  127.         {
  128.             ans = min(ans, t.second);
  129.         }
  130.        
  131.         bfs.pop();
  132.        
  133.         for(int i = 0; i < g[t.first].size(); i++)
  134.         {
  135.             if(used[ g[t.first][i].first ] == 239)
  136.             {
  137.                 bfs.push(make_pair(g[t.first][i].first, g[t.first][i].second + t.second));
  138.                
  139.                 used[g[t.first][i].first] = g[t.first][i].second + t.second;
  140.             }
  141.         }
  142.    
  143.     }
  144.    
  145.     //for(int i = 0; i < 2000; i++)
  146.     //  used[i] = 0;
  147.    
  148.     if(ans == 1e9)
  149.     {
  150.         cout << 0;
  151.         return 0;
  152.     }
  153.    
  154.     string answer = rec(f);
  155.    
  156.     cerr << answer;
  157.    
  158.     int j = 0;
  159.    
  160.     while(answer[j] == '0')
  161.     {
  162.         j++;
  163.     }
  164.    
  165.     for(int i = j; i < answer.size(); i++)
  166.         cout << answer[i]; 
  167.    
  168.     return 0;
  169. }
Add Comment
Please, Sign In to add comment