Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- //#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define gcd __gcd
- #define getl(s) getline(cin, s);
- #define setpre(x) fixed << setprecision(x)
- #define mset(a) memset(a, 0, sizeof(a))
- #define endl '\n'
- const int N=1050,M=1000000007;
- const ll INF=1e18+7;
- int main(){
- ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
- // freopen(".inp","r",stdin);
- // freopen(".out","w",stdout);
- ll n,m,q,x,y,z,graph[N][N];
- cin>>n>>m;
- for(int i=1;i<=n;++i){
- for(int j=1;j<=n;++j){
- if(i==j) graph[i][j]=0;
- else graph[i][j]=INF;
- }
- }
- for(int i=0;i<m;++i){
- cin>>x>>y>>z;
- graph[x][y]=z;
- }
- for(int k=1;k<=n;++k){
- for(int i=1;i<=n;++i){
- for(int j=1;j<=n;++j){
- graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]);
- }
- }
- }
- for(int i=1;i<=n;++i){
- for(int j=1;j<=n;++j){
- if(graph[i][j]==INF) graph[i][j]=-1;
- }
- }
- cin>>q;
- while(q--){
- cin>>x>>y;
- cout<<graph[x][y]<<endl;
- }
- return 0;
- }
- /*
- ==================================+
- INPUT: |
- ------------------------------ |
- ------------------------------ |
- ==================================+
- OUTPUT: |
- ------------------------------ |
- ------------------------------ |
- ==================================+
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement