Guest User

Untitled

a guest
May 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 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. const int INF = 1000000000;
  27.  
  28. int used[3000];
  29.  
  30. int am[5000];
  31. int bm[5000];
  32. int rm[5000];
  33.  
  34. string d[3000];
  35.  
  36. int ans = 1e9;
  37.  
  38. string sum(const string &a1, const string &b1)
  39. {
  40.    string a = a1;  
  41.     string b = b1;
  42.                
  43.     //длинное сложение
  44.     //cerr << a << endl << b << endl;
  45.            
  46.     for(int i = 0; i < 2100; i++)
  47.     {
  48.         am[i] = 0;
  49.         bm[i] = 0;
  50.         rm[i] = 0;
  51.     }
  52.    
  53.     int u1 = 1;
  54.     int u2 = 1;
  55.    
  56.     if(a.size() > b.size())
  57.     {
  58.         int r = a.size() - b.size();
  59.        
  60.         u2 += r;
  61.     }
  62.    
  63.     if(a.size() < b.size())
  64.     {
  65.         int r = b.size() - a.size();
  66.        
  67.         u1 += r;
  68.     }
  69.  
  70.     //cerr << u1 << " " << u2 << endl;
  71.    
  72.     for(int i = u1; i < u1 + a.size(); i++)
  73.         am[i] = a[i - u1] - '0';
  74.    
  75.     for(int i = u2; i < u2 + b.size(); i++)
  76.         bm[i] = b[i - u2] - '0';
  77.    
  78.     for(int i = 0; i < u2 + b.size(); i++)
  79.         rm[i] = am[i] + bm[i];
  80.    
  81.     for(int i = u2 + b.size(); i > 0; i--)
  82.     {
  83.         if(rm[i] >= 10)
  84.         {
  85.             rm[i] -= 10;
  86.             rm[i - 1]++;
  87.         }
  88.     }
  89.        
  90.     string answer = "";
  91.    
  92.     int flag = 0;
  93.    
  94.     for(int i = 0; i < u1 + a.size(); i++)
  95.     {
  96.         if(rm[i] != 0)
  97.             flag = 1;
  98.            
  99.         if(flag == 1)
  100.             answer.push_back( char( int(rm[i]) + int('0') ) ); 
  101.     }
  102.        
  103.     //cerr << answer << endl;
  104.        
  105.     return answer;
  106. }
  107.  
  108.  
  109. string rec(int x)
  110. {
  111.     if(d[x] == "")
  112.     {
  113.    
  114.         if(x == s)
  115.         {
  116.             return("1");
  117.         }  
  118.  
  119.         string  res = "00000000";
  120.    
  121.         for(int i = 0; i < g[x].size(); i++)
  122.         {
  123.             if(used[g[x][i].first] == used[x] - g[x][i].second)
  124.             {
  125.                 //cerr << "(" << res << " "  << rec(g[x][i].first) << ")" << endl;
  126.                 res = sum(res, rec(g[x][i].first) );
  127.             }
  128.         }
  129.  
  130.         d[x] = res;
  131.         return res;
  132.     }
  133.     else
  134.         return(d[x]);
  135. }
  136. int main ()
  137. {
  138.                    
  139.    
  140.     int n, m;
  141.    
  142.     cin >> n >> m;
  143.    
  144.     for(int i = 0; i < 2000; i++)
  145.         used[i] = 1e8;
  146.    
  147.     for(int i = 0; i < m; i++)
  148.     {
  149.         int a, b, c;
  150.         scanf("%d%d%d", &a, &b, &c);
  151.        
  152.         a--;
  153.         b--;
  154.        
  155.         g[a].push_back(make_pair(b, c));
  156.         g[b].push_back(make_pair(a, c));
  157.     }
  158.    
  159.     cin >> s >> f;
  160.     s--;
  161.     f--;
  162.    
  163.     vector<int> dist (n, INF),  p (n);
  164.     dist[s] = 0;
  165.     vector<char> u (n);
  166.    
  167.     for (int i=0; i<n; ++i) {
  168.         int v = -1;
  169.         for (int j=0; j<n; ++j)
  170.             if (!u[j] && (v == -1 || dist[j] < dist[v]))
  171.                 v = j;
  172.         if (dist[v] == INF)
  173.             break;
  174.         u[v] = true;
  175.  
  176.         for (size_t j=0; j<g[v].size(); ++j) {
  177.             int to = g[v][j].first,
  178.                 len = g[v][j].second;
  179.             if (dist[v] + len < dist[to]) {
  180.                 dist[to] = dist[v] + len;
  181.                 p[to] = v;
  182.             }
  183.         }
  184.     }
  185.    
  186.     for(int i = 0; i < n; i++)
  187.         used[i] = dist[i];
  188.    
  189.    
  190.     if(used[f] == INF)
  191.     {
  192.         cout << 0;
  193.         return 0;
  194.     }
  195.    
  196.     int flag = 0;
  197.     //return 0;
  198.    
  199.     string answer = rec(f);
  200.    
  201.     for(int i = 0; i < answer.size(); i++)
  202.     {
  203.         if(flag == 0 && answer[i] != '0')
  204.         {
  205.             flag = 1;
  206.         }
  207.    
  208.         if(flag == 1)
  209.             cout << answer[i];
  210.     }
  211.    
  212.     //cout << rec(f);
  213.    
  214.     return 0;
  215. }
Add Comment
Please, Sign In to add comment