Advertisement
Guest User

Untitled

a guest
May 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. #pragma GCC optimize("Ofast")
  9. #define rep(i,a,b) for(__typeof(b)i=a;i<b;++i)
  10. #define fs first
  11. #define sc second
  12. #define pb emplace_back
  13.  
  14. typedef long long ll;
  15. typedef pair<ll,ll> pl;
  16. typedef vector<ll> vl;
  17.  
  18. const ll inf = ~((ll)1<<(ll)63);
  19. const double pi = acos(-1);
  20.  
  21.  
  22. struct graph
  23. {
  24.     vector<pl> dist;vector<bool> vis;vector<vector<pl>>edg;
  25.     graph(ll n){edg.resize(n);dist.resize(n);vis.resize(n);}
  26.     void connect1(ll v, ll u, ll w=1){edg[v].pb(pl{w,u});}
  27.     void connect(ll v, ll u, ll w=1){connect1(v,u,w);connect1(u,v,w);}
  28.     void dijk(ll);
  29. };
  30.  
  31.  
  32. void graph::dijk(ll start){
  33.     priority_queue<pair<ll,pl>,vector<pair<ll,pl>>,greater<pair<ll,pl>>>pq;
  34.     fill(dist.begin(),dist.end(),pl{inf,-1});
  35.     fill(vis.begin(),vis.end(),false);
  36.     pq.push(pair<ll,pl>{0,pl{start,start}});dist[start]=pl{0,start};
  37.     while(!pq.empty())
  38.     {
  39.         pair<ll,pl> cur=pq.top();pq.pop();if(vis[cur.sc.fs])continue;
  40.         dist[cur.sc.fs]=pl{cur.fs,cur.sc.sc};vis[cur.sc.fs]=true;
  41.         rep(i, 0, edg[cur.sc.fs].size())
  42.         {
  43.             pl nxt=edg[cur.sc.fs][i];ll weight=nxt.fs,vertex_index=nxt.sc;
  44.             if(!vis[vertex_index])pq.push(pair<ll,pl>{weight+cur.fs,pl{vertex_index,cur.sc.fs}});
  45.         }
  46.     }
  47. }
  48.  
  49.  
  50. ll val(char c)
  51. {
  52.     if(c=='#')return -1;
  53.     if(c=='S')return 1;
  54.     if(c=='G')return 1;
  55.     if(c=='.')return 1;
  56.     if(c=='F')return 2;
  57.     if(c=='M')return 3;
  58.     return inf;
  59. }
  60.  
  61.  
  62. int main()
  63. {
  64.     ios_base::sync_with_stdio(false);
  65.     cin.tie(nullptr);
  66.  
  67.     ll n,m,k,s,G;
  68.     cin >> n >> m >> k;
  69.     graph g(n*m);
  70.     vector<vector<char>> grid(m,vector<char>(n));
  71.     vector<char> ref(n*m);
  72.     rep(y,0,n)
  73.     {
  74.         rep(x,0,m)
  75.         {
  76.             cin >> grid[x][y];
  77.             ref[y*m+x] = grid[x][y];
  78.             if(grid[x][y]=='S')s=y*m+x;
  79.             else if(grid[x][y]=='G')G=y*m+x;
  80.         }
  81.     }
  82.  
  83.     rep(i,0,m)
  84.     {
  85.         rep(j,0,n)
  86.         {
  87.             ll w;
  88.             if(i!=0)
  89.             {
  90.                 w = val(grid[i-1][j]);
  91.                 if(w!=-1)g.connect1(j*m+i,j*m+i-1,w);
  92.             }
  93.             if(i<m-1)
  94.             {
  95.                 w = val(grid[i+1][j]);
  96.                 if(w!=-1)g.connect1(j*m+i,j*m+i+1,w);
  97.             }
  98.             if(j!=0)
  99.             {
  100.                 w = val(grid[i][j-1]);
  101.                 if(w!=-1)g.connect1(j*m+i,(j-1)*m+i,w);
  102.             }
  103.             if(j<n-1)
  104.             {
  105.                 w = val(grid[i][j+1]);
  106.                 if(w!=-1)g.connect1(j*m+i,(j+1)*m+i,w);
  107.             }
  108.         }
  109.     }
  110.     g.dijk(s);
  111.     pl d = g.dist[G];
  112.     cout << d.fs << " " << d.sc << endl;
  113.     if(d.fs==inf)
  114.     {
  115.         cout << -1;
  116.         return 0;
  117.     }
  118.     vector<ll> path;
  119.     path.reserve(n*m);
  120.     path.pb(G);
  121.     path.pb(d.sc);
  122.     while(true)
  123.     {
  124.         d = g.dist[d.sc];
  125.         if(d.sc != path[path.size()-1])path.pb(d.sc);
  126.         else break;
  127.     }
  128.     ll ku = k,days=1;
  129.     for(ll i = path.size();i--;)
  130.     {
  131.         if(path[i]!=G)
  132.         {
  133.             if(val(ref[path[-1]])>ku&&ku==k)
  134.             {
  135.                 cout << -1;
  136.                 return 0;
  137.             }
  138.             else if(val(ref[path[-1]])>ku&&val(ref[path[-1]])<=k)
  139.             {
  140.                 days++;
  141.                 ku -= val(ref[path[-1]]);
  142.             }
  143.             else if(val(ref[path[-1]])<=ku)
  144.             {
  145.                 ku-=val(ref[path[-1]]);
  146.             }
  147.             else{
  148.                 cout << -1;
  149.                 return 0;
  150.             }
  151.         }
  152.         else{
  153.             break;
  154.         }
  155.     }
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement