답안 #826399

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826399 2023-08-15T14:25:17 Z MohamedAliSaidane Olympic Bus (JOI20_ho_t4) C++14
100 / 100
296 ms 3532 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second

const int nax = 205;
const int fax = 5e4 + 4;
const int INF = 1e9;
int n, m;
int seen[nax];
int sp1[nax], ps1[nax], sp2[nax], ps2[nax];
vector<int> adj[nax], rev_adj[nax];
int U[fax], V[fax], is_on_path[fax];
int C[fax];
ll D[fax];
int previ[nax][4];
int vis[nax][nax];

void calculate_sp(int up = -1, int vp = -1, int edg = -1)
{
	for(int i = 1; i <= n; i++)
	{
		sp1[i] = ps1[i] = sp2[i] = ps2[i] = INF;
		seen[i] = 0;
	}

	sp1[1] = ps1[1] = sp2[n] = ps2[n] = 0ll;
	previ[1][0] = previ[1][1] = -1;

	for(int loop = 1; loop <= n; loop++)
    {
        pair<int,int> E = {INF, n};
        for(int i = 1; i <= n; i++)
        {
            if(!seen[i] && sp1[i] < E.ff)
                E = {sp1[i], i};
        }
		int node = E.ss;
		if(seen[node])
			continue;
		seen[node] = 1;
		int path = E.ff;
		if(previ[node][0] != -1)
			is_on_path[previ[node][0]] = 1;
		for(auto e: adj[node])
		{
			if(e == edg)
				continue;
			int v = V[e];
			if(sp1[v] <= path + C[e])
				continue;
			sp1[v] = path + C[e];
			previ[v][0] = e;
		}
		if(node == vp)
		{
			int v = up;
			if(sp1[v] <= path + C[edg])
				continue;
			sp1[v] = path + C[edg];
		}
	}
	for(int i= 1; i <= n; i++)
		seen[i] = 0;

	for(int loop = 1; loop <= n; loop++)
    {
        pair<int,int> E = {INF, n};
        for(int i = 1; i <= n; i++)
        {
            if(!seen[i] && ps1[i] < E.ff)
                E = {ps1[i], i};
        }
		int node = E.ss;
		if(seen[node])
			continue;
		seen[node] = 1;
		int path = E.ff;
		if(previ[node][1] != -1)
			is_on_path[previ[node][1]] = 1;
		for(auto e: rev_adj[node])
		{
			if(e == edg)
				continue;
			int v = U[e];
			if(ps1[v] <= path + C[e])
				continue;
			ps1[v] = path + C[e];
		}
		if(node == up)
		{
			int v = vp;
			if(ps1[v] <= path + C[edg])
				continue;
			ps1[v] = path + C[edg];
		}
	}

	previ[n][2] = previ[n][3] = -1;
	for(int i =1; i <= n; i++)
		seen[i] = 0;

	for(int loop = 1; loop <= n; loop++)
    {
        pair<int,int> E = {INF, n};
        for(int i = 1; i <= n; i++)
        {
            if(!seen[i] && sp2[i] < E.ff)
                E = {sp2[i], i};
        }
		int node = E.ss;
		if(seen[node])
			continue;
		seen[node] = 1;
		int path = E.ff;
		if(previ[node][2] != -1)
			is_on_path[previ[node][2]] = 1;
		for(auto e: adj[node])
		{
			if(e == edg)
				continue;
			int v = V[e];
			if(sp2[v] <= path + C[e])
				continue;
			sp2[v] = path + C[e];
			previ[v][2] = e;
		}
		if(node == vp)
		{
			int v = up;
			if(sp2[v] <= path + C[edg])
				continue;
			sp2[v] = path + C[edg];
		}
	}
	for(int i =1; i <= n; i++)
		seen[i] = 0;

	for(int loop = 1; loop <= n; loop++)
    {
        pair<int,int> E = {INF, n};
        for(int i = 1; i <= n; i++)
        {
            if(!seen[i] && ps2[i] < E.ff)
                E = {ps2[i], i};
        }
		int node = E.ss;
		if(seen[node])
			continue;
		seen[node] = 1;
		int path = E.ff;
		if(previ[node][3] != -1)
			is_on_path[previ[node][3]] = 1;
		for(auto e: rev_adj[node])
		{
			if(e == edg)
				continue;
			int v = U[e];
			if(ps2[v] <= path + C[e])
				continue;
			ps2[v] = path + C[e];
			previ[v][3] = e;
		}
		if(node == up)
		{
			int v = vp;
			if(ps2[v] <= path + C[edg])
				continue;
			ps2[v] = path + C[edg];
		}

	}
	assert(sp1[1] == 0);
	assert(sp2[n] == 0);
	assert(sp1[n] == ps2[1]);
	assert(sp2[1] == ps1[n]);
}
void solve()
{
	cin >> n >> m;
	vector<pair<int,int>> VEC;
	for(int i = 0; i < m; i++)
	{
		cin >> U[i] >> V[i] >> C[i] >> D[i];
		VEC.pb({C[i], i});
	}
	sort(all(VEC));
	for(auto p: VEC)
	{
		int e = p.ss;
		if(vis[U[e]][V[e]] == 2)
			continue;
		adj[U[e]].pb(e);
		rev_adj[V[e]].pb(e);
		vis[U[e]][V[e]]++;
	}
	calculate_sp();
	vector<int> NON, YES;
	for(int i = 0; i < m; i++)
	{
		if(is_on_path[i])
			YES.pb(i);
		else
			NON.pb(i);
	}
	assert((int)(YES.size()) <= 4 * n);
	ll ans = sp1[n] >= INF || sp2[1] >= INF? 2ll * INF: sp1[n] + sp2[1];
	for(auto i: NON)
	{
		int u = U[i], v = V[i];
		ll c =  C[i], d = D[i];
		ll un = sp1[n];
		ll uV = sp1[v];
		ll Un = ps2[u];
		ll nu = sp2[1];
		ll nV = sp2[v];
		ll Uu = ps1[u];
		if((un >= INF && uV + c + Un >= INF) || (nu >= INF && nV + c + Uu >= INF))
				continue;
		ll rep = d + min(un, uV + c + Un) + min(nu, nV + c + Uu);
		ans = min(ans, rep);
	}
	for(auto i: YES)
	{

		calculate_sp(U[i], V[i], i);
		if(sp1[n] >= INF || sp2[1] >= INF)
			continue;
		ans = min(ans, (ll)(sp1[n]) + (ll)(sp2[1]) + D[i]);
		//cout << sp1[n] << ' ' << sp2[1] << '\n';
	}
	if(ans >= 2ll * INF)
		cout << "-1";
	else
		cout << ans;
}

int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int tt = 1; // cin >> tt;
	while(tt--)
		solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 596 KB Output is correct
2 Correct 13 ms 468 KB Output is correct
3 Correct 126 ms 596 KB Output is correct
4 Correct 133 ms 596 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 17 ms 468 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 392 KB Output is correct
10 Correct 71 ms 596 KB Output is correct
11 Correct 98 ms 596 KB Output is correct
12 Correct 90 ms 596 KB Output is correct
13 Correct 61 ms 596 KB Output is correct
14 Correct 108 ms 600 KB Output is correct
15 Correct 94 ms 596 KB Output is correct
16 Correct 102 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 261 ms 3384 KB Output is correct
2 Correct 253 ms 3372 KB Output is correct
3 Correct 245 ms 3496 KB Output is correct
4 Correct 133 ms 716 KB Output is correct
5 Correct 98 ms 596 KB Output is correct
6 Correct 26 ms 576 KB Output is correct
7 Correct 5 ms 468 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 100 ms 3404 KB Output is correct
10 Correct 98 ms 3396 KB Output is correct
11 Correct 160 ms 3392 KB Output is correct
12 Correct 186 ms 3488 KB Output is correct
13 Correct 154 ms 3488 KB Output is correct
14 Correct 176 ms 3532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 596 KB Output is correct
2 Correct 31 ms 468 KB Output is correct
3 Correct 233 ms 3016 KB Output is correct
4 Correct 22 ms 468 KB Output is correct
5 Correct 264 ms 3372 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 148 ms 3480 KB Output is correct
9 Correct 123 ms 3508 KB Output is correct
10 Correct 217 ms 3372 KB Output is correct
11 Correct 156 ms 3372 KB Output is correct
12 Correct 153 ms 3284 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 197 ms 3276 KB Output is correct
20 Correct 163 ms 3264 KB Output is correct
21 Correct 173 ms 3368 KB Output is correct
22 Correct 175 ms 3324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 596 KB Output is correct
2 Correct 13 ms 468 KB Output is correct
3 Correct 126 ms 596 KB Output is correct
4 Correct 133 ms 596 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 17 ms 468 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 392 KB Output is correct
10 Correct 71 ms 596 KB Output is correct
11 Correct 98 ms 596 KB Output is correct
12 Correct 90 ms 596 KB Output is correct
13 Correct 61 ms 596 KB Output is correct
14 Correct 108 ms 600 KB Output is correct
15 Correct 94 ms 596 KB Output is correct
16 Correct 102 ms 596 KB Output is correct
17 Correct 261 ms 3384 KB Output is correct
18 Correct 253 ms 3372 KB Output is correct
19 Correct 245 ms 3496 KB Output is correct
20 Correct 133 ms 716 KB Output is correct
21 Correct 98 ms 596 KB Output is correct
22 Correct 26 ms 576 KB Output is correct
23 Correct 5 ms 468 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 100 ms 3404 KB Output is correct
26 Correct 98 ms 3396 KB Output is correct
27 Correct 160 ms 3392 KB Output is correct
28 Correct 186 ms 3488 KB Output is correct
29 Correct 154 ms 3488 KB Output is correct
30 Correct 176 ms 3532 KB Output is correct
31 Correct 80 ms 596 KB Output is correct
32 Correct 31 ms 468 KB Output is correct
33 Correct 233 ms 3016 KB Output is correct
34 Correct 22 ms 468 KB Output is correct
35 Correct 264 ms 3372 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 148 ms 3480 KB Output is correct
39 Correct 123 ms 3508 KB Output is correct
40 Correct 217 ms 3372 KB Output is correct
41 Correct 156 ms 3372 KB Output is correct
42 Correct 153 ms 3284 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 197 ms 3276 KB Output is correct
50 Correct 163 ms 3264 KB Output is correct
51 Correct 173 ms 3368 KB Output is correct
52 Correct 175 ms 3324 KB Output is correct
53 Correct 289 ms 3292 KB Output is correct
54 Correct 296 ms 3372 KB Output is correct
55 Correct 288 ms 3368 KB Output is correct
56 Correct 174 ms 596 KB Output is correct
57 Correct 176 ms 596 KB Output is correct
58 Correct 129 ms 3040 KB Output is correct
59 Correct 142 ms 3020 KB Output is correct
60 Correct 235 ms 3036 KB Output is correct
61 Correct 126 ms 3036 KB Output is correct
62 Correct 185 ms 3044 KB Output is correct
63 Correct 194 ms 2924 KB Output is correct
64 Correct 169 ms 3084 KB Output is correct
65 Correct 173 ms 3088 KB Output is correct
66 Correct 227 ms 3088 KB Output is correct
67 Correct 13 ms 2092 KB Output is correct
68 Correct 108 ms 3376 KB Output is correct
69 Correct 105 ms 3308 KB Output is correct
70 Correct 214 ms 3364 KB Output is correct
71 Correct 188 ms 3144 KB Output is correct
72 Correct 162 ms 3204 KB Output is correct
73 Correct 164 ms 3264 KB Output is correct
74 Correct 196 ms 3148 KB Output is correct