#include <bits/stdc++.h>
using namespace std;
int f1d[210],b1d[210];
int fnd[210],bnd[210];
int tempd[210];
vector<int> adjlst[210];
vector<int> badjlst[210];
int parent[210];
bool visited[210];
int p[210];
bool ton[50100],to1[50100];
int N,M;
vector<int> edge[50100];
int u, v, c, d;
int inf = 2e9;
bool is1,isn;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
int main(){
scanf(" %d",&N);
scanf(" %d",&M);
for(int i = 0; i < M; i++){
scanf(" %d",&u);
scanf(" %d",&v);
scanf(" %d",&c);
scanf(" %d",&d);
edge[i] = vector<int> {u,v,c,d};
adjlst[u].push_back(i);
badjlst[v].push_back(i);
}
for(int i = 0; i <= N; i++){
f1d[i] = inf;
b1d[i] = inf;
fnd[i] = inf;
bnd[i] = inf;
parent[i] = -1;
}
for(int i = 0; i < M; i++){
ton[1] = false;
to1[1] = false;
}
f1d[1] = 0;
for(int i : adjlst[1]){
pq.push(make_pair(edge[i][2],i));
}
while(!pq.empty()){
pair<int,int> ii = pq.top();
pq.pop();
int i = edge[ii.second][1];
if(f1d[i] != inf){
continue;
}
f1d[i] = ii.first;
parent[i] = ii.second;
for(int j : adjlst[i]){
pq.push(make_pair(f1d[i] + edge[j][2],j));
}
}
int t = parent[N];
while(t != -1){
ton[t] = true;
t = parent[edge[t][0]];
}
for(int i = 1; i <= N; i++) parent[i] = -1;
fnd[N] = 0;
for(int i : adjlst[N]){
pq.push(make_pair(edge[i][2],i));
}
while(!pq.empty()){
pair<int,int> ii = pq.top();
pq.pop();
int i = edge[ii.second][1];
if(fnd[i] != inf){
continue;
}
fnd[i] = ii.first;
parent[i] = ii.second;
for(int j : adjlst[i]){
pq.push(make_pair(fnd[i] + edge[j][2],j));
}
}
t = parent[1];
while(t != -1){
to1[t] = true;
t = parent[edge[t][0]];
}
b1d[1] = 0;
for(int i : badjlst[1]){
pq.push(make_pair(edge[i][2],i));
}
while(!pq.empty()){
pair<int,int> ii = pq.top();
pq.pop();
int i = edge[ii.second][0];
if(b1d[i] != inf){
continue;
}
b1d[i] = ii.first;
for(int j : badjlst[i]){
pq.push(make_pair(b1d[i] + edge[j][2],j));
}
}
bnd[N] = 0;
for(int i : badjlst[N]){
pq.push(make_pair(edge[i][2],i));
}
while(!pq.empty()){
pair<int,int> ii = pq.top();
pq.pop();
int i = edge[ii.second][0];
if(bnd[i] != inf){
continue;
}
bnd[i] = ii.first;
for(int j : badjlst[i]){
pq.push(make_pair(bnd[i] + edge[j][2],j));
}
}
long long int small = f1d[N] + (long long int) + fnd[1];
for(int i = 0; i < M; i++){
long long int temp = 0;
if(ton[i]){
for(int i = 1; i <= N; i++){
tempd[i] = inf;
visited[i] = false;
}
pair<int,int> small = make_pair(0,1);
tempd[1] = 0;
while(small.second != -1){
visited[small.second] = true;
if(edge[i][1] == small.second){
tempd[edge[i][0]] = min(tempd[edge[i][0]], tempd[small.second] + edge[i][2]);
}
for(int j : adjlst[small.second]){
if(j == i) continue;
tempd[edge[j][1]] = min(tempd[edge[j][1]], tempd[small.second] + edge[j][2]);
}
small = make_pair(inf,-1);
for(int i = 1; i <= N; i++){
if(!visited[i]) small = min(small, make_pair(tempd[i],i));
}
}
temp = tempd[N];
}
else temp = min((long long int)f1d[N], f1d[edge[i][1]] + (long long int)bnd[edge[i][0]] + edge[i][2]);
if(to1[i]){
for(int i = 1; i <= N; i++){
tempd[i] = inf;
visited[i] = false;
}
pair<int,int> small = make_pair(0,N);
tempd[N] = 0;
while(small.second != -1){
visited[small.second] = true;
if(edge[i][1] == small.second){
tempd[edge[i][0]] = min(tempd[edge[i][0]], tempd[small.second] + edge[i][2]);
}
for(int j : adjlst[small.second]){
if(j == i) continue;
tempd[edge[j][1]] = min(tempd[edge[j][1]], tempd[small.second] + edge[j][2]);
}
small = make_pair(inf,-1);
for(int i = 1; i <= N; i++){
if(!visited[i]) small = min(small, make_pair(tempd[i],i));
}
}
temp += tempd[1];
}
else temp += min((long long int)fnd[1], fnd[edge[i][1]] + (long long int)b1d[edge[i][0]] + edge[i][2]);
small = min(small, temp + edge[i][3]);
}
if(small >= inf){
printf("-1");
}
else{
printf("%lld",small);
}
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
ho_t4.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf(" %d",&M);
| ~~~~~^~~~~~~~~~
ho_t4.cpp:25:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf(" %d",&u);
| ~~~~~^~~~~~~~~~
ho_t4.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf(" %d",&v);
| ~~~~~^~~~~~~~~~
ho_t4.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf(" %d",&c);
| ~~~~~^~~~~~~~~~
ho_t4.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf(" %d",&d);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1628 KB |
Output is correct |
4 |
Correct |
3 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
33 ms |
1628 KB |
Output is correct |
11 |
Correct |
40 ms |
1628 KB |
Output is correct |
12 |
Correct |
34 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1880 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1628 KB |
Output is correct |
16 |
Correct |
3 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
4312 KB |
Output is correct |
2 |
Correct |
51 ms |
4564 KB |
Output is correct |
3 |
Correct |
47 ms |
4300 KB |
Output is correct |
4 |
Correct |
3 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
27 ms |
4188 KB |
Output is correct |
10 |
Correct |
27 ms |
3928 KB |
Output is correct |
11 |
Correct |
42 ms |
4208 KB |
Output is correct |
12 |
Correct |
39 ms |
4304 KB |
Output is correct |
13 |
Correct |
39 ms |
4312 KB |
Output is correct |
14 |
Correct |
42 ms |
5588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
33 ms |
3724 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
50 ms |
4344 KB |
Output is correct |
6 |
Correct |
1 ms |
1624 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
8 |
Correct |
25 ms |
3912 KB |
Output is correct |
9 |
Correct |
25 ms |
4188 KB |
Output is correct |
10 |
Correct |
39 ms |
4312 KB |
Output is correct |
11 |
Correct |
39 ms |
4308 KB |
Output is correct |
12 |
Correct |
37 ms |
4188 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1628 KB |
Output is correct |
18 |
Correct |
1 ms |
1628 KB |
Output is correct |
19 |
Correct |
39 ms |
4464 KB |
Output is correct |
20 |
Correct |
49 ms |
5376 KB |
Output is correct |
21 |
Correct |
39 ms |
5328 KB |
Output is correct |
22 |
Correct |
39 ms |
5336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1628 KB |
Output is correct |
4 |
Correct |
3 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
33 ms |
1628 KB |
Output is correct |
11 |
Correct |
40 ms |
1628 KB |
Output is correct |
12 |
Correct |
34 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1880 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1628 KB |
Output is correct |
16 |
Correct |
3 ms |
1628 KB |
Output is correct |
17 |
Correct |
48 ms |
4312 KB |
Output is correct |
18 |
Correct |
51 ms |
4564 KB |
Output is correct |
19 |
Correct |
47 ms |
4300 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
1 ms |
1628 KB |
Output is correct |
25 |
Correct |
27 ms |
4188 KB |
Output is correct |
26 |
Correct |
27 ms |
3928 KB |
Output is correct |
27 |
Correct |
42 ms |
4208 KB |
Output is correct |
28 |
Correct |
39 ms |
4304 KB |
Output is correct |
29 |
Correct |
39 ms |
4312 KB |
Output is correct |
30 |
Correct |
42 ms |
5588 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
1 ms |
1628 KB |
Output is correct |
33 |
Correct |
33 ms |
3724 KB |
Output is correct |
34 |
Correct |
1 ms |
1628 KB |
Output is correct |
35 |
Correct |
50 ms |
4344 KB |
Output is correct |
36 |
Correct |
1 ms |
1624 KB |
Output is correct |
37 |
Correct |
1 ms |
1880 KB |
Output is correct |
38 |
Correct |
25 ms |
3912 KB |
Output is correct |
39 |
Correct |
25 ms |
4188 KB |
Output is correct |
40 |
Correct |
39 ms |
4312 KB |
Output is correct |
41 |
Correct |
39 ms |
4308 KB |
Output is correct |
42 |
Correct |
37 ms |
4188 KB |
Output is correct |
43 |
Correct |
1 ms |
1628 KB |
Output is correct |
44 |
Correct |
1 ms |
1628 KB |
Output is correct |
45 |
Correct |
1 ms |
1628 KB |
Output is correct |
46 |
Correct |
1 ms |
1628 KB |
Output is correct |
47 |
Correct |
1 ms |
1628 KB |
Output is correct |
48 |
Correct |
1 ms |
1628 KB |
Output is correct |
49 |
Correct |
39 ms |
4464 KB |
Output is correct |
50 |
Correct |
49 ms |
5376 KB |
Output is correct |
51 |
Correct |
39 ms |
5328 KB |
Output is correct |
52 |
Correct |
39 ms |
5336 KB |
Output is correct |
53 |
Correct |
48 ms |
5360 KB |
Output is correct |
54 |
Correct |
52 ms |
5256 KB |
Output is correct |
55 |
Correct |
47 ms |
5328 KB |
Output is correct |
56 |
Correct |
2 ms |
1628 KB |
Output is correct |
57 |
Correct |
2 ms |
1628 KB |
Output is correct |
58 |
Correct |
134 ms |
4520 KB |
Output is correct |
59 |
Correct |
130 ms |
4456 KB |
Output is correct |
60 |
Correct |
134 ms |
4444 KB |
Output is correct |
61 |
Correct |
136 ms |
4480 KB |
Output is correct |
62 |
Correct |
131 ms |
4440 KB |
Output is correct |
63 |
Correct |
130 ms |
4440 KB |
Output is correct |
64 |
Correct |
131 ms |
4568 KB |
Output is correct |
65 |
Correct |
128 ms |
4568 KB |
Output is correct |
66 |
Correct |
109 ms |
4560 KB |
Output is correct |
67 |
Correct |
26 ms |
4560 KB |
Output is correct |
68 |
Correct |
28 ms |
5212 KB |
Output is correct |
69 |
Correct |
27 ms |
5208 KB |
Output is correct |
70 |
Correct |
42 ms |
5584 KB |
Output is correct |
71 |
Correct |
48 ms |
5584 KB |
Output is correct |
72 |
Correct |
49 ms |
5624 KB |
Output is correct |
73 |
Correct |
40 ms |
5588 KB |
Output is correct |
74 |
Correct |
41 ms |
5580 KB |
Output is correct |