Advertisement
Dang_Quan_10_Tin

Untitled

Feb 25th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NguyenDangQuan the_author
  3.  
  4. #define task ""
  5. #define all(x) x.begin(),x.end()
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. #define ld long double
  10. #define ll long long
  11. #define ull unsigned long long
  12.  
  13. using namespace std;
  14.  
  15. int typetest;
  16.  
  17. inline void fastIOfileinput(){
  18.     ios_base:: sync_with_stdio(0);
  19.     cin.tie(0);
  20.     cout.tie(0);
  21.     if(fopen(task".inp", "r")){
  22.         freopen(task".inp", "r", stdin);
  23.         freopen(task".out", "w", stdout);
  24.     }
  25.     if(fopen(task".in", "r")){
  26.         freopen(task".in", "r", stdin);
  27.         freopen(task".out", "w", stdout);
  28.     }
  29.     typetest = 0;
  30. }
  31.  
  32. struct dat{
  33.     int id;
  34.     ll w;
  35.     dat() {}
  36.     dat(int u, ll v){
  37.         this->id = u;
  38.         this->w = v;
  39.     }
  40.     bool operator <(const dat& a) const{
  41.         return w > a.w
  42.             ||(w == a.w && id > a.id);
  43.     }
  44. };
  45.  
  46. int n, m, t;
  47. vector <dat> ke[501];
  48. bool on[501];
  49.  
  50. inline void Enter(){
  51.     memset(on, 0, sizeof(on));
  52.     cin >> n >> m >> t;
  53.     for(int i = 1; i <= m; ++i){
  54.         int a, b;
  55.         ll c;
  56.         cin >> a >> b >> c;
  57.         ke[a].pb(dat(b, c));
  58.     }
  59. }
  60.  
  61. ll BFS(int x, int y){
  62.     bool free[n + 1];
  63.     memset(free, 1, sizeof(free));
  64.     priority_queue<dat, vector<dat> > open;
  65.     open.push(dat(x, 0));
  66.     while( !open.empty() ) {
  67.         auto c = open.top();
  68.         open.pop();
  69.         free[c.id] = 1;
  70.         if(c.id == y) return c.w;
  71.         for(auto i : ke[c.id]){
  72.             if(free[i.id] || (!on[i.id] && i.id != y) ) continue;
  73.             open.push(dat(i.id, i.w + c.w));
  74.         }
  75.     }
  76.     return -1;
  77. }
  78.  
  79. inline void solve(){
  80.     int typ;
  81.     while(t--){
  82.         cin >> typ;
  83.         if(typ == 1){
  84.             cin >> typ;
  85.             on[typ] = 1;
  86.             continue;
  87.         }
  88.         int x, y;
  89.         cin >> x >> y;
  90.         ll ans = BFS(x, y);
  91.         cout << ans << "\n";
  92.     }
  93. }
  94.  
  95. int main(){
  96.     fastIOfileinput();
  97.     if(typetest){
  98.         int t;
  99.         cin >> t;
  100.         while(t--){
  101.             Enter();
  102.             solve();
  103.         }
  104.     }
  105.     else{
  106.         Enter();
  107.         solve();
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement