답안 #696623

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696623 2023-02-07T01:44:50 Z mousey Autobus (COCI22_autobus) C++14
70 / 70
390 ms 6312 KB
#include <bits/stdc++.h>
#define int long long
#define vi vector <int>
#define vv vector <vi>
#define pi pair <int, int>
#define vip vector <pi>
#define pb push_back
#define f first
#define s second
#define nl cout<<"\n"
using namespace std;

const int mod=1e9+7;
const double eps=1e-9;
const int N=70;
int n, m, k, q, w[N+5][N+5];
int dist[N+5][N+5][N+5];
bool used[N+5][N+5];

void input()
{
	cin >> n >> m;
	for(int i = 1; i <= m; i++)
	{
		int u, v, t;
		cin >> u >> v >> t;
		if(!w[u][v]) w[u][v]=t;
		else w[u][v]=min(w[u][v], t);
	}
	cin >> k >> q;
}

void solve()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            for(int p = 0; p <= n; p++)
            {
                dist[i][j][p]=LLONG_MAX;
            }
        }
    }
	for(int i = 1; i <= n; i++)
	{
		priority_queue <pair <int, pi> > pq;
		pq.push({LLONG_MAX-0, {i, 0}});
		dist[i][i][0]=0;
		while(!pq.empty())
		{
			pi p=pq.top().s;
			pq.pop();
			if(used[p.f][p.s]) continue;
			if(p.s==n) continue;
			for(int j = 1; j <= n; j++)
			{
				if(!w[p.f][j]) continue;
				if(dist[i][j][p.s+1]>dist[i][p.f][p.s]+w[p.f][j])
				{
					dist[i][j][p.s+1]=dist[i][p.f][p.s]+w[p.f][j];
					pq.push({LLONG_MAX-dist[i][j][p.s+1], {j, p.s+1}});
				}
			}
			used[p.f][p.s]=true;
		}
		for(int j = 1; j <= n; j++)
		{
			used[j][0]=false;
			for(int p = 1; p <= n; p++)
			{
				used[j][p]=false;
				dist[i][j][p]=min(dist[i][j][p-1], dist[i][j][p]);
			}
		}
	}
	if(k>n) k=n;
	while(q--)
	{
		int u, v;
		cin >> u >> v;
		if(dist[u][v][k]==LLONG_MAX) cout << -1;
		else cout << dist[u][v][k];
		nl;
	}
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
//    freopen("math.inp", "r", stdin);
//    freopen("math.out", "w", stdout);
//    cin >> t;
    for(int i = 1; i <= t; i++)
    {
        input();
        solve();
        nl;
    }
}

/*
6
a
b
c
d
e
f
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 3584 KB Output is correct
2 Correct 125 ms 3572 KB Output is correct
3 Correct 138 ms 3572 KB Output is correct
4 Correct 235 ms 3580 KB Output is correct
5 Correct 220 ms 3584 KB Output is correct
6 Correct 327 ms 4328 KB Output is correct
7 Correct 308 ms 4276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 112 ms 3816 KB Output is correct
8 Correct 124 ms 3824 KB Output is correct
9 Correct 185 ms 3576 KB Output is correct
10 Correct 187 ms 3580 KB Output is correct
11 Correct 279 ms 4164 KB Output is correct
12 Correct 290 ms 4344 KB Output is correct
13 Correct 343 ms 5372 KB Output is correct
14 Correct 345 ms 5372 KB Output is correct
15 Correct 342 ms 5412 KB Output is correct
16 Correct 341 ms 5196 KB Output is correct
17 Correct 280 ms 4836 KB Output is correct
18 Correct 288 ms 4828 KB Output is correct
19 Correct 309 ms 5048 KB Output is correct
20 Correct 289 ms 4840 KB Output is correct
21 Correct 271 ms 4732 KB Output is correct
22 Correct 282 ms 4740 KB Output is correct
23 Correct 282 ms 4736 KB Output is correct
24 Correct 267 ms 4740 KB Output is correct
25 Correct 331 ms 5000 KB Output is correct
26 Correct 334 ms 4940 KB Output is correct
27 Correct 313 ms 5068 KB Output is correct
28 Correct 316 ms 5012 KB Output is correct
29 Correct 390 ms 5356 KB Output is correct
30 Correct 363 ms 5388 KB Output is correct
31 Correct 354 ms 5492 KB Output is correct
32 Correct 352 ms 5480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 122 ms 3584 KB Output is correct
8 Correct 125 ms 3572 KB Output is correct
9 Correct 138 ms 3572 KB Output is correct
10 Correct 235 ms 3580 KB Output is correct
11 Correct 220 ms 3584 KB Output is correct
12 Correct 327 ms 4328 KB Output is correct
13 Correct 308 ms 4276 KB Output is correct
14 Correct 112 ms 3816 KB Output is correct
15 Correct 124 ms 3824 KB Output is correct
16 Correct 185 ms 3576 KB Output is correct
17 Correct 187 ms 3580 KB Output is correct
18 Correct 279 ms 4164 KB Output is correct
19 Correct 290 ms 4344 KB Output is correct
20 Correct 343 ms 5372 KB Output is correct
21 Correct 345 ms 5372 KB Output is correct
22 Correct 342 ms 5412 KB Output is correct
23 Correct 341 ms 5196 KB Output is correct
24 Correct 280 ms 4836 KB Output is correct
25 Correct 288 ms 4828 KB Output is correct
26 Correct 309 ms 5048 KB Output is correct
27 Correct 289 ms 4840 KB Output is correct
28 Correct 271 ms 4732 KB Output is correct
29 Correct 282 ms 4740 KB Output is correct
30 Correct 282 ms 4736 KB Output is correct
31 Correct 267 ms 4740 KB Output is correct
32 Correct 331 ms 5000 KB Output is correct
33 Correct 334 ms 4940 KB Output is correct
34 Correct 313 ms 5068 KB Output is correct
35 Correct 316 ms 5012 KB Output is correct
36 Correct 390 ms 5356 KB Output is correct
37 Correct 363 ms 5388 KB Output is correct
38 Correct 354 ms 5492 KB Output is correct
39 Correct 352 ms 5480 KB Output is correct
40 Correct 105 ms 3660 KB Output is correct
41 Correct 111 ms 3572 KB Output is correct
42 Correct 180 ms 3572 KB Output is correct
43 Correct 188 ms 3568 KB Output is correct
44 Correct 289 ms 4272 KB Output is correct
45 Correct 291 ms 4216 KB Output is correct
46 Correct 360 ms 5492 KB Output is correct
47 Correct 375 ms 5296 KB Output is correct
48 Correct 359 ms 5272 KB Output is correct
49 Correct 347 ms 5328 KB Output is correct
50 Correct 279 ms 4944 KB Output is correct
51 Correct 281 ms 5084 KB Output is correct
52 Correct 273 ms 5016 KB Output is correct
53 Correct 282 ms 5036 KB Output is correct
54 Correct 289 ms 4904 KB Output is correct
55 Correct 268 ms 4892 KB Output is correct
56 Correct 267 ms 5000 KB Output is correct
57 Correct 272 ms 5188 KB Output is correct
58 Correct 325 ms 5424 KB Output is correct
59 Correct 331 ms 5504 KB Output is correct
60 Correct 356 ms 5400 KB Output is correct
61 Correct 337 ms 5532 KB Output is correct
62 Correct 359 ms 6128 KB Output is correct
63 Correct 350 ms 6312 KB Output is correct
64 Correct 355 ms 6076 KB Output is correct
65 Correct 363 ms 6008 KB Output is correct