Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. #define ll long long
  10. #define ul unsigned long long
  11. #define pii pair<int,int>
  12. #define mp make_pair
  13. #define pb push_back
  14. #define fi first
  15. #define se second
  16. #define REP(i, n) for (int (i) = 0; (i) < (n); (i) ++)
  17. #define REP1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
  18.  
  19. struct  ed
  20. {
  21.     int a, b;
  22.     ll cost;
  23. };
  24.  
  25. struct node
  26. {
  27.     int num;
  28.     vector < pair<int, ll> > ch;
  29. };
  30.  
  31. struct ans
  32. {
  33.     ll len;
  34.     char c;
  35.     ans()
  36.     {
  37.         c = 0;
  38.     }
  39. };
  40.  
  41. const ll INF = 9223372036854775807LL;
  42. vector<node> g;
  43. vector<bool> used, temp_used;
  44. int ind[2005];
  45. vector<ed> e;
  46. vector<ans> rez;
  47. vector<int> tt;
  48. ll temp_a[2005][2005];
  49. int kvet = 0;
  50. int s;
  51.  
  52. bool IsNotUsed(const node& t)
  53. {
  54.     return (!used[t.num]);//&& (t.num != s));
  55. }
  56. bool IsNotUsed_2(pair<int, ll> t)
  57. {
  58.     return (!used[t.first]);
  59. }
  60.  
  61. void bfs(int u)
  62. {
  63.     queue<int> q;
  64.     q.push(u);
  65.     //used[ind[u]] = true;
  66.     used[u] = true;
  67.     while (!q.empty())
  68.     {
  69.         int t = q.front();
  70.         t = ind[t];
  71.         q.pop();
  72.         REP(i, g[t].ch.size())
  73.         {
  74.             if (!used[g[t].ch[i].first])
  75.             {
  76.                 used[g[t].ch[i].first] = true;
  77.                 q.push(g[t].ch[i].first);
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83. void ford()
  84. {
  85.     if (g.empty())
  86.         return;
  87.     vector<ll> d(2005, INF);
  88.     vector<int> p(2005, -1);
  89.     int x;
  90.     d[s] = 0;
  91.     REP(i, g.size())
  92.     {
  93.         x = -1;
  94.         REP(j, e.size())
  95.         {
  96.             if (d[e[j].a] < INF)
  97.             {
  98.                 if (d[e[j].b] > d[e[j].a] + e[j].cost)
  99.                 {
  100.                     d[e[j].b] = max(-INF, d[e[j].a] + e[j].cost);
  101.                     p[e[j].b] = e[j].a;
  102.                     x = e[j].b;
  103.                 }
  104.             }
  105.         }
  106.     }
  107.     if (x == -1)
  108.     {
  109.         REP(i, g.size())
  110.         {
  111.             rez[g[i].num].c = 0;
  112.             rez[g[i].num].len = d[g[i].num];
  113.         }
  114.         return;
  115.     }
  116.     else
  117.     {
  118.         int y = x;
  119.         REP(i, 2004)
  120.             y = p[y];
  121.         /*int k = 0;
  122.         for (int cur = y;; cur = p[cur])
  123.         {
  124.             k++;
  125.             if (cur == y && k > 1)
  126.             {
  127.                 y = cur;
  128.                 break;
  129.             }
  130.         }*/
  131.         temp_used = used;
  132.         fill(used.begin(), used.end(), false);
  133.         REP(i, g.size())
  134.             ind[g[i].num] = i;
  135.         bfs(y);
  136.         REP(i, g.size())
  137.         {
  138.             if (used[g[i].num])
  139.                 rez[g[i].num].c = '-';
  140.             used[g[i].num] = !used[g[i].num];
  141.         }
  142.         g.erase(remove_if(g.begin(), g.end(), (IsNotUsed)), g.end());
  143.         REP(i, g.size())
  144.             ind[g[i].num] = i;
  145.         e.clear();
  146.         REP(i, g.size())
  147.         {
  148.             REP(j, g[i].ch.size())
  149.             {
  150.                 if (used[g[i].ch[j].first])
  151.                 {
  152.                     ed ee;
  153.                     ee.a = g[i].num;
  154.                     ee.b = g[i].ch[j].first;
  155.                     ee.cost = g[i].ch[j].second;
  156.                     e.push_back(ee);
  157.                 }
  158.             }
  159.         }
  160.         ford();
  161.     }
  162. }
  163.  
  164.  
  165. int main()
  166. {
  167.     int m, n;
  168.     freopen("path.in", "r", stdin);
  169.     freopen("path.out", "w", stdout);
  170.     scanf("%d %d %d", &n, &m, &s);
  171.     g.resize(n);
  172.     rez.resize(n);
  173.     used.resize(n);
  174.     s--;
  175.     int a, b;
  176.     ll c;
  177.     REP(i, n)
  178.         g[i].num = i;
  179.    
  180.     REP(i, 2003)
  181.         REP(j, 2003)
  182.         temp_a[i][j] = INF;
  183.    
  184.     REP(i, m)
  185.     {
  186.         cin >> a >> b >> c;
  187.         a--;
  188.         b--;
  189.         temp_a[a][b] = min(temp_a[a][b], c);
  190.     }
  191.     REP(i, 2003)
  192.         REP(j, 2003)
  193.     {
  194.             if (temp_a[i][j] != INF)
  195.                 g[i].ch.push_back(mp(j, temp_a[i][j]));
  196.         }
  197.     REP(i, g.size())
  198.         ind[g[i].num] = i;
  199.  
  200.     bfs(s);
  201.    
  202.     REP(i, n)
  203.     {
  204.         if (!used[i])
  205.             rez[i].c = '*';
  206.     }
  207.    
  208.     g.erase(remove_if(g.begin(), g.end(), (IsNotUsed)), g.end());
  209.     REP(i, g.size())
  210.         ind[i] = g[i].num;
  211.  
  212.     REP(i, g.size())
  213.     {
  214.         REP(j, g[i].ch.size())
  215.         {
  216.             ed ee;
  217.             ee.a = g[i].num;
  218.             ee.b = g[i].ch[j].first;
  219.             ee.cost = g[i].ch[j].second;
  220.             e.push_back(ee);
  221.         }
  222.     }
  223.    
  224.     ford();
  225.    
  226.     REP(i, rez.size())
  227.     {
  228.         if (rez[i].c == 0)
  229.             cout << rez[i].len;
  230.         else
  231.             cout << rez[i].c;
  232.         cout << "\n";
  233.     }
  234.    
  235.     //wait;
  236.     return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement