fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <string>
  9. #include <queue>
  10. #include <stack>
  11. #include <algorithm>
  12. #include <iomanip>
  13. #define dibs reserve
  14. #define OVER9000 1234567890
  15. #define patkan 9
  16. #define tisic 47
  17. #define soclose 10e-7
  18. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  19. #define chocolate win
  20. #define ff first
  21. #define ss second
  22. #define abs(x) ((x < 0)?-(x):(x))
  23. // mylittlepony
  24. using namespace std;
  25.  
  26. int main() {
  27. cin.sync_with_stdio(0);
  28. int N,K;
  29. cin >> N >> K;
  30. map<int,pair<int,int> > X[150000];
  31. map<int,pair<int,int> > Y[150000];
  32. vector< pair<int,int> > pl(N);
  33. vector<int> F(N);
  34. for(int i =0; i < N; i++) {
  35. int x,y,f;
  36. cin >> x >> y >> f;
  37. pl[i] =make_pair(x,y);
  38. F[i] =f;
  39. X[x][y] =make_pair(i,f);
  40. Y[y][x] =make_pair(i,f);}
  41.  
  42. vector< vector<int> > D(3,vector<int>(N,-1));
  43. vector< vector<int> > ako(3,vector<int>(N,-1));
  44. D[2][0] =F[0];
  45. for(int i =0; i < 150000; i++) ALL_THE(X[i],jt) for(int j =0; j < 3; j++) if(D[j][jt->ss.ff] > 0) {
  46. pair<int,int> p =make_pair(D[j][jt->ss.ff],3*(jt->ss.ff)+j);
  47. // skok v smere x
  48. int a =p.ss/3, t =p.ss%3;
  49. auto it =X[pl[a].ff].find(pl[a].ss);
  50. it++;
  51. if(it != X[pl[a].ff].end()) {
  52. pair<int,int> r =it->ss;
  53. int d =(t == 0)?-min(K,F[a]):-K;
  54. if(p.ff+d >= 0 && D[0][r.ff] < p.ff+d+r.ss) {
  55. D[0][r.ff] =p.ff+d+r.ss;
  56. ako[0][r.ff] =(t == 0 && F[a] < K)?ako[t][a]:p.ss;}
  57. }
  58. // v smere y
  59. it =Y[pl[a].ss].find(pl[a].ff);
  60. it++;
  61. if(it != Y[pl[a].ss].end()) {
  62. pair<int,int> r =it->ss;
  63. int d =(t == 1)?-min(K,F[a]):-K;
  64. if(p.ff+d >= 0 && D[1][r.ff] < p.ff+d+r.ss) {
  65. D[1][r.ff] =p.ff+d+r.ss;
  66. ako[1][r.ff] =(t == 1 && F[a] < K)?ako[t][a]:p.ss;}
  67. }
  68. }
  69.  
  70. cout << max(D[0][N-1],D[1][N-1]) << "\n";
  71. int akt =3*(N-1)+1;
  72. if(D[0][N-1] > D[1][N-1]) akt =3*(N-1);
  73. vector<int> ans;
  74. while(akt/3 > 0) {
  75. ans.push_back(akt/3);
  76. akt =ako[akt%3][akt/3];}
  77. ans.push_back(0);
  78. cout << ans.size() << "\n";
  79. reverse(ans.begin(),ans.end());
  80. for(int i =0; i < ans.size(); i++) cout << pl[ans[i]].ff << " " << pl[ans[i]].ss << "\n";
  81. return 0;}
  82.  
  83. // look at my code
  84. // my code is amazing
Success #stdin #stdout 0.01s 10392KB
stdin
8 10
1 1 15
2 2 30
1 2 8
2 1 7
3 2 8
2 3 7
4 2 100
3 3 15
stdout
36
5
1 1
1 2
2 2
3 2
3 3