Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- using namespace std;
- #pragma GCC optimize("Ofast")
- #define rep(i,a,b) for(__typeof(b)i=a;i<b;++i)
- #define fs first
- #define sc second
- #define pb emplace_back
- typedef long long ll;
- typedef pair<ll,ll> pl;
- typedef vector<ll> vl;
- const ll inf = ~((ll)1<<(ll)63);
- const double pi = acos(-1);
- struct graph
- {
- vector<pl> dist;vector<bool> vis;vector<vector<pl>>edg;
- graph(ll n){edg.resize(n);dist.resize(n);vis.resize(n);}
- void connect1(ll v, ll u, ll w=1){edg[v].pb(pl{w,u});}
- void connect(ll v, ll u, ll w=1){connect1(v,u,w);connect1(u,v,w);}
- void dijk(ll);
- };
- void graph::dijk(ll start){
- priority_queue<pair<ll,pl>,vector<pair<ll,pl>>,greater<pair<ll,pl>>>pq;
- fill(dist.begin(),dist.end(),pl{inf,-1});
- fill(vis.begin(),vis.end(),false);
- pq.push(pair<ll,pl>{0,pl{start,start}});dist[start]=pl{0,start};
- while(!pq.empty())
- {
- pair<ll,pl> cur=pq.top();pq.pop();if(vis[cur.sc.fs])continue;
- dist[cur.sc.fs]=pl{cur.fs,cur.sc.sc};vis[cur.sc.fs]=true;
- rep(i, 0, edg[cur.sc.fs].size())
- {
- pl nxt=edg[cur.sc.fs][i];ll weight=nxt.fs,vertex_index=nxt.sc;
- if(!vis[vertex_index])pq.push(pair<ll,pl>{weight+cur.fs,pl{vertex_index,cur.sc.fs}});
- }
- }
- }
- ll val(char c)
- {
- if(c=='#')return -1;
- if(c=='S')return 1;
- if(c=='G')return 1;
- if(c=='.')return 1;
- if(c=='F')return 2;
- if(c=='M')return 3;
- return inf;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- ll n,m,k,s,G;
- cin >> n >> m >> k;
- graph g(n*m);
- vector<vector<char>> grid(m,vector<char>(n));
- vector<char> ref(n*m);
- rep(y,0,n)
- {
- rep(x,0,m)
- {
- cin >> grid[x][y];
- ref[y*m+x] = grid[x][y];
- if(grid[x][y]=='S')s=y*m+x;
- else if(grid[x][y]=='G')G=y*m+x;
- }
- }
- rep(i,0,m)
- {
- rep(j,0,n)
- {
- ll w;
- if(i!=0)
- {
- w = val(grid[i-1][j]);
- if(w!=-1)g.connect1(j*m+i,j*m+i-1,w);
- }
- if(i<m-1)
- {
- w = val(grid[i+1][j]);
- if(w!=-1)g.connect1(j*m+i,j*m+i+1,w);
- }
- if(j!=0)
- {
- w = val(grid[i][j-1]);
- if(w!=-1)g.connect1(j*m+i,(j-1)*m+i,w);
- }
- if(j<n-1)
- {
- w = val(grid[i][j+1]);
- if(w!=-1)g.connect1(j*m+i,(j+1)*m+i,w);
- }
- }
- }
- g.dijk(s);
- pl d = g.dist[G];
- cout << d.fs << " " << d.sc << endl;
- if(d.fs==inf)
- {
- cout << -1;
- return 0;
- }
- vector<ll> path;
- path.reserve(n*m);
- path.pb(G);
- path.pb(d.sc);
- while(true)
- {
- d = g.dist[d.sc];
- if(d.sc != path[path.size()-1])path.pb(d.sc);
- else break;
- }
- ll ku = k,days=1;
- for(ll i = path.size();i--;)
- {
- if(path[i]!=G)
- {
- if(val(ref[path[-1]])>ku&&ku==k)
- {
- cout << -1;
- return 0;
- }
- else if(val(ref[path[-1]])>ku&&val(ref[path[-1]])<=k)
- {
- days++;
- ku -= val(ref[path[-1]]);
- }
- else if(val(ref[path[-1]])<=ku)
- {
- ku-=val(ref[path[-1]]);
- }
- else{
- cout << -1;
- return 0;
- }
- }
- else{
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement