#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 200;
const int MAXM = 5e4;
const ll INF = 1e15;
struct Edge
{
int u, v, p; ll c, d;
};
struct Queue
{
int u, p; ll w;
bool operator < (const Queue &p) const { return w>p.w; }
};
int N, M;
vector<Edge> adj[MAXN+10], radj[MAXN+10];
Edge E[MAXM+10];
ll D1[MAXN+10], DR1[MAXN+10], D2[MAXN+10], DR2[MAXN+10], D[MAXN+10];
int T1[MAXM+10], TR1[MAXM+10], T2[MAXM+10], TR2[MAXM+10], T[MAXM+10];
ll V[MAXM+10];
void dijk(int S, vector<Edge> *aadj, ll *DD, int *TT)
{
int i, j;
priority_queue<Queue> PQ;
for(i=1; i<=N; i++) DD[i]=INF;
for(i=1; i<=M; i++) TT[i]=0;
PQ.push({S, -1, 0}); DD[S]=0;
while(!PQ.empty())
{
Queue now=PQ.top(); PQ.pop();
if(DD[now.u]!=now.w) continue;
TT[now.p]=1;
for(auto nxt : aadj[now.u])
{
if(DD[nxt.v]<=now.w+nxt.c) continue;
DD[nxt.v]=now.w+nxt.c;
PQ.push({nxt.v, nxt.p, now.w+nxt.c});
}
}
}
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<=M; i++)
{
int u, v, c, d;
scanf("%d%d%d%d", &u, &v, &c, &d);
E[i]={u, v, i, c, d};
adj[u].push_back({u, v, i, c, d});
radj[v].push_back({v, u, i, c, d});
}
dijk(1, adj, D1, T1);
dijk(1, radj, DR1, TR1);
dijk(N, adj, D2, T2);
dijk(N, radj, DR2, TR2);
for(i=1; i<=M; i++) V[i]+=E[i].d;
for(i=1; i<=M; i++)
{
if(T1[i])
{
for(auto &it : adj[E[i].u]) if(it.p==i) it.c=INF;
adj[E[i].v].push_back({E[i].v, E[i].u, i, E[i].c, E[i].d});
dijk(1, adj, D, T);
V[i]+=D[N];
adj[E[i].v].pop_back();
for(auto &it : adj[E[i].u]) if(it.p==i) it.c=E[i].c;
}
else
{
ll t=D1[N];
t=min(t, D1[E[i].v]+DR2[E[i].u]+E[i].c);
V[i]+=t;
}
}
for(i=1; i<=M; i++)
{
if(T2[i])
{
for(auto &it : adj[E[i].u]) if(it.p==i) it.c=INF;
adj[E[i].v].push_back({E[i].v, E[i].u, i, E[i].c, E[i].d});
dijk(N, adj, D, T);
V[i]+=D[1];
adj[E[i].v].pop_back();
for(auto &it : adj[E[i].u]) if(it.p==i) it.c=E[i].c;
}
else
{
ll t=D2[1];
t=min(t, D2[E[i].v]+DR1[E[i].u]+E[i].c);
V[i]+=t;
}
}
ll ans=INF;
ans=min(ans, D1[N]+D2[1]);
for(i=1; i<=M; i++) ans=min(ans, V[i]);
if(ans==INF) ans=-1;
printf("%lld\n", ans);
}
Compilation message
ho_t4.cpp: In function 'void dijk(int, std::vector<Edge>*, ll*, int*)':
ho_t4.cpp:32:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:54:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
ho_t4.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &u, &v, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
15 ms |
512 KB |
Output is correct |
4 |
Correct |
17 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
20 ms |
512 KB |
Output is correct |
11 |
Correct |
19 ms |
512 KB |
Output is correct |
12 |
Correct |
19 ms |
512 KB |
Output is correct |
13 |
Correct |
8 ms |
512 KB |
Output is correct |
14 |
Correct |
11 ms |
512 KB |
Output is correct |
15 |
Correct |
11 ms |
512 KB |
Output is correct |
16 |
Correct |
11 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
8184 KB |
Output is correct |
2 |
Correct |
107 ms |
9080 KB |
Output is correct |
3 |
Correct |
107 ms |
9252 KB |
Output is correct |
4 |
Correct |
13 ms |
768 KB |
Output is correct |
5 |
Correct |
8 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
56 ms |
9088 KB |
Output is correct |
10 |
Correct |
53 ms |
9336 KB |
Output is correct |
11 |
Correct |
82 ms |
9336 KB |
Output is correct |
12 |
Correct |
83 ms |
9208 KB |
Output is correct |
13 |
Correct |
82 ms |
9484 KB |
Output is correct |
14 |
Correct |
80 ms |
9848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
63 ms |
6144 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
79 ms |
7928 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
46 ms |
9088 KB |
Output is correct |
9 |
Correct |
50 ms |
8832 KB |
Output is correct |
10 |
Correct |
63 ms |
8828 KB |
Output is correct |
11 |
Correct |
62 ms |
8824 KB |
Output is correct |
12 |
Correct |
66 ms |
9336 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
84 ms |
9208 KB |
Output is correct |
20 |
Correct |
66 ms |
8840 KB |
Output is correct |
21 |
Correct |
66 ms |
8832 KB |
Output is correct |
22 |
Correct |
116 ms |
8824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
15 ms |
512 KB |
Output is correct |
4 |
Correct |
17 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
20 ms |
512 KB |
Output is correct |
11 |
Correct |
19 ms |
512 KB |
Output is correct |
12 |
Correct |
19 ms |
512 KB |
Output is correct |
13 |
Correct |
8 ms |
512 KB |
Output is correct |
14 |
Correct |
11 ms |
512 KB |
Output is correct |
15 |
Correct |
11 ms |
512 KB |
Output is correct |
16 |
Correct |
11 ms |
512 KB |
Output is correct |
17 |
Correct |
105 ms |
8184 KB |
Output is correct |
18 |
Correct |
107 ms |
9080 KB |
Output is correct |
19 |
Correct |
107 ms |
9252 KB |
Output is correct |
20 |
Correct |
13 ms |
768 KB |
Output is correct |
21 |
Correct |
8 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
56 ms |
9088 KB |
Output is correct |
26 |
Correct |
53 ms |
9336 KB |
Output is correct |
27 |
Correct |
82 ms |
9336 KB |
Output is correct |
28 |
Correct |
83 ms |
9208 KB |
Output is correct |
29 |
Correct |
82 ms |
9484 KB |
Output is correct |
30 |
Correct |
80 ms |
9848 KB |
Output is correct |
31 |
Correct |
9 ms |
512 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
63 ms |
6144 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
79 ms |
7928 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
4 ms |
384 KB |
Output is correct |
38 |
Correct |
46 ms |
9088 KB |
Output is correct |
39 |
Correct |
50 ms |
8832 KB |
Output is correct |
40 |
Correct |
63 ms |
8828 KB |
Output is correct |
41 |
Correct |
62 ms |
8824 KB |
Output is correct |
42 |
Correct |
66 ms |
9336 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
84 ms |
9208 KB |
Output is correct |
50 |
Correct |
66 ms |
8840 KB |
Output is correct |
51 |
Correct |
66 ms |
8832 KB |
Output is correct |
52 |
Correct |
116 ms |
8824 KB |
Output is correct |
53 |
Correct |
143 ms |
8952 KB |
Output is correct |
54 |
Correct |
123 ms |
9080 KB |
Output is correct |
55 |
Correct |
115 ms |
8824 KB |
Output is correct |
56 |
Correct |
15 ms |
512 KB |
Output is correct |
57 |
Correct |
16 ms |
512 KB |
Output is correct |
58 |
Correct |
102 ms |
7032 KB |
Output is correct |
59 |
Correct |
106 ms |
7160 KB |
Output is correct |
60 |
Correct |
108 ms |
7032 KB |
Output is correct |
61 |
Correct |
96 ms |
7032 KB |
Output is correct |
62 |
Correct |
102 ms |
7040 KB |
Output is correct |
63 |
Correct |
104 ms |
7032 KB |
Output is correct |
64 |
Correct |
682 ms |
8044 KB |
Output is correct |
65 |
Correct |
637 ms |
7780 KB |
Output is correct |
66 |
Correct |
617 ms |
8160 KB |
Output is correct |
67 |
Correct |
29 ms |
6236 KB |
Output is correct |
68 |
Correct |
59 ms |
8952 KB |
Output is correct |
69 |
Correct |
55 ms |
9216 KB |
Output is correct |
70 |
Correct |
89 ms |
9080 KB |
Output is correct |
71 |
Correct |
88 ms |
8952 KB |
Output is correct |
72 |
Correct |
89 ms |
9080 KB |
Output is correct |
73 |
Correct |
94 ms |
9164 KB |
Output is correct |
74 |
Correct |
88 ms |
9212 KB |
Output is correct |