Submission #379548

# Submission time Handle Problem Language Result Execution time Memory
379548 2021-03-18T14:02:59 Z peijar Olympic Bus (JOI20_ho_t4) C++17
100 / 100
181 ms 3804 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];
int matAdj[MAXN][MAXN];
vector<Arete> aretes;
int nbSommets, nbAretes;

// O(M log N)

pair<vector<int>, vector<int>> dijsktra(int from, bool flip=false)
{
	priority_queue<pair<int, int>> pq;
	pq.push({0, from});
	vector<int> distances, pred;
	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(move(distances), move(pred));
}

int disjk(int from, int to)
{
	vector<bool> vu(nbSommets);
	priority_queue<pair<int, int>> pq;
	pq.push({0, from});
	vector<int> dis(nbSommets, INF);
	dis[from] = 0;
	for (int iter(0); iter < nbSommets; ++iter)
	{
		int iBest(-1);
		for (int iNoeud(0); iNoeud < nbSommets; ++iNoeud)
			if (!vu[iNoeud] and (iBest == -1 or dis[iBest] > dis[iNoeud]))
				iBest = iNoeud;
		int curDis = dis[iBest];
		int iNoeud = iBest;
		if (iNoeud == to) return curDis;
		vu[iNoeud] = true;
		for (int iFrere(0); iFrere < nbSommets; ++iFrere)
			dis[iFrere] = min(dis[iFrere], curDis + matAdj[iNoeud][iFrere]);
	}
	return INF;
}

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)
		{
			aretesPrise[pred0[cur]] = true;
			cur = aretes[pred0[cur]].from;
		}
	}

	if (distances1[0] != INF)
	{
		int cur = 0;
		while (cur != nbSommets-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);
		}

	// If we 

	for (int iArete = 0; iArete < nbAretes; ++iArete) 
		if (aretesPrise[iArete])
		{
			for (int i = 0; i < nbSommets; ++i) 
				for (int j = 0; j < nbSommets; ++j) 
					matAdj[i][j] = INF;
			for (int iArete2 = 0; iArete2 < nbAretes; ++iArete2)
			{
				int u = aretes[iArete2].from;
				int v = aretes[iArete2].to;
				if (iArete2 == iArete)
					swap(u, v);
				matAdj[u][v] = min(matAdj[u][v], aretes[iArete2].cost);
			}
			curSol = min(curSol, aretes[iArete].costFlip + disjk(0, nbSommets-1) + disjk(nbSommets-1, 0));
		}

	if (curSol == INF)
		curSol = -1;
	cout << curSol << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 748 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 3 ms 748 KB Output is correct
4 Correct 5 ms 748 KB Output is correct
5 Correct 1 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 1 ms 364 KB Output is correct
10 Correct 63 ms 748 KB Output is correct
11 Correct 86 ms 748 KB Output is correct
12 Correct 78 ms 748 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 4 ms 748 KB Output is correct
15 Correct 3 ms 748 KB Output is correct
16 Correct 4 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 3564 KB Output is correct
2 Correct 27 ms 3564 KB Output is correct
3 Correct 27 ms 3692 KB Output is correct
4 Correct 2 ms 748 KB Output is correct
5 Correct 4 ms 748 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 3308 KB Output is correct
10 Correct 23 ms 3308 KB Output is correct
11 Correct 29 ms 3564 KB Output is correct
12 Correct 28 ms 3692 KB Output is correct
13 Correct 28 ms 3564 KB Output is correct
14 Correct 28 ms 3804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 780 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 19 ms 2796 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 23 ms 3564 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 21 ms 3180 KB Output is correct
9 Correct 21 ms 3180 KB Output is correct
10 Correct 23 ms 3564 KB Output is correct
11 Correct 22 ms 3564 KB Output is correct
12 Correct 23 ms 3564 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 24 ms 3564 KB Output is correct
20 Correct 22 ms 3564 KB Output is correct
21 Correct 23 ms 3564 KB Output is correct
22 Correct 22 ms 3564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 748 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 3 ms 748 KB Output is correct
4 Correct 5 ms 748 KB Output is correct
5 Correct 1 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 1 ms 364 KB Output is correct
10 Correct 63 ms 748 KB Output is correct
11 Correct 86 ms 748 KB Output is correct
12 Correct 78 ms 748 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 4 ms 748 KB Output is correct
15 Correct 3 ms 748 KB Output is correct
16 Correct 4 ms 748 KB Output is correct
17 Correct 25 ms 3564 KB Output is correct
18 Correct 27 ms 3564 KB Output is correct
19 Correct 27 ms 3692 KB Output is correct
20 Correct 2 ms 748 KB Output is correct
21 Correct 4 ms 748 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 3308 KB Output is correct
26 Correct 23 ms 3308 KB Output is correct
27 Correct 29 ms 3564 KB Output is correct
28 Correct 28 ms 3692 KB Output is correct
29 Correct 28 ms 3564 KB Output is correct
30 Correct 28 ms 3804 KB Output is correct
31 Correct 5 ms 780 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 19 ms 2796 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 23 ms 3564 KB Output is correct
36 Correct 0 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 21 ms 3180 KB Output is correct
39 Correct 21 ms 3180 KB Output is correct
40 Correct 23 ms 3564 KB Output is correct
41 Correct 22 ms 3564 KB Output is correct
42 Correct 23 ms 3564 KB Output is correct
43 Correct 1 ms 492 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 0 ms 364 KB Output is correct
46 Correct 0 ms 364 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 24 ms 3564 KB Output is correct
50 Correct 22 ms 3564 KB Output is correct
51 Correct 23 ms 3564 KB Output is correct
52 Correct 22 ms 3564 KB Output is correct
53 Correct 25 ms 3564 KB Output is correct
54 Correct 29 ms 3564 KB Output is correct
55 Correct 27 ms 3564 KB Output is correct
56 Correct 4 ms 768 KB Output is correct
57 Correct 2 ms 748 KB Output is correct
58 Correct 118 ms 2924 KB Output is correct
59 Correct 136 ms 2924 KB Output is correct
60 Correct 178 ms 3052 KB Output is correct
61 Correct 118 ms 2924 KB Output is correct
62 Correct 137 ms 2924 KB Output is correct
63 Correct 181 ms 3052 KB Output is correct
64 Correct 124 ms 3140 KB Output is correct
65 Correct 140 ms 3252 KB Output is correct
66 Correct 179 ms 3156 KB Output is correct
67 Correct 17 ms 2660 KB Output is correct
68 Correct 23 ms 3180 KB Output is correct
69 Correct 23 ms 3308 KB Output is correct
70 Correct 29 ms 3564 KB Output is correct
71 Correct 28 ms 3564 KB Output is correct
72 Correct 27 ms 3564 KB Output is correct
73 Correct 27 ms 3564 KB Output is correct
74 Correct 29 ms 3564 KB Output is correct