Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- int n,m;
- const ld inf = 2e9;
- const int mx_way = 5e4 + 10;
- vector <queue <int> > waves;
- vector <ld> d(100);
- struct ed{
- int fr, to;
- ld w;
- };
- vector <vector <ed>> G;
- //map
- void readG(){
- cin>>n>>m;
- G.resize(n);
- //cout<<"n m = "<<n<<' '<<m<<"\n";
- for (int i=0;i<m;++i){
- int a,b;
- ld w;
- cin>>a>>b>>w;
- a--; b--;
- w /= 1000.0;
- ed any = {a, b, w};
- G[a].push_back(any);
- }
- //cout<<"done\n";
- }
- void shG(){
- cout<<"G\n";
- for (int i=0;i<n;++i){
- cout<<i+1<<"\n";
- for (auto x: G[i]){
- cout<<x.to+1<<' '<<fixed<<x.w<<"\n";
- }
- cout<<"\n";
- }
- }
- void cvld(vector <ld> v){
- for (int i=0;i<v.size();++i)cout<<v[i]<<' ';
- cout<<"\n\n";
- }
- void fndWaves(int s){
- d.assign(n, inf);
- waves.resize(mx_way);
- d[s] = 0;
- waves[0].push(s);
- for (int l = 0; l < mx_way; ++l){
- //cout<<"l = "<<l<<"\n";
- while (!waves[l].empty()){
- int v = waves[l].front();
- waves[l].pop();
- //cout<<"l = "<<l<<"\n";
- //cout<<"v = "<<v+1<<"\n";
- for (ed e: G[v]){
- int nv = e.to;
- ld nl = d[v] + e.w;
- //cout<<"e = "<<e.fr+1<<' '<<e.to+1<<' '<<fixed<<e.w<<"\n";
- if (d[nv] > nl){
- //if (d[e.to] != inf){
- // waves[l].erase(e.to);
- //}
- d[nv] = nl;
- //cout<<"l e.w = "<<l<<' '<<e.w<<"\n";
- //cout<<"d[e.to] = "<<d[e.to]<<"\n";
- waves[(int)floor(nl)].push(e.to);
- }
- }
- }
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout.precision(5);
- readG();
- /*int fr; cin>>fr;
- fndWaves(fr);
- //shG();
- cout<<"dst\n";
- cvld(d);*/
- int k=1;
- cin>>k;
- for (int i=0;i<k;++i){
- int a,b; cin>>a>>b;
- a--; b--;
- fndWaves(a);
- //cout<<"d\n";
- //cvld(d);
- //cvld(d);
- if (d[b] == inf){
- cout<<-1<<"\n";
- continue;
- }
- ll ans = (ll)roundl(d[b] * 1000.0);
- //cout<<"ans = "<<ans<<"\n";
- //cout<<ans<<"\n";
- //cout<<d[b]<<' '<<ans<<"\n";
- cout<<ans<<"\n";
- }
- }
- /*
- 3 3
- 1 2 1001
- 2 3 1257
- 3 1 1782
- 10
- 5 5
- 1 2 2000
- 1 3 1000
- 1 4 1200
- 2 3 1500
- 3 4 1500
- 4
- 1 5
- 2 4
- 3 3
- 1 2
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement