jainbot27

timeline

Jul 22nd, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. /*
  2.     Notes:
  3.     A little weird of a question cause it may not be obvious to me that topo sort works :D
  4. */
  5. #pragma GCC optimize ("O3")
  6. #pragma GCC target("sse4")
  7. #include <bits/stdc++.h>
  8. #define f first
  9. #define s second
  10. #define pb push_back
  11. #define mp make_pair
  12. #define ts to_string
  13. #define ub upper_bound
  14. #define lb lower_bound
  15. const char nl = '\n';
  16. using namespace std;
  17. using ll = long long;
  18. using vi = vector<int>;
  19. using vl = vector<ll>;
  20. using pii = pair<int, int>;
  21. using pll = pair<ll, ll>;
  22. using str = string;
  23. //The following debugging code was taken from Benjamin Qi's template :)
  24. str ts(char c) { return str(1,c); }
  25. str ts(bool b) { return b ? "true" : "false"; }
  26. str ts(const char* s) { return (str)s; }
  27. str ts(str s) { return s; }
  28. template<class A> str ts(complex<A> c) {
  29.     stringstream ss; ss << c; return ss.str(); }
  30. str ts(vector<bool> v) {
  31.     str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
  32.     res += "}"; return res; }
  33. template<size_t SZ> str ts(bitset<SZ> b) {
  34.     str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
  35.     return res; }
  36. template<class A, class B> str ts(pair<A,B> p);
  37. template<class T> str ts(T v) {
  38.     bool fst = 1; str res = "{";
  39.     for (const auto& x: v) {
  40.         if (!fst) res += ", ";
  41.         fst = 0; res += ts(x);
  42.     }
  43.     res += "}"; return res;
  44. }
  45. template<class A, class B> str ts(pair<A,B> p) {
  46.     return "("+ts(p.f)+", "+ts(p.s)+")"; }
  47. void DBG() { cerr << "]" << endl; }
  48. template<class H, class... T> void DBG(H h, T... t) {
  49.     cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  50.     DBG(t...); }
  51.  
  52. #ifdef LOCAL
  53. #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  54. #else
  55. #define dbg(...) 0
  56. //freopen(".in", "r", stdin);
  57. //freopen(".out", "w", stdout);
  58. #endif
  59. //CODE STARTS HERE-----------------------------------------------------------------------------------------//
  60. const int N = 1e5 + 10;
  61. int n, m, c, ans[N], vis[N], in[N];
  62. int a[N]; vector<pii> adj[N];
  63. queue<int> bfs;
  64. signed main(){
  65.     cin.tie(nullptr);
  66.     ios_base::sync_with_stdio(false);
  67.     ifstream cin("timeline.in");
  68.     ofstream cout("timeline.out");
  69.     cin >> n >> m >> c;
  70.     for(int i = 0; i < n; i++){
  71.         cin >> a[i];
  72.     }
  73.     for(int i = 0; i < c; i++){
  74.         int x, y, u;
  75.         cin >> x >> y >> u;
  76.         //use a topological sort since you wanna process nodes with fewer edges first
  77.         x--,y--;
  78.         adj[x].pb({y,u});
  79.         in[y]++;
  80.     }
  81.     for(int i =0 ; i < n; i++)
  82.         if(!in[i])
  83.             bfs.push(i);
  84.     while(bfs.size()){
  85.         int x= bfs.front();
  86.         bfs.pop();
  87.         for(auto&y : adj[x]){
  88.             a[y.f] = max(a[y.f], a[x] + y.s);
  89.             if(--in[y.f] == 0)
  90.                 bfs.push(y.f);
  91.         }
  92.     }
  93.     for(int i =0 ; i<n; i++){
  94.         cout << a[i] << "\n";
  95.     }
  96. }
Add Comment
Please, Sign In to add comment