#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
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
604 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
636 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
660 KB |
Output is correct |
11 |
Correct |
25 ms |
600 KB |
Output is correct |
12 |
Correct |
8 ms |
604 KB |
Output is correct |
13 |
Correct |
11 ms |
600 KB |
Output is correct |
14 |
Correct |
8 ms |
600 KB |
Output is correct |
15 |
Correct |
8 ms |
604 KB |
Output is correct |
16 |
Correct |
8 ms |
664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
1756 KB |
Output is correct |
2 |
Correct |
22 ms |
2520 KB |
Output is correct |
3 |
Correct |
19 ms |
1756 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
9 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
69 ms |
2908 KB |
Output is correct |
10 |
Correct |
53 ms |
2776 KB |
Output is correct |
11 |
Correct |
24 ms |
2520 KB |
Output is correct |
12 |
Correct |
23 ms |
2512 KB |
Output is correct |
13 |
Correct |
25 ms |
2748 KB |
Output is correct |
14 |
Correct |
25 ms |
2880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
9 ms |
604 KB |
Output is correct |
3 |
Correct |
16 ms |
1756 KB |
Output is correct |
4 |
Correct |
7 ms |
852 KB |
Output is correct |
5 |
Correct |
19 ms |
1756 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
63 ms |
2252 KB |
Output is correct |
9 |
Correct |
73 ms |
2264 KB |
Output is correct |
10 |
Correct |
26 ms |
2264 KB |
Output is correct |
11 |
Correct |
23 ms |
2008 KB |
Output is correct |
12 |
Correct |
23 ms |
1996 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
2264 KB |
Output is correct |
20 |
Correct |
30 ms |
2264 KB |
Output is correct |
21 |
Correct |
31 ms |
2280 KB |
Output is correct |
22 |
Correct |
25 ms |
2276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
604 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
636 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
660 KB |
Output is correct |
11 |
Correct |
25 ms |
600 KB |
Output is correct |
12 |
Correct |
8 ms |
604 KB |
Output is correct |
13 |
Correct |
11 ms |
600 KB |
Output is correct |
14 |
Correct |
8 ms |
600 KB |
Output is correct |
15 |
Correct |
8 ms |
604 KB |
Output is correct |
16 |
Correct |
8 ms |
664 KB |
Output is correct |
17 |
Correct |
19 ms |
1756 KB |
Output is correct |
18 |
Correct |
22 ms |
2520 KB |
Output is correct |
19 |
Correct |
19 ms |
1756 KB |
Output is correct |
20 |
Correct |
8 ms |
604 KB |
Output is correct |
21 |
Correct |
9 ms |
604 KB |
Output is correct |
22 |
Correct |
7 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
69 ms |
2908 KB |
Output is correct |
26 |
Correct |
53 ms |
2776 KB |
Output is correct |
27 |
Correct |
24 ms |
2520 KB |
Output is correct |
28 |
Correct |
23 ms |
2512 KB |
Output is correct |
29 |
Correct |
25 ms |
2748 KB |
Output is correct |
30 |
Correct |
25 ms |
2880 KB |
Output is correct |
31 |
Correct |
8 ms |
604 KB |
Output is correct |
32 |
Correct |
9 ms |
604 KB |
Output is correct |
33 |
Correct |
16 ms |
1756 KB |
Output is correct |
34 |
Correct |
7 ms |
852 KB |
Output is correct |
35 |
Correct |
19 ms |
1756 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
63 ms |
2252 KB |
Output is correct |
39 |
Correct |
73 ms |
2264 KB |
Output is correct |
40 |
Correct |
26 ms |
2264 KB |
Output is correct |
41 |
Correct |
23 ms |
2008 KB |
Output is correct |
42 |
Correct |
23 ms |
1996 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 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 |
2264 KB |
Output is correct |
50 |
Correct |
30 ms |
2264 KB |
Output is correct |
51 |
Correct |
31 ms |
2280 KB |
Output is correct |
52 |
Correct |
25 ms |
2276 KB |
Output is correct |
53 |
Correct |
19 ms |
1756 KB |
Output is correct |
54 |
Correct |
19 ms |
1756 KB |
Output is correct |
55 |
Correct |
19 ms |
1756 KB |
Output is correct |
56 |
Correct |
8 ms |
604 KB |
Output is correct |
57 |
Correct |
7 ms |
604 KB |
Output is correct |
58 |
Correct |
23 ms |
2520 KB |
Output is correct |
59 |
Correct |
49 ms |
2008 KB |
Output is correct |
60 |
Correct |
46 ms |
2008 KB |
Output is correct |
61 |
Correct |
30 ms |
2008 KB |
Output is correct |
62 |
Correct |
332 ms |
2004 KB |
Output is correct |
63 |
Correct |
771 ms |
2212 KB |
Output is correct |
64 |
Correct |
103 ms |
3124 KB |
Output is correct |
65 |
Correct |
296 ms |
3032 KB |
Output is correct |
66 |
Correct |
656 ms |
3100 KB |
Output is correct |
67 |
Correct |
17 ms |
3288 KB |
Output is correct |
68 |
Correct |
51 ms |
4056 KB |
Output is correct |
69 |
Correct |
60 ms |
4056 KB |
Output is correct |
70 |
Correct |
26 ms |
3792 KB |
Output is correct |
71 |
Correct |
26 ms |
3792 KB |
Output is correct |
72 |
Correct |
25 ms |
3484 KB |
Output is correct |
73 |
Correct |
25 ms |
4048 KB |
Output is correct |
74 |
Correct |
25 ms |
3536 KB |
Output is correct |