#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 = 1e18;
struct Edge
{
int u, v; ll c, d; int p;
bool operator < (const Edge &q) const { return p<q.p; }
};
int N, M;
vector<Edge> adj[MAXN+10], radj[MAXN+10];
Edge E[MAXM+10];
ll ans[MAXM+10];
ll DA[MAXN+10], DB[MAXN+10], RDA[MAXN+10], RDB[MAXN+10];
int PA[MAXN+10], PB[MAXN+10], RPA[MAXN+10], RPB[MAXN+10];
ll TD[MAXN+10];
int TP[MAXN+10];
bool vis[MAXN+10];
void dijk(vector<Edge> *G, int S, ll *D, int *P)
{
for(int i=1; i<=N; i++) D[i]=INF, P[i]=-1, vis[i]=0;
D[S]=0;
while(1)
{
int now=-1; ll val=INF;
for(int i=1; i<=N; i++)
{
if(vis[i]) continue;
if(val>D[i]) val=D[i], now=i;
}
if(now==-1) break;
vis[now]=1;
//printf("!%d %lld\n", now, val);
for(auto nxt : G[now])
{
if(vis[nxt.v]) continue;
if(val+nxt.c<=D[nxt.v])
{
D[nxt.v]=val+nxt.c;
P[nxt.v]=nxt.p;
}
}
}
}
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=M; i++)
{
int u, v, c, d;
scanf("%d%d%d%d", &u, &v, &c, &d);
adj[u].push_back({u, v, c, d, i});
radj[v].push_back({v, u, c, d, i});
E[i]={u, v, c, d, i};
}
for(int i=1; i<=N; i++) sort(adj[i].begin(), adj[i].end());
dijk(adj, 1, DA, PA);
dijk(adj, N, DB, PB);
dijk(radj, 1, RDA, RPA);
dijk(radj, N, RDB, RPB);
{
vector<int> V;
for(int i=1; i<=N; i++) if(PA[i]) V.push_back(PA[i]);
for(int i=1; i<=N; i++) if(RPB[i]) V.push_back(RPB[i]);
sort(V.begin(), V.end());
for(int i=1; i<=M; i++)
{
if(binary_search(V.begin(), V.end(), i))
{
auto it=lower_bound(adj[E[i].u].begin(), adj[E[i].u].end(), E[i]);
it->c=INF;
dijk(adj, 1, TD, TP);
ans[i]+=TD[N];
it->c=E[i].c;
}
else
{
ans[i]+=min(DA[E[i].v]+E[i].c+RDB[E[i].u], DA[N]);
}
}
}
{
vector<int> V;
for(int i=1; i<=N; i++) if(PB[i]) V.push_back(PB[i]);
for(int i=1; i<=N; i++) if(RPA[i]) V.push_back(RPA[i]);
sort(V.begin(), V.end());
for(int i=1; i<=M; i++)
{
if(binary_search(V.begin(), V.end(), i))
{
auto it=lower_bound(adj[E[i].u].begin(), adj[E[i].u].end(), E[i]);
it->c=INF;
dijk(adj, N, TD, TP);
ans[i]+=TD[1];
it->c=E[i].c;
}
else
{
ans[i]+=min(DB[E[i].v]+E[i].c+RDA[E[i].u], DB[1]);
}
}
}
for(int i=1; i<=M; i++) ans[i]+=E[i].d;
ll val=DA[N]+DB[1];
for(int i=1; i<=M; i++) val=min(val, ans[i]);
if(val>=INF) val=-1;
printf("%lld\n", val);
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
62 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | scanf("%d%d%d%d", &u, &v, &c, &d);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
73 ms |
512 KB |
Output is correct |
4 |
Correct |
86 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
47 ms |
632 KB |
Output is correct |
11 |
Correct |
45 ms |
512 KB |
Output is correct |
12 |
Correct |
45 ms |
512 KB |
Output is correct |
13 |
Correct |
22 ms |
544 KB |
Output is correct |
14 |
Correct |
48 ms |
512 KB |
Output is correct |
15 |
Correct |
48 ms |
512 KB |
Output is correct |
16 |
Correct |
52 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
7032 KB |
Output is correct |
2 |
Correct |
227 ms |
7032 KB |
Output is correct |
3 |
Correct |
231 ms |
7032 KB |
Output is correct |
4 |
Correct |
77 ms |
640 KB |
Output is correct |
5 |
Correct |
58 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
81 ms |
6904 KB |
Output is correct |
10 |
Correct |
77 ms |
7244 KB |
Output is correct |
11 |
Correct |
163 ms |
7160 KB |
Output is correct |
12 |
Correct |
172 ms |
7032 KB |
Output is correct |
13 |
Correct |
162 ms |
7288 KB |
Output is correct |
14 |
Correct |
156 ms |
7544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
636 KB |
Output is correct |
2 |
Correct |
8 ms |
384 KB |
Output is correct |
3 |
Correct |
210 ms |
5368 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
268 ms |
6904 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
76 ms |
7160 KB |
Output is correct |
9 |
Correct |
85 ms |
6776 KB |
Output is correct |
10 |
Correct |
189 ms |
6904 KB |
Output is correct |
11 |
Correct |
182 ms |
7800 KB |
Output is correct |
12 |
Correct |
183 ms |
8184 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
190 ms |
8152 KB |
Output is correct |
20 |
Correct |
184 ms |
8024 KB |
Output is correct |
21 |
Correct |
184 ms |
7804 KB |
Output is correct |
22 |
Correct |
186 ms |
7800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
73 ms |
512 KB |
Output is correct |
4 |
Correct |
86 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
47 ms |
632 KB |
Output is correct |
11 |
Correct |
45 ms |
512 KB |
Output is correct |
12 |
Correct |
45 ms |
512 KB |
Output is correct |
13 |
Correct |
22 ms |
544 KB |
Output is correct |
14 |
Correct |
48 ms |
512 KB |
Output is correct |
15 |
Correct |
48 ms |
512 KB |
Output is correct |
16 |
Correct |
52 ms |
512 KB |
Output is correct |
17 |
Correct |
225 ms |
7032 KB |
Output is correct |
18 |
Correct |
227 ms |
7032 KB |
Output is correct |
19 |
Correct |
231 ms |
7032 KB |
Output is correct |
20 |
Correct |
77 ms |
640 KB |
Output is correct |
21 |
Correct |
58 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
81 ms |
6904 KB |
Output is correct |
26 |
Correct |
77 ms |
7244 KB |
Output is correct |
27 |
Correct |
163 ms |
7160 KB |
Output is correct |
28 |
Correct |
172 ms |
7032 KB |
Output is correct |
29 |
Correct |
162 ms |
7288 KB |
Output is correct |
30 |
Correct |
156 ms |
7544 KB |
Output is correct |
31 |
Correct |
53 ms |
636 KB |
Output is correct |
32 |
Correct |
8 ms |
384 KB |
Output is correct |
33 |
Correct |
210 ms |
5368 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
268 ms |
6904 KB |
Output is correct |
36 |
Correct |
0 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
76 ms |
7160 KB |
Output is correct |
39 |
Correct |
85 ms |
6776 KB |
Output is correct |
40 |
Correct |
189 ms |
6904 KB |
Output is correct |
41 |
Correct |
182 ms |
7800 KB |
Output is correct |
42 |
Correct |
183 ms |
8184 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
190 ms |
8152 KB |
Output is correct |
50 |
Correct |
184 ms |
8024 KB |
Output is correct |
51 |
Correct |
184 ms |
7804 KB |
Output is correct |
52 |
Correct |
186 ms |
7800 KB |
Output is correct |
53 |
Correct |
281 ms |
7996 KB |
Output is correct |
54 |
Correct |
282 ms |
7932 KB |
Output is correct |
55 |
Correct |
276 ms |
7928 KB |
Output is correct |
56 |
Correct |
76 ms |
512 KB |
Output is correct |
57 |
Correct |
76 ms |
640 KB |
Output is correct |
58 |
Correct |
153 ms |
6264 KB |
Output is correct |
59 |
Correct |
152 ms |
6264 KB |
Output is correct |
60 |
Correct |
150 ms |
6392 KB |
Output is correct |
61 |
Correct |
150 ms |
6136 KB |
Output is correct |
62 |
Correct |
151 ms |
6264 KB |
Output is correct |
63 |
Correct |
149 ms |
6264 KB |
Output is correct |
64 |
Correct |
158 ms |
6648 KB |
Output is correct |
65 |
Correct |
139 ms |
6392 KB |
Output is correct |
66 |
Correct |
125 ms |
6776 KB |
Output is correct |
67 |
Correct |
28 ms |
5484 KB |
Output is correct |
68 |
Correct |
87 ms |
7928 KB |
Output is correct |
69 |
Correct |
84 ms |
8184 KB |
Output is correct |
70 |
Correct |
189 ms |
8056 KB |
Output is correct |
71 |
Correct |
194 ms |
8056 KB |
Output is correct |
72 |
Correct |
191 ms |
8056 KB |
Output is correct |
73 |
Correct |
199 ms |
8312 KB |
Output is correct |
74 |
Correct |
192 ms |
8188 KB |
Output is correct |