#include <bits/stdc++.h>
using namespace std;
#define int unsigned int
const int inf = 1e9;
const int some_fixed_seed = 12345;
struct ura
{
int u,v,c,d;
};
int n,m;
vector<ura>e;
int d[205][205];
vector<pair<int,int>>g[205];
int getdist(int sursa,int destinatie)
{
vector<int>dist(n + 1);
for (int i = 1; i <= n; i++)
dist[i] = inf;
priority_queue<pair<int,int>>pq;
pq.push({0,sursa});
while (!pq.empty())
{
int nod = pq.top().second,cost = -pq.top().first;
if (nod == destinatie)
return cost;
pq.pop();
if (dist[nod] == inf)
{
dist[nod] = cost;
for (auto vecin : g[nod])
pq.push({-(cost + vecin.second),vecin.first});
}
}
return inf;
}
int tryrev(int idx)
{
for (int i = 1; i <= n; i++)
g[i].clear();
for (int i = 0; i < e.size(); i++)
{
if (i == idx)
g[e[i].v].push_back({e[i].u,e[i].c});
else
g[e[i].u].push_back({e[i].v,e[i].c});
}
return getdist(1,n) + getdist(n,1);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
d[i][j] = inf;
for (int i = 1; i <= m; i++)
{
ura aux;
cin >> aux.u >> aux.v >> aux.c >> aux.d;
e.push_back(aux);
d[aux.u][aux.v] = min(d[aux.u][aux.v],aux.c);
}
//shuffle(e.begin(),e.end(),default_random_engine(some_fixed_seed));
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
int ans = d[1][n] + d[n][1];
for (int i = 0; i < e.size(); i++)
{
auto it = e[i];
int d1 = min(d[1][n],d[1][it.v] + d[it.u][n] + it.c);
int d2 = min(d[n][1],d[n][it.v] + d[it.u][1] + it.c);
int cur = d1 + d2 + it.d;
if (cur < ans)
ans = min(ans,it.d + tryrev(i));
}
if (ans >= inf)
cout << -1;
else
cout << ans;
return 0;
}
/**
hai sa zicem ca dau invert la muchia u->v
costul o sa fie >= cost_rev + min(dist(1,n),cost_edge + dist(1,v) + dist(u,n)) + min(dist(n,1),cost_edge + dist(n,v) + dist(u,1))
dau random shuffle la muchii si verific o muchie doar daca are acest cost < ans
teoretic as verifica log muchii, asa o sa verific ceva mai multe dar hopefully tot putine
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Correct |
12 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
676 KB |
Output is correct |
4 |
Correct |
8 ms |
680 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
476 KB |
Output is correct |
10 |
Correct |
14 ms |
860 KB |
Output is correct |
11 |
Correct |
25 ms |
696 KB |
Output is correct |
12 |
Correct |
8 ms |
604 KB |
Output is correct |
13 |
Correct |
8 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
604 KB |
Output is correct |
16 |
Correct |
7 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2524 KB |
Output is correct |
2 |
Correct |
20 ms |
3544 KB |
Output is correct |
3 |
Correct |
18 ms |
2780 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
8 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
7 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
56 ms |
4060 KB |
Output is correct |
10 |
Correct |
38 ms |
3928 KB |
Output is correct |
11 |
Correct |
23 ms |
3536 KB |
Output is correct |
12 |
Correct |
24 ms |
3700 KB |
Output is correct |
13 |
Correct |
22 ms |
3792 KB |
Output is correct |
14 |
Correct |
24 ms |
4048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Correct |
7 ms |
584 KB |
Output is correct |
3 |
Correct |
16 ms |
2520 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
18 ms |
2524 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
88 ms |
3260 KB |
Output is correct |
9 |
Correct |
109 ms |
3024 KB |
Output is correct |
10 |
Correct |
25 ms |
2992 KB |
Output is correct |
11 |
Correct |
23 ms |
3032 KB |
Output is correct |
12 |
Correct |
22 ms |
3064 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
24 ms |
3132 KB |
Output is correct |
20 |
Correct |
30 ms |
3164 KB |
Output is correct |
21 |
Correct |
25 ms |
3032 KB |
Output is correct |
22 |
Correct |
24 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Correct |
12 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
676 KB |
Output is correct |
4 |
Correct |
8 ms |
680 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
476 KB |
Output is correct |
10 |
Correct |
14 ms |
860 KB |
Output is correct |
11 |
Correct |
25 ms |
696 KB |
Output is correct |
12 |
Correct |
8 ms |
604 KB |
Output is correct |
13 |
Correct |
8 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
604 KB |
Output is correct |
16 |
Correct |
7 ms |
684 KB |
Output is correct |
17 |
Correct |
18 ms |
2524 KB |
Output is correct |
18 |
Correct |
20 ms |
3544 KB |
Output is correct |
19 |
Correct |
18 ms |
2780 KB |
Output is correct |
20 |
Correct |
7 ms |
604 KB |
Output is correct |
21 |
Correct |
8 ms |
604 KB |
Output is correct |
22 |
Correct |
7 ms |
604 KB |
Output is correct |
23 |
Correct |
7 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
56 ms |
4060 KB |
Output is correct |
26 |
Correct |
38 ms |
3928 KB |
Output is correct |
27 |
Correct |
23 ms |
3536 KB |
Output is correct |
28 |
Correct |
24 ms |
3700 KB |
Output is correct |
29 |
Correct |
22 ms |
3792 KB |
Output is correct |
30 |
Correct |
24 ms |
4048 KB |
Output is correct |
31 |
Correct |
7 ms |
600 KB |
Output is correct |
32 |
Correct |
7 ms |
584 KB |
Output is correct |
33 |
Correct |
16 ms |
2520 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
18 ms |
2524 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
88 ms |
3260 KB |
Output is correct |
39 |
Correct |
109 ms |
3024 KB |
Output is correct |
40 |
Correct |
25 ms |
2992 KB |
Output is correct |
41 |
Correct |
23 ms |
3032 KB |
Output is correct |
42 |
Correct |
22 ms |
3064 KB |
Output is correct |
43 |
Correct |
0 ms |
600 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
24 ms |
3132 KB |
Output is correct |
50 |
Correct |
30 ms |
3164 KB |
Output is correct |
51 |
Correct |
25 ms |
3032 KB |
Output is correct |
52 |
Correct |
24 ms |
3028 KB |
Output is correct |
53 |
Correct |
23 ms |
2520 KB |
Output is correct |
54 |
Correct |
20 ms |
2524 KB |
Output is correct |
55 |
Correct |
24 ms |
2780 KB |
Output is correct |
56 |
Correct |
7 ms |
604 KB |
Output is correct |
57 |
Correct |
7 ms |
604 KB |
Output is correct |
58 |
Correct |
33 ms |
3340 KB |
Output is correct |
59 |
Correct |
38 ms |
2964 KB |
Output is correct |
60 |
Correct |
45 ms |
3028 KB |
Output is correct |
61 |
Correct |
40 ms |
3276 KB |
Output is correct |
62 |
Correct |
333 ms |
3020 KB |
Output is correct |
63 |
Correct |
787 ms |
3032 KB |
Output is correct |
64 |
Correct |
103 ms |
3076 KB |
Output is correct |
65 |
Correct |
308 ms |
3276 KB |
Output is correct |
66 |
Correct |
637 ms |
3020 KB |
Output is correct |
67 |
Correct |
22 ms |
3284 KB |
Output is correct |
68 |
Correct |
51 ms |
4008 KB |
Output is correct |
69 |
Correct |
38 ms |
4056 KB |
Output is correct |
70 |
Correct |
25 ms |
3968 KB |
Output is correct |
71 |
Correct |
30 ms |
3788 KB |
Output is correct |
72 |
Correct |
24 ms |
3532 KB |
Output is correct |
73 |
Correct |
24 ms |
4056 KB |
Output is correct |
74 |
Correct |
33 ms |
3788 KB |
Output is correct |