Submission #379537

# Submission time Handle Problem Language Result Execution time Memory
379537 2021-03-18T13:25:58 Z peijar Olympic Bus (JOI20_ho_t4) C++17
37 / 100
1000 ms 4588 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

struct Arete
{
	int from, to, cost, costFlip;
};

const int MAXN = 200;
const int INF = 1e18;

vector<int> adj[MAXN];
vector<int> adjRev[MAXN];
vector<Arete> aretes;
int nbSommets, nbAretes;

pair<vector<int>, vector<int>> dijsktra(int from, bool flip=false)
{
	priority_queue<pair<int, int>> pq;
	pq.push({0, from});
	vector<int> distance(nbSommets, INF);
	vector<int> pred(nbSommets, -1);
	distance[from] = 0;
	while (!pq.empty())
	{
		auto [curDis, iNoeud] = pq.top(); pq.pop();
		curDis = -curDis;
		if (curDis > distance[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 < distance[iFrere])
			{
				pq.push({-nxtDis, iFrere});
				pred[iFrere] = iArete;
				distance[iFrere] = nxtDis;
			}
		}
	}
	return make_pair(distance, 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 = distances0[nbSommets-1] + distances1[0];
	curSol = min(curSol, INF);
	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;
		}
	}
	
	for (int iArete = 0; iArete < nbAretes; ++iArete) 
		if (!aretesPrise[iArete])
		{
			int dis0 = min(distances0[nbSommets-1],
					distances0[aretes[iArete].to] + distances1Rev[aretes[iArete].from]
					+ aretes[iArete].cost);
			int dis1 = min(distances1[0],
					distances1[aretes[iArete].to] + distances0Rev[aretes[iArete].from]
					+ aretes[iArete].cost);
			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);

			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 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 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 492 KB Output is correct
11 Correct 26 ms 492 KB Output is correct
12 Correct 31 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 3308 KB Output is correct
2 Correct 29 ms 3308 KB Output is correct
3 Correct 30 ms 3308 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 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 24 ms 4460 KB Output is correct
10 Correct 30 ms 4460 KB Output is correct
11 Correct 30 ms 4460 KB Output is correct
12 Correct 31 ms 4460 KB Output is correct
13 Correct 31 ms 4480 KB Output is correct
14 Correct 28 ms 4588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 20 ms 3308 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 25 ms 4224 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 22 ms 4204 KB Output is correct
9 Correct 21 ms 4204 KB Output is correct
10 Correct 23 ms 4204 KB Output is correct
11 Correct 26 ms 4204 KB Output is correct
12 Correct 29 ms 4204 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 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 27 ms 4204 KB Output is correct
20 Correct 28 ms 4204 KB Output is correct
21 Correct 23 ms 4204 KB Output is correct
22 Correct 23 ms 4204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 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 492 KB Output is correct
11 Correct 26 ms 492 KB Output is correct
12 Correct 31 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 30 ms 3308 KB Output is correct
18 Correct 29 ms 3308 KB Output is correct
19 Correct 30 ms 3308 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 24 ms 4460 KB Output is correct
26 Correct 30 ms 4460 KB Output is correct
27 Correct 30 ms 4460 KB Output is correct
28 Correct 31 ms 4460 KB Output is correct
29 Correct 31 ms 4480 KB Output is correct
30 Correct 28 ms 4588 KB Output is correct
31 Correct 2 ms 492 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 20 ms 3308 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 25 ms 4224 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 22 ms 4204 KB Output is correct
39 Correct 21 ms 4204 KB Output is correct
40 Correct 23 ms 4204 KB Output is correct
41 Correct 26 ms 4204 KB Output is correct
42 Correct 29 ms 4204 KB Output is correct
43 Correct 1 ms 492 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 1 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 27 ms 4204 KB Output is correct
50 Correct 28 ms 4204 KB Output is correct
51 Correct 23 ms 4204 KB Output is correct
52 Correct 23 ms 4204 KB Output is correct
53 Correct 29 ms 4204 KB Output is correct
54 Correct 34 ms 4204 KB Output is correct
55 Correct 33 ms 4332 KB Output is correct
56 Correct 2 ms 632 KB Output is correct
57 Correct 2 ms 640 KB Output is correct
58 Correct 173 ms 3564 KB Output is correct
59 Correct 178 ms 3564 KB Output is correct
60 Correct 258 ms 3436 KB Output is correct
61 Correct 161 ms 3436 KB Output is correct
62 Correct 176 ms 3436 KB Output is correct
63 Correct 238 ms 3436 KB Output is correct
64 Correct 815 ms 4160 KB Output is correct
65 Correct 897 ms 4220 KB Output is correct
66 Execution timed out 1080 ms 4228 KB Time limit exceeded
67 Halted 0 ms 0 KB -