#include <bits/stdc++.h>
using namespace std;
struct Arete
{
int from, to, cost, costFlip;
};
const int MAXN = 200;
const int INF = 2e9;
vector<int> adj[MAXN];
vector<int> adjRev[MAXN];
vector<Arete> aretes;
int nbSommets, nbAretes;
// O(M log N)
vector<int> distances, pred;
pair<vector<int>, vector<int>> dijsktra(int from, bool flip=false)
{
priority_queue<pair<int, int>> pq;
pq.push({0, from});
distances.assign(nbSommets, INF);
pred.assign(nbSommets, -1);
distances[from] = 0;
while (!pq.empty())
{
auto [curDis, iNoeud] = pq.top(); pq.pop();
curDis = -curDis;
if (curDis > distances[iNoeud])
continue;
for (auto iArete : (flip ? adjRev[iNoeud] : adj[iNoeud]))
{
int iFrere = flip ? aretes[iArete].from : aretes[iArete].to;
int nxtDis = curDis + aretes[iArete].cost;
if (nxtDis < distances[iFrere])
{
pq.push({-nxtDis, iFrere});
pred[iFrere] = iArete;
distances[iFrere] = nxtDis;
}
}
}
return make_pair(distances, pred);
}
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> nbSommets >> nbAretes;
aretes.resize(nbAretes);
for (auto &[from, to, cost, costFlip] : aretes)
{
cin >> from >> to >> cost >> costFlip;
--from, --to;
}
for (int iArete = 0; iArete < nbAretes; ++iArete)
{
adj[aretes[iArete].from].push_back(iArete);
adjRev[aretes[iArete].to].push_back(iArete);
}
auto [distances0, pred0] = dijsktra(0);
auto [distances0Rev, pred0Rev] = dijsktra(0, 1);
auto [distances1, pred1] = dijsktra(nbSommets-1);
auto [distances1Rev, pred1Rev] = dijsktra(nbSommets-1, 1);
int curSol = INF;
if (distances0[nbSommets-1] < INF and distances1[0] < INF)
curSol = distances0[nbSommets-1] + distances1[0];
vector<bool> aretesPrise(nbAretes);
if (distances0[nbSommets-1] != INF)
{
int cur = nbSommets-1;
while (cur)
{
assert(pred0[cur] != -1);
aretesPrise[pred0[cur]] = true;
cur = aretes[pred0[cur]].from;
}
}
if (distances1[0] != INF)
{
int cur = 0;
while (cur != nbSommets-1)
{
assert(pred1[cur] != -1);
aretesPrise[pred1[cur]] = true;
cur = aretes[pred1[cur]].from;
}
}
// O(NM log N)
for (int iArete = 0; iArete < nbAretes; ++iArete)
if (!aretesPrise[iArete])
{
int dis0 = distances0[nbSommets-1];
int dis1 = distances1[0];;
int iTo = aretes[iArete].to;
int iFrom = aretes[iArete].from;
if (distances0[iTo] < INF and distances1Rev[iFrom] < INF)
dis0 = min(dis0,
distances0[aretes[iArete].to] + distances1Rev[aretes[iArete].from]
+ aretes[iArete].cost);
if (distances1[iTo] < INF and distances0Rev[iFrom] < INF)
dis1 = min(dis1,
distances1[aretes[iArete].to] + distances0Rev[aretes[iArete].from]
+ aretes[iArete].cost);
if (dis0 < INF and dis1 < INF)
curSol = min(curSol, dis0 + dis1 + aretes[iArete].costFlip);
}
for (int iArete = 0; iArete < nbAretes; ++iArete)
if (aretesPrise[iArete])
{
int iFrom = aretes[iArete].from;
int iTo = aretes[iArete].to;
int posFrom = lower_bound(adj[iFrom].begin(), adj[iFrom].end(), iArete)
- adj[iFrom].begin();
int posTo = lower_bound(adjRev[iTo].begin(), adjRev[iTo].end(), iArete)
- adjRev[iTo].begin();
swap(aretes[iArete].from, aretes[iArete].to);
swap(adj[iFrom][posFrom], adj[iFrom].back());
swap(adjRev[iTo][posTo], adjRev[iTo].back());
adj[iFrom].pop_back();
adjRev[iTo].pop_back();
adjRev[iFrom].push_back(iArete);
adj[iTo].push_back(iArete);
auto [dis0, p0] = dijsktra(0);
auto [dis1, p1]= dijsktra(nbSommets-1);
if (dis1[0] < INF and dis0[nbSommets-1] < INF)
curSol = min(curSol, dis0[nbSommets-1] + dis1[0] + aretes[iArete].costFlip);
adj[iTo].pop_back();
adjRev[iFrom].pop_back();
adj[iFrom].push_back(iArete);
adjRev[iTo].push_back(iArete);
swap(adj[iFrom][posFrom], adj[iFrom].back());
swap(adjRev[iTo][posTo], adjRev[iTo].back());
swap(aretes[iArete].from, aretes[iArete].to);
}
if (curSol == INF)
curSol = -1;
cout << curSol << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
364 KB |
Output is correct |
10 |
Correct |
20 ms |
364 KB |
Output is correct |
11 |
Correct |
25 ms |
384 KB |
Output is correct |
12 |
Correct |
26 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
1772 KB |
Output is correct |
2 |
Correct |
27 ms |
1900 KB |
Output is correct |
3 |
Correct |
27 ms |
1900 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
23 ms |
1772 KB |
Output is correct |
10 |
Correct |
23 ms |
1772 KB |
Output is correct |
11 |
Correct |
28 ms |
1900 KB |
Output is correct |
12 |
Correct |
27 ms |
1772 KB |
Output is correct |
13 |
Correct |
26 ms |
1772 KB |
Output is correct |
14 |
Correct |
27 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
19 ms |
1388 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
22 ms |
1900 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
20 ms |
1772 KB |
Output is correct |
9 |
Correct |
20 ms |
1792 KB |
Output is correct |
10 |
Correct |
21 ms |
1772 KB |
Output is correct |
11 |
Correct |
21 ms |
1772 KB |
Output is correct |
12 |
Correct |
21 ms |
1900 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 |
364 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 |
21 ms |
1920 KB |
Output is correct |
20 |
Correct |
21 ms |
1900 KB |
Output is correct |
21 |
Correct |
24 ms |
1772 KB |
Output is correct |
22 |
Correct |
21 ms |
1772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
364 KB |
Output is correct |
10 |
Correct |
20 ms |
364 KB |
Output is correct |
11 |
Correct |
25 ms |
384 KB |
Output is correct |
12 |
Correct |
26 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
26 ms |
1772 KB |
Output is correct |
18 |
Correct |
27 ms |
1900 KB |
Output is correct |
19 |
Correct |
27 ms |
1900 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
0 ms |
364 KB |
Output is correct |
25 |
Correct |
23 ms |
1772 KB |
Output is correct |
26 |
Correct |
23 ms |
1772 KB |
Output is correct |
27 |
Correct |
28 ms |
1900 KB |
Output is correct |
28 |
Correct |
27 ms |
1772 KB |
Output is correct |
29 |
Correct |
26 ms |
1772 KB |
Output is correct |
30 |
Correct |
27 ms |
1900 KB |
Output is correct |
31 |
Correct |
3 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
19 ms |
1388 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
22 ms |
1900 KB |
Output is correct |
36 |
Correct |
0 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
20 ms |
1772 KB |
Output is correct |
39 |
Correct |
20 ms |
1792 KB |
Output is correct |
40 |
Correct |
21 ms |
1772 KB |
Output is correct |
41 |
Correct |
21 ms |
1772 KB |
Output is correct |
42 |
Correct |
21 ms |
1900 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 |
364 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 |
21 ms |
1920 KB |
Output is correct |
50 |
Correct |
21 ms |
1900 KB |
Output is correct |
51 |
Correct |
24 ms |
1772 KB |
Output is correct |
52 |
Correct |
21 ms |
1772 KB |
Output is correct |
53 |
Correct |
32 ms |
1772 KB |
Output is correct |
54 |
Correct |
31 ms |
1772 KB |
Output is correct |
55 |
Correct |
29 ms |
1772 KB |
Output is correct |
56 |
Correct |
2 ms |
364 KB |
Output is correct |
57 |
Correct |
2 ms |
364 KB |
Output is correct |
58 |
Correct |
128 ms |
1516 KB |
Output is correct |
59 |
Correct |
153 ms |
1532 KB |
Output is correct |
60 |
Correct |
206 ms |
1644 KB |
Output is correct |
61 |
Correct |
127 ms |
1644 KB |
Output is correct |
62 |
Correct |
145 ms |
1536 KB |
Output is correct |
63 |
Correct |
203 ms |
1644 KB |
Output is correct |
64 |
Correct |
747 ms |
1948 KB |
Output is correct |
65 |
Correct |
797 ms |
1872 KB |
Output is correct |
66 |
Execution timed out |
1042 ms |
1868 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |