답안 #379547

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379547 2021-03-18T14:00:42 Z peijar Olympic Bus (JOI20_ho_t4) C++17
100 / 100
967 ms 4992 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)
{
	priority_queue<pair<int, int>> pq;
	pq.push({0, from});
	vector<int> dis(nbSommets, INF);
	dis[from] = 0;
	while (!pq.empty())
	{
		auto [curDis, iNoeud] = pq.top(); pq.pop();
		curDis = -curDis;
		if (curDis > dis[iNoeud]) continue;
		if (iNoeud == to) return curDis;
		for (int iFrere(0); iFrere < nbSommets; ++iFrere)
			if (curDis + matAdj[iNoeud][iFrere] < dis[iFrere])
			{
				dis[iFrere] = curDis + matAdj[iNoeud][iFrere];
				pq.push({-dis[iFrere], 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 748 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 748 KB Output is correct
4 Correct 3 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 40 ms 748 KB Output is correct
11 Correct 53 ms 748 KB Output is correct
12 Correct 50 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 2 ms 748 KB Output is correct
16 Correct 3 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 3564 KB Output is correct
2 Correct 27 ms 3564 KB Output is correct
3 Correct 27 ms 3564 KB Output is correct
4 Correct 2 ms 876 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 1 ms 364 KB Output is correct
9 Correct 28 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 27 ms 3692 KB Output is correct
13 Correct 27 ms 3564 KB Output is correct
14 Correct 27 ms 3692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 748 KB Output is correct
2 Correct 2 ms 896 KB Output is correct
3 Correct 19 ms 2796 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 24 ms 3564 KB Output is correct
6 Correct 1 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 23 ms 3564 KB Output is correct
12 Correct 23 ms 3564 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 0 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 24 ms 3692 KB Output is correct
20 Correct 31 ms 3692 KB Output is correct
21 Correct 25 ms 3564 KB Output is correct
22 Correct 23 ms 3564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 748 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 748 KB Output is correct
4 Correct 3 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 40 ms 748 KB Output is correct
11 Correct 53 ms 748 KB Output is correct
12 Correct 50 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 2 ms 748 KB Output is correct
16 Correct 3 ms 748 KB Output is correct
17 Correct 26 ms 3564 KB Output is correct
18 Correct 27 ms 3564 KB Output is correct
19 Correct 27 ms 3564 KB Output is correct
20 Correct 2 ms 876 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 1 ms 364 KB Output is correct
25 Correct 28 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 27 ms 3692 KB Output is correct
29 Correct 27 ms 3564 KB Output is correct
30 Correct 27 ms 3692 KB Output is correct
31 Correct 3 ms 748 KB Output is correct
32 Correct 2 ms 896 KB Output is correct
33 Correct 19 ms 2796 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 24 ms 3564 KB Output is correct
36 Correct 1 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 23 ms 3564 KB Output is correct
42 Correct 23 ms 3564 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 0 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 24 ms 3692 KB Output is correct
50 Correct 31 ms 3692 KB Output is correct
51 Correct 25 ms 3564 KB Output is correct
52 Correct 23 ms 3564 KB Output is correct
53 Correct 26 ms 3564 KB Output is correct
54 Correct 28 ms 3564 KB Output is correct
55 Correct 27 ms 3564 KB Output is correct
56 Correct 3 ms 748 KB Output is correct
57 Correct 2 ms 748 KB Output is correct
58 Correct 124 ms 2924 KB Output is correct
59 Correct 142 ms 2924 KB Output is correct
60 Correct 198 ms 2944 KB Output is correct
61 Correct 120 ms 2924 KB Output is correct
62 Correct 140 ms 2924 KB Output is correct
63 Correct 195 ms 3052 KB Output is correct
64 Correct 682 ms 3736 KB Output is correct
65 Correct 746 ms 3656 KB Output is correct
66 Correct 967 ms 3804 KB Output is correct
67 Correct 17 ms 3432 KB Output is correct
68 Correct 24 ms 4460 KB Output is correct
69 Correct 24 ms 4460 KB Output is correct
70 Correct 36 ms 4716 KB Output is correct
71 Correct 28 ms 4716 KB Output is correct
72 Correct 33 ms 4716 KB Output is correct
73 Correct 31 ms 4992 KB Output is correct
74 Correct 29 ms 4844 KB Output is correct