#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5+5;
typedef long long ll;
#define inf 1e17
typedef pair<ll,ll>pii;
ll cost[mxn];
ll dir[mxn];
struct data
{
int u, v;
ll c, d;
};
vector<pii>adj[205];
vector<pii>rev[205];
vector<data>edge;
ll dis[5][205];
int path[3][205];
int n, m;
void dijkstra1(int src, int pos)
{
vector<int>vis(n+1, 0);
dis[pos][src] = 0;
priority_queue<pii>pq;
pq.push({0LL, src});
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto it : adj[u]) {
ll w = cost[it.second];
int v = it.first;
if(dis[pos][u]+w<dis[pos][v]) {
dis[pos][v] = dis[pos][u]+w;
path[pos][v] = it.second;
pq.push({-dis[pos][v], v});
}
}
}
return;
}
void dijkstra2(int src, int pos)
{
vector<int>vis(n+1, 0);
dis[pos][src] = 0;
priority_queue<pii>pq;
pq.push({0LL, src});
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto it : rev[u]) {
ll w = cost[it.second];
int v = it.first;
if(dis[pos][u]+w<dis[pos][v]) {
dis[pos][v] = dis[pos][u]+w;
pq.push({-dis[pos][v], v});
}
}
}
return;
}
ll dijkstra(int src, int idx)
{
vector<ll>dp(n+5, inf);
vector<bool>vis(n+5, 0);
dp[src] = 0;
priority_queue<pii>pq;
pq.push({0, src});
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto it : adj[u]) {
if(idx==it.second) continue;
int v = it.first;
ll w = cost[it.second];
if(dp[u]+w<dp[v]) {
dp[v] = dp[u]+w;
pq.push({-dp[v], v});
}
}
if(u==edge[idx].v) {
int x = edge[idx].v;
int y = edge[idx].u;
ll w = edge[idx].c;
if(dp[x]+w<dp[y]) {
dp[y] = dp[x]+w;
pq.push({-dp[y], y});
}
}
}
if(src==1) return dp[n];
return dp[1];
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < 5; i++) {
for(int r = 0; r <= n; r++) dis[i][r] = inf;
}
for(int i = 0; i < m; i++) {
int u, v;
scanf("%d%d%lld%lld", &u, &v, &cost[i], &dir[i]);
adj[u].push_back({v, i});
rev[v].push_back({u, i});
edge.push_back({u, v, cost[i], dir[i]});
}
dijkstra1(1, 0);
dijkstra1(n, 1);
dijkstra2(1, 2);
dijkstra2(n, 3);
ll ans = dis[0][n]+dis[1][1];
for(int i = 0; i < m; i++) {
int u = edge[i].u;
int v = edge[i].v;
ll c = edge[i].c;
ll d = edge[i].d;
ll res = d;
/*
if(i==2) {
cout << res << '\n';
}
*/
if(path[0][v]==i) {
res+=dijkstra(1, i);
}
else {
res+=min(dis[0][n], dis[0][v]+dis[3][u]+c);
}
/*
if(i==2) {
cout << res << '\n';
}
*/
if(path[1][v]==i) {
res+=dijkstra(n, i);
}
else {
res+=min(dis[1][1], dis[1][v]+dis[2][u]+c);
}
/*
if(i==2) {
cout << res << '\n';
}
*/
//cout << res << '\n';
ans = min(ans, res);
}
if(ans>=inf) {
cout << -1 << '\n';
}
else {
cout << ans << '\n';
}
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
107 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:114:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
114 | scanf("%d%d%lld%lld", &u, &v, &cost[i], &dir[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
14 ms |
512 KB |
Output is correct |
4 |
Correct |
14 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
18 ms |
492 KB |
Output is correct |
11 |
Correct |
19 ms |
492 KB |
Output is correct |
12 |
Correct |
22 ms |
492 KB |
Output is correct |
13 |
Correct |
4 ms |
492 KB |
Output is correct |
14 |
Correct |
10 ms |
492 KB |
Output is correct |
15 |
Correct |
7 ms |
492 KB |
Output is correct |
16 |
Correct |
8 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
5024 KB |
Output is correct |
2 |
Correct |
107 ms |
5024 KB |
Output is correct |
3 |
Correct |
116 ms |
5024 KB |
Output is correct |
4 |
Correct |
13 ms |
620 KB |
Output is correct |
5 |
Correct |
6 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
42 ms |
4896 KB |
Output is correct |
10 |
Correct |
41 ms |
4896 KB |
Output is correct |
11 |
Correct |
81 ms |
5024 KB |
Output is correct |
12 |
Correct |
78 ms |
5024 KB |
Output is correct |
13 |
Correct |
75 ms |
5024 KB |
Output is correct |
14 |
Correct |
85 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
74 ms |
4260 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
80 ms |
4896 KB |
Output is correct |
6 |
Correct |
1 ms |
396 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
35 ms |
4916 KB |
Output is correct |
9 |
Correct |
37 ms |
4896 KB |
Output is correct |
10 |
Correct |
58 ms |
4896 KB |
Output is correct |
11 |
Correct |
59 ms |
4896 KB |
Output is correct |
12 |
Correct |
60 ms |
5024 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
62 ms |
5024 KB |
Output is correct |
20 |
Correct |
59 ms |
4896 KB |
Output is correct |
21 |
Correct |
64 ms |
4896 KB |
Output is correct |
22 |
Correct |
59 ms |
4860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
14 ms |
512 KB |
Output is correct |
4 |
Correct |
14 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
18 ms |
492 KB |
Output is correct |
11 |
Correct |
19 ms |
492 KB |
Output is correct |
12 |
Correct |
22 ms |
492 KB |
Output is correct |
13 |
Correct |
4 ms |
492 KB |
Output is correct |
14 |
Correct |
10 ms |
492 KB |
Output is correct |
15 |
Correct |
7 ms |
492 KB |
Output is correct |
16 |
Correct |
8 ms |
492 KB |
Output is correct |
17 |
Correct |
110 ms |
5024 KB |
Output is correct |
18 |
Correct |
107 ms |
5024 KB |
Output is correct |
19 |
Correct |
116 ms |
5024 KB |
Output is correct |
20 |
Correct |
13 ms |
620 KB |
Output is correct |
21 |
Correct |
6 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
42 ms |
4896 KB |
Output is correct |
26 |
Correct |
41 ms |
4896 KB |
Output is correct |
27 |
Correct |
81 ms |
5024 KB |
Output is correct |
28 |
Correct |
78 ms |
5024 KB |
Output is correct |
29 |
Correct |
75 ms |
5024 KB |
Output is correct |
30 |
Correct |
85 ms |
5212 KB |
Output is correct |
31 |
Correct |
10 ms |
620 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
74 ms |
4260 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
80 ms |
4896 KB |
Output is correct |
36 |
Correct |
1 ms |
396 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
35 ms |
4916 KB |
Output is correct |
39 |
Correct |
37 ms |
4896 KB |
Output is correct |
40 |
Correct |
58 ms |
4896 KB |
Output is correct |
41 |
Correct |
59 ms |
4896 KB |
Output is correct |
42 |
Correct |
60 ms |
5024 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
0 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
62 ms |
5024 KB |
Output is correct |
50 |
Correct |
59 ms |
4896 KB |
Output is correct |
51 |
Correct |
64 ms |
4896 KB |
Output is correct |
52 |
Correct |
59 ms |
4860 KB |
Output is correct |
53 |
Correct |
127 ms |
4768 KB |
Output is correct |
54 |
Correct |
137 ms |
4896 KB |
Output is correct |
55 |
Correct |
132 ms |
4896 KB |
Output is correct |
56 |
Correct |
13 ms |
492 KB |
Output is correct |
57 |
Correct |
14 ms |
512 KB |
Output is correct |
58 |
Correct |
104 ms |
4132 KB |
Output is correct |
59 |
Correct |
107 ms |
4388 KB |
Output is correct |
60 |
Correct |
112 ms |
4260 KB |
Output is correct |
61 |
Correct |
101 ms |
4260 KB |
Output is correct |
62 |
Correct |
103 ms |
4260 KB |
Output is correct |
63 |
Correct |
110 ms |
4260 KB |
Output is correct |
64 |
Correct |
706 ms |
4564 KB |
Output is correct |
65 |
Correct |
670 ms |
4512 KB |
Output is correct |
66 |
Correct |
622 ms |
4512 KB |
Output is correct |
67 |
Correct |
20 ms |
3596 KB |
Output is correct |
68 |
Correct |
45 ms |
4896 KB |
Output is correct |
69 |
Correct |
43 ms |
4896 KB |
Output is correct |
70 |
Correct |
85 ms |
4896 KB |
Output is correct |
71 |
Correct |
83 ms |
4896 KB |
Output is correct |
72 |
Correct |
89 ms |
4896 KB |
Output is correct |
73 |
Correct |
90 ms |
5024 KB |
Output is correct |
74 |
Correct |
82 ms |
5024 KB |
Output is correct |