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<iostream>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define ii pair<ll, int>
#define task "test"
const int inf = 1e9 + 7;
const ll infll = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int N = 2e6 + 3;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
int k,n,m,q;
int par[N], s[N];
int u[N], v[N];
vector<ii> adj[N];
struct ha {
int x,y,z;
};
ha a[N];
bool sor(const ha X, const ha Y) {
return X.x < Y.x;
}
void solve(int x,int y) {
vector<ll> d(n+1, inf);
d[x] = 0;
priority_queue< ii, vector<ii>, greater<ii> > p;
p.push(mp(0,x));
while(!p.empty()) {
int u = p.top().se;
int val = p.top().fi; p.pop();
if(val != d[u]) continue;
for(ii X : adj[u]) {
int v = X.se;
int he = X.fi;
if(d[v] > val + he) {
d[v] = val + he;
p.push(mp(d[v], v));
}
}
}
if(d[y] == inf) cout<<-1<<'\n'; else cout<<d[y]<<'\n';
}
int getroot(int x) {
if(par[x] == x) return x;
return par[x] = getroot(par[x]);
}
void join(int x,int y) {
x = getroot(x);
y = getroot(y);
if(x == y) return;
par[x] = y;
s[y] = s[x] + adj[x][0].fi;
}
void sub1() {
for(int i=1;i<=n;i++) par[i] = i;
for(int i=1;i<=m;i++) {
//cout<<a[i].x<<" "<<a[i].y<<'\n';
join(a[i].x, a[i].y);
}
for(int i=1;i<=q;i++) {
if(getroot(u[i]) != getroot(v[i])) cout<<-1<<'\n'; else
cout<<s[v[i]] - s[u[i]]<<'\n';
}
//cout<<s[4];
}
void sub2() {
vector<ll> d(n+1, inf);
d[0] = 0;
priority_queue< ii, vector<ii>, greater<ii> > p;
p.push(mp(0,0));
while(!p.empty()) {
int u = p.top().se;
int val = p.top().fi; p.pop();
if(val != d[u]) continue;
for(ii X : adj[u]) {
int v = X.se;
int he = X.fi;
if(d[v] > val + he) {
d[v] = val + he;
p.push(mp(d[v], v));
}
}
}
for(int i=1;i<=q;i++) {
if(d[v[i]] == inf) cout<<-1<<'\n'; else cout<<d[v[i]]<<'\n';
}
}
void bruteforce() {
for(int i=1;i<=q;i++) {
solve(u[i], v[i]);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen(task".inp" , "r" , stdin);
// freopen(task".out" , "w" , stdout);
cin>>k>>n>>m>>q;
for(int i=1;i<=m;i++) {
int u,v,w; cin>>u>>v>>w;
a[i] = {u,v,w};
adj[u].pb(mp(w,v));
//join(u,v);
}
sort(a+1,a+1+m,sor);
int count_sub2 = 0;
for(int i=1;i<=q;i++) {
cin>>u[i]>>v[i];
if(u[i] == 0) count_sub2++;
}
if(k == 1) sub1(); else
if(count_sub2 == q) sub2(); else
bruteforce();
return 0;
}
/* 5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13 */
# | 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... |