Guest User

Untitled

a guest
Jan 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. using namespace std;
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <sstream>
  6. #include <fstream>
  7. #include <cassert>
  8. #include <climits>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <string>
  12. #include <cstdio>
  13. #include <vector>
  14. #include <cmath>
  15. #include <queue>
  16. #include <deque>
  17. #include <stack>
  18. #include <list>
  19. #include <map>
  20. #include <set>
  21.  
  22. typedef long long ll;
  23. typedef pair<int,int> way;
  24.  
  25. template <class T> string toStr(const T &x)
  26. { stringstream s; s << x; return s.str(); }
  27.  
  28. template <class T> int toInt(const T &x)
  29. { stringstream s; s << x; int r; s >> r; return r; }
  30.  
  31. #define For(i, a, b) for (int i=(a); i<(b); ++i)
  32. #define D(x) cout << #x " es " << (x) << endl
  33.  
  34. int fuel[1005];
  35. int visited[1005][1005];
  36. vector<way> dist[1005];
  37.  
  38. struct node{
  39. int cty,g,cost;
  40. node() {}
  41. node(int t, int w, int iu) : cty(t), g(w), cost(iu) {}
  42. bool operator < (const node &that) const {
  43. return cost > that.cost;
  44. }
  45. };
  46.  
  47. int dijkstra(int tam, int a, int b){
  48.  
  49. memset(visited,0,sizeof visited);
  50. priority_queue<node> q;
  51. q.push( node(a,0,0) );
  52.  
  53. while( q.size() ){
  54. int g = q.top().g;
  55. int cty = q.top().cty;
  56. int cost = q.top().cost;
  57. q.pop();
  58.  
  59. if( !visited[cty][g] ){
  60. visited[cty][g] = 1;
  61. if( cty == b ) return cost;
  62. if( (g+1) <= tam && !visited[cty][g+1] ) q.push( node(cty,g+1,cost+fuel[cty]) );
  63. for( int i = 0; i <dist[cty].size(); ++i ){
  64. int temp1 = dist[cty][i].first;
  65. int temp2 = dist[cty][i].second;
  66. if( g>=temp2 && !visited[temp1][g-temp2] ) q.push( node(temp1,g-temp2,cost) );
  67. }
  68. }
  69. }
  70. return -1;
  71. }
  72.  
  73. int main(){
  74. freopen("in.txt","r",stdin);
  75. int n,m,a,b,c,q,t,s,e;
  76. while( cin >> n >> m ){
  77. for( int i = 0; i<n; ++i ) scanf("%d",&fuel[i]), dist[i].clear();
  78. for( int i = 0; i<m; ++i ){
  79. cin >> a >> b >> c;
  80. dist[a].push_back( way(b,c) );
  81. dist[b].push_back( way(a,c) );
  82. }
  83. cin >> q;
  84. while( q-- ){
  85. cin >> t >> s >> e;
  86. int result = dijkstra(t,s,e);
  87. if( result == -1 ) cout << "impossible" << endl;
  88. else cout << result << endl;
  89. }
  90. }
  91. return 0;
  92. }
Add Comment
Please, Sign In to add comment