fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define REP(i, a, b) for(int i = (int)a, _b = (int)b; i < _b; ++ i)
  5. #define PER(i, a, b) for(int i = (int)a, _b = (int)b; i > _b; -- i)
  6. #define TRcon(a, it) for(auto it = (a).begin(); it != (a).end(); ++ it)
  7. #define pb push_back
  8. #define mp make_pair
  9. #define all(a) (a.begin(), a.end())
  10. #define sz(a) ((int) a.size())
  11. #define ms(a, val) memset(a, val, sizeof (a))
  12. #define INF 1000000000
  13. #define inp(a) scanf("%d", &a)
  14. #define inpp(a, b) scanf("%d %d", &a, &b)
  15. #define inppp(a, b, c) scanf("%d %d %d", &a, &b, &c)
  16. #define readfrom(a) freopen(a, "r", stdin)
  17. #define writeto(a) freopen(a, "w", stdout)
  18.  
  19.  
  20. typedef long long ll;
  21. typedef unsigned long long ull;
  22. typedef pair<int, int> ii;
  23. typedef pair<int, ii> iii;
  24. typedef pair<string, string> ss;
  25. typedef vector<int> vi;
  26. typedef vector<bool> vb;
  27. typedef vector<ii> vii;
  28. typedef vector<string> vs;
  29. typedef set <int> si;
  30. typedef map<int, int> mii;
  31. typedef map<ii, int> mpi;
  32. typedef map<string, int> msi;
  33.  
  34. const int N = 151;
  35.  
  36. vii G[N];
  37. bool vis[N];
  38. int V, E, Q;
  39.  
  40. struct m {
  41. int a, b, c, d;
  42. m() {}
  43. m(int _a, int _b, int _c, int _d) {
  44. a = _a, b = _b, c = _c, d = _d;
  45. }
  46. bool operator< (const m &o) const {
  47. return a < o.a;
  48. }
  49. };
  50.  
  51. map <m, int> mem;
  52.  
  53. int DFS(int u, int pd, int dist, int C, int des) {
  54. if (C == 0) {
  55. if (u == des)
  56. return dist;
  57. return INF;
  58. }
  59. //cout << u + 1 << endl;
  60. if (u == des)
  61. return dist;
  62. int ans = INF;
  63. TRcon(G[u], v) {
  64. if (!vis[v->first]) {
  65. if (v->second >= pd) {
  66. vis[v->first] = true;
  67. ans = min(ans, DFS(v->first, v->second, dist + v->second, C - 1, des));
  68. vis[v->first] = false;
  69. }
  70. }
  71. }
  72. return ans;
  73. }
  74.  
  75. map <iii, int> yrab;
  76.  
  77. int main(void) {
  78. //readfrom("in.txt");
  79. //writeto("out.txt");
  80. int TC; inp(TC);
  81. while (TC--) {
  82. inppp(V, E, Q);
  83. REP(i, 0, V + 1) {
  84. G[i].clear();
  85. }
  86. REP(i, 0, E) {
  87. int u, v, w; inppp(u, v, w);
  88. G[u - 1].push_back(ii(v - 1, w));
  89. }
  90. REP(i, 0, Q) {
  91. int A, B, C; inppp(A, B, C);
  92. int ans = DFS(A - 1, 0, 0, C, B - 1);
  93. printf(ans == INF ? "-1\n" : "%d\n", ans);
  94. }
  95. }
  96. return 0;
  97. }
  98.  
Time limit exceeded #stdin #stdout 5s 3340KB
stdin
Standard input is empty
stdout
Standard output is empty