This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define endl "\n"
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz size()
#define rep(i,k,n) for(int i = k ; i <= n ; ++i)
#define per(i,k,n) for(int i = k ; i >= n ; --i)
#define Zymraq() ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(x) x.begin(),x.end()
#define fr(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define toqta return 0
#define PERMUTE next_permutation
#define no cout<<"No"<<endl;
#define yes cout<<"Yes"<<endl;
#define a() a + 1, a + n + 1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef string S;
typedef double ld;
typedef long double lld;
const int N = 1e6 + 17;
const int modd = 1e9 + 7;
const int INF = 1e9 - 19;
const int P = 37;
const ll NN = 1e7 + 17;
const ld eps = 1e-19;
const double pi = 3.141592653589793238462643383279 ;
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b){
return (a.second < b.second);
}
void pre (ll a) {
cout << fixed << setprecision(a);
return;
}
ll n, m, k, q;
ll used[10005][10005];
vector <ll> g[N];
vector <ll> npp;
ll dp[10500][10500];
ll timer = 0;
ll s, t;
ll col[N];
ll ans, ans_now[10500];
inline void Solution () {
cin >> n >> m;
for (int i = 1; i <= m; ++ i) {
ll x, y, dst;
cin >> x >> y >> dst;
g[x].pb (y);
g[y].pb (x);
dp[x][y] = dst;
dp[y][x] = dst;
}
for (int k = 1; k <= n; ++ k) {
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= n; ++ j) {
if (i == j || dp[i][k] == 0 || dp[k][j] == 0) continue;
dp[i][j] = min (dp[i][j], dp[i][k] + dp[k][j]);
if (!dp[i][j]) dp[i][j] = dp[i][k] + dp[k][j];
}
}
}
cin >> k;
for (int i = 1; i <= k; ++ i) {
ll x;
cin >> x;
npp.pb (x);
}
cin >> q;
while (q --) {
timer ++;
cin >> s >> t;
ans = INF;
for (auto i: npp) {
ans = min (dp[i][s], ans);
ans = min (dp[i][t], ans);
}
cout << ans << '\n';
}
return;
}
int main () {
Zymraq();
Solution ();
exit (0);
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |