답안 #452477

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
452477 2021-08-04T04:49:51 Z jwvg0425 날다람쥐 (JOI14_ho_t4) C++17
100 / 100
337 ms 29400 KB
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second

using namespace std;

template<typename T, typename Pr = less<T>>
using pq = priority_queue<T, vector<T>, Pr>;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

vector<ii64> edge[100005];
i64 x;

struct Data
{
	i64 t, fh, h;
	int idx;

	i64 time() const
	{
		return t + llabs(x - fh);
	}

	bool operator<(const Data& r) const
	{
		return time() > r.time();
	}
};

Data dist[100005];

int main()
{
	memset(dist, -1, sizeof(dist));

	int n, m;
	scanf("%d %d %lld", &n, &m, &x);

	vector<i64> h(n + 5);

	for (int i = 1; i <= n; i++)
		scanf("%lld", &h[i]);

	for (int i = 0; i < m; i++)
	{
		int a, b, t;
		scanf("%d %d %d", &a, &b, &t);
		if (h[a] >= t)
			edge[a].emplace_back(b, t);

		if (h[b] >= t)
			edge[b].emplace_back(a, t);
	}

	pq<Data> q;
	dist[1] = { 0, x, x, 1 };
	q.emplace(dist[1]);

	i64 ans = 1ll << 60;

	while (!q.empty())
	{
		auto now = q.top();
		q.pop();

		if (now.idx == n)
			ans = min(ans, dist[n].time() + h[n] - dist[n].h);

		if (now.time() > dist[now.idx].time())
			continue;

		for (auto& e : edge[now.idx])
		{
			Data nxt;
			nxt.t = now.t + e.yy;
			nxt.fh = now.fh;
			nxt.h = now.h - e.yy;
			nxt.idx = e.xx;

			if (nxt.h < 0)
			{
				nxt.fh -= nxt.h;
				nxt.h = 0;
			}

			if (nxt.h > h[e.xx])
			{
				nxt.fh -= h[e.xx] - nxt.h;
				nxt.h = h[e.xx];
			}

			if (dist[e.xx].t != -1 && nxt.time() >= dist[e.xx].time())
				continue;

			dist[e.xx] = nxt;
			q.emplace(nxt);
		}
	}

	if (ans == 1ll << 60)
		ans = -1;

	printf("%lld\n", ans);

	return 0;
}

Compilation message

2014_ho_t4.cpp: In function 'int main()':
2014_ho_t4.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d %d %lld", &n, &m, &x);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
2014_ho_t4.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |   scanf("%lld", &h[i]);
      |   ~~~~~^~~~~~~~~~~~~~~
2014_ho_t4.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |   scanf("%d %d %d", &a, &b, &t);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 5 ms 5836 KB Output is correct
3 Correct 5 ms 5836 KB Output is correct
4 Correct 4 ms 5836 KB Output is correct
5 Correct 4 ms 5836 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 4 ms 5780 KB Output is correct
8 Correct 5 ms 5836 KB Output is correct
9 Correct 5 ms 5928 KB Output is correct
10 Correct 6 ms 5836 KB Output is correct
11 Correct 4 ms 5772 KB Output is correct
12 Correct 6 ms 5964 KB Output is correct
13 Correct 5 ms 5904 KB Output is correct
14 Correct 4 ms 5708 KB Output is correct
15 Correct 5 ms 5908 KB Output is correct
16 Correct 5 ms 5708 KB Output is correct
17 Correct 4 ms 5772 KB Output is correct
18 Correct 4 ms 5780 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 4 ms 5836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 195 ms 17676 KB Output is correct
2 Correct 193 ms 29400 KB Output is correct
3 Correct 152 ms 16740 KB Output is correct
4 Correct 219 ms 28356 KB Output is correct
5 Correct 206 ms 28088 KB Output is correct
6 Correct 12 ms 6092 KB Output is correct
7 Correct 125 ms 11824 KB Output is correct
8 Correct 318 ms 24516 KB Output is correct
9 Correct 138 ms 14244 KB Output is correct
10 Correct 87 ms 10692 KB Output is correct
11 Correct 21 ms 7740 KB Output is correct
12 Correct 123 ms 18052 KB Output is correct
13 Correct 58 ms 16144 KB Output is correct
14 Correct 146 ms 12712 KB Output is correct
15 Correct 10 ms 6220 KB Output is correct
16 Correct 173 ms 26988 KB Output is correct
17 Correct 22 ms 7628 KB Output is correct
18 Correct 34 ms 7752 KB Output is correct
19 Correct 50 ms 8656 KB Output is correct
20 Correct 27 ms 8056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 5 ms 5836 KB Output is correct
3 Correct 5 ms 5836 KB Output is correct
4 Correct 4 ms 5836 KB Output is correct
5 Correct 4 ms 5836 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 4 ms 5780 KB Output is correct
8 Correct 5 ms 5836 KB Output is correct
9 Correct 5 ms 5928 KB Output is correct
10 Correct 6 ms 5836 KB Output is correct
11 Correct 4 ms 5772 KB Output is correct
12 Correct 6 ms 5964 KB Output is correct
13 Correct 5 ms 5904 KB Output is correct
14 Correct 4 ms 5708 KB Output is correct
15 Correct 5 ms 5908 KB Output is correct
16 Correct 5 ms 5708 KB Output is correct
17 Correct 4 ms 5772 KB Output is correct
18 Correct 4 ms 5780 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 4 ms 5836 KB Output is correct
21 Correct 195 ms 17676 KB Output is correct
22 Correct 193 ms 29400 KB Output is correct
23 Correct 152 ms 16740 KB Output is correct
24 Correct 219 ms 28356 KB Output is correct
25 Correct 206 ms 28088 KB Output is correct
26 Correct 12 ms 6092 KB Output is correct
27 Correct 125 ms 11824 KB Output is correct
28 Correct 318 ms 24516 KB Output is correct
29 Correct 138 ms 14244 KB Output is correct
30 Correct 87 ms 10692 KB Output is correct
31 Correct 21 ms 7740 KB Output is correct
32 Correct 123 ms 18052 KB Output is correct
33 Correct 58 ms 16144 KB Output is correct
34 Correct 146 ms 12712 KB Output is correct
35 Correct 10 ms 6220 KB Output is correct
36 Correct 173 ms 26988 KB Output is correct
37 Correct 22 ms 7628 KB Output is correct
38 Correct 34 ms 7752 KB Output is correct
39 Correct 50 ms 8656 KB Output is correct
40 Correct 27 ms 8056 KB Output is correct
41 Correct 138 ms 12136 KB Output is correct
42 Correct 41 ms 8320 KB Output is correct
43 Correct 96 ms 10636 KB Output is correct
44 Correct 99 ms 14496 KB Output is correct
45 Correct 175 ms 26372 KB Output is correct
46 Correct 20 ms 7244 KB Output is correct
47 Correct 234 ms 24436 KB Output is correct
48 Correct 337 ms 25496 KB Output is correct
49 Correct 240 ms 21844 KB Output is correct
50 Correct 241 ms 27988 KB Output is correct
51 Correct 37 ms 8548 KB Output is correct
52 Correct 236 ms 21132 KB Output is correct
53 Correct 115 ms 12312 KB Output is correct
54 Correct 147 ms 14232 KB Output is correct
55 Correct 122 ms 12684 KB Output is correct
56 Correct 259 ms 26020 KB Output is correct
57 Correct 53 ms 10308 KB Output is correct
58 Correct 7 ms 6092 KB Output is correct
59 Correct 115 ms 12040 KB Output is correct
60 Correct 49 ms 9960 KB Output is correct