Advertisement
paulomiranda98

K menores caminhos

May 19th, 2021
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define all(x) x.begin(),x.end()
  5. #define endl '\n'
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. const int INF = 0x3f3f3f3f;
  12. const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
  13. const int MOD = 1000000007;
  14. const int dx[] = { 0, 0, -1, 1, 1, -1,  1, -1};
  15. const int dy[] = {-1, 1,  0, 0, 1, -1, -1,  1};
  16. const int MAXN = 200010;
  17. #include <bits/stdc++.h>
  18. using namespace std;
  19. namespace Dijkstra{
  20.   typedef long long T;
  21.   typedef pair<T, int> pti;
  22.   vector<vector<pti>> adj;
  23.   int n;
  24.   void init(int n1){
  25.     n = n1;
  26.     adj.assign(n, vector<pti>());
  27.   }
  28.   void addEdge(int from, int to, T w){
  29.     adj[from].emplace_back(w, to);
  30.   }
  31.   vector<T> solve(int s, int t, int k){  
  32.     vector<vector<T>> dist(n, vector<T>());
  33.     priority_queue<pti, vector<pti>, greater<pti>> st;
  34.     st.emplace(0, s);
  35.     while(!st.empty()){
  36.       auto [wu, u] = st.top();
  37.       st.pop();
  38.       if(dist[u].size() == k) continue;
  39.       dist[u].push_back(wu);
  40.       for(auto [w, to]: adj[u]){
  41.         st.emplace(wu + w, to);
  42.       }
  43.     }
  44.     return dist[t];
  45.   }
  46. };
  47.  
  48. int main() {
  49.   ios_base::sync_with_stdio(false); cin.tie(NULL);
  50.  
  51.   int n, m, k;
  52.   cin >> n >> m >> k;
  53.  
  54.   Dijkstra::init(n);
  55.  
  56.   for(int i=0; i<m; i++){
  57.     int a, b, c;
  58.     cin >> a >> b >> c;
  59.     a--; b--;
  60.     Dijkstra::addEdge(a, b, c);
  61.   }
  62.   vector<ll> ans = Dijkstra::solve(0, n-1, k);
  63.   for(ll x: ans)
  64.     cout << x << " ";
  65.   cout << endl;
  66.   return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement