/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
map<pair<int,int>,int>d;
int const N=5e4+10;
vector<pair<int,int>>nei[N]={};
int dist[N]={};
bool vis[N]={};
int bfs(int n,int tar)
{
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
pq.push({0,n});
dist[n]=0;
while (pq.size())
{
int z=pq.top().second;
pq.pop();
if (vis[z])
continue;
if (z==tar)
return dist[z];
vis[z]=1;
for (auto [j,w]:nei[z])
{
if (dist[j]>dist[z]+w)
{
dist[j]=dist[z]+w;
pq.push({dist[j],j});
}
}
}
return -1;
}
inline void solve()
{
int n,m,k,o;
cin>>k>>n>>m>>o;
while (m--)
{
int a,b,t;
cin>>a>>b>>t;
nei[a].push_back({b,t});
}
vector<pair<int,int>>ord;
bool w=1;
while (o--)
{
int x,y;
cin>>x>>y;
w=(w&&(x==0));
ord.push_back({x,y});
}
if (w)
{
for (int i=0;i<n;i++)
dist[i]=1e9+10,vis[i]=0;
bfs(0,-1);
for (auto i:ord)
cout<<(dist[i.second]==1e9+10?-1:dist[i.second])<<'\n';
}
else
{
for (auto i:ord)
{
for (int j=0;j<n;j++)
dist[j]=1e9+10,vis[j]=0;
cout<<bfs(i.first,i.second)<<'\n';
}
}
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3017 ms |
3900 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3480 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
8 |
Correct |
3 ms |
1884 KB |
Output is correct |
9 |
Correct |
13 ms |
3504 KB |
Output is correct |
10 |
Correct |
37 ms |
4444 KB |
Output is correct |
11 |
Correct |
26 ms |
3676 KB |
Output is correct |
12 |
Correct |
18 ms |
3416 KB |
Output is correct |
13 |
Correct |
38 ms |
4444 KB |
Output is correct |
14 |
Correct |
24 ms |
3408 KB |
Output is correct |
15 |
Correct |
20 ms |
3164 KB |
Output is correct |
16 |
Correct |
20 ms |
2928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1624 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
6 ms |
1628 KB |
Output is correct |
9 |
Correct |
5 ms |
1708 KB |
Output is correct |
10 |
Correct |
18 ms |
3420 KB |
Output is correct |
11 |
Correct |
111 ms |
3412 KB |
Output is correct |
12 |
Correct |
173 ms |
4332 KB |
Output is correct |
13 |
Correct |
187 ms |
4596 KB |
Output is correct |
14 |
Correct |
150 ms |
3928 KB |
Output is correct |
15 |
Correct |
105 ms |
3072 KB |
Output is correct |
16 |
Correct |
87 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1624 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
6 ms |
1628 KB |
Output is correct |
9 |
Correct |
5 ms |
1708 KB |
Output is correct |
10 |
Correct |
18 ms |
3420 KB |
Output is correct |
11 |
Correct |
111 ms |
3412 KB |
Output is correct |
12 |
Correct |
173 ms |
4332 KB |
Output is correct |
13 |
Correct |
187 ms |
4596 KB |
Output is correct |
14 |
Correct |
150 ms |
3928 KB |
Output is correct |
15 |
Correct |
105 ms |
3072 KB |
Output is correct |
16 |
Correct |
87 ms |
2908 KB |
Output is correct |
17 |
Correct |
2416 ms |
3500 KB |
Output is correct |
18 |
Correct |
1 ms |
1628 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1620 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1628 KB |
Output is correct |
23 |
Correct |
7 ms |
1736 KB |
Output is correct |
24 |
Correct |
27 ms |
1736 KB |
Output is correct |
25 |
Correct |
107 ms |
1784 KB |
Output is correct |
26 |
Correct |
73 ms |
1628 KB |
Output is correct |
27 |
Correct |
130 ms |
3476 KB |
Output is correct |
28 |
Execution timed out |
3067 ms |
4436 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3017 ms |
3900 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |