답안 #895088

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895088 2023-12-29T11:56:37 Z hariaakas646 Olympic Bus (JOI20_ho_t4) C++14
100 / 100
422 ms 5460 KB
#include <bits/stdc++.h>

using namespace std;

#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t);
#define forr(i, l, r) for(int i=l; i<r; i++)
#define frange(i, l) forr(i, 0, l)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef long long lli;
typedef vector<vi> vvi;
typedef vector<lli> vll;
typedef vector<bool> vb;
typedef set<int> seti;

void usaco() {
	freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
}

struct Edge {
	int a, b;
	lli c, d;
};

int main() {
	// usaco();
	int n, m;
	scd(n);
	scd(m);

	vector<seti> graph(n+1);
	vector<Edge> edg(m);
	vector<vll> dist(n+1, vll(n+1, 1e17));
	forr(i, 1, n+1) dist[i][i] = 0;
	frange(i, m) {
		int a, b;
		lli c, d;
		scd(a);
		scd(b);
		sclld(c);
		sclld(d);
		graph[a].insert(i);
		edg[i].a = a;
		edg[i].b = b;
		edg[i].c = c;
		edg[i].d = d;
		dist[a][b] = min(dist[a][b], c);
	}

	forr(k, 1, n+1) {
		forr(i, 1, n+1) {
			forr(j, 1, n+1) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
		}
	}

	lli mi = dist[1][n]+dist[n][1];

	frange(i, m) {
		auto p = edg[i];
		lli d1 = min(dist[1][n], dist[1][p.b] + dist[p.a][n] + p.c);
		lli d2 = min(dist[n][1], dist[n][p.b] + dist[p.a][1] + p.c);

		if(d1 + d2 + p.d < mi) {
			graph[edg[i].a].erase(i);
			graph[edg[i].b].insert(i);
			swap(edg[i].a, edg[i].b);

			priority_queue<pair<lli, int>> pq;
			vll dist(n+1, 1e17);
			dist[1] = 0;

			pq.push(mp(0, 1));
			vb vis(n+1);
			while(pq.size()) {
				auto p = pq.top();
				pq.pop();
				if(vis[p.s]) continue;
				vis[p.s] = true;

				for(auto x : graph[p.s]) {
					auto e = edg[x];
					if(dist[p.s] + e.c < dist[e.b]) {
						dist[e.b] = dist[p.s] + e.c;
						pq.push(mp(-dist[e.b], e.b));
					}
				}
			}
			lli dt1 = dist[n];
			dist = vll(n+1, 1e17);
			dist[n] = 0;

			pq.push(mp(0, n));
			vis = vb(n+1);
			while(pq.size()) {
				auto p = pq.top();
				pq.pop();
				if(vis[p.s]) continue;
				vis[p.s] = true;

				for(auto x : graph[p.s]) {
					auto e = edg[x];
					if(dist[p.s] + e.c < dist[e.b]) {
						dist[e.b] = dist[p.s] + e.c;
						pq.push(mp(-dist[e.b], e.b));
					}
				}
			}
			lli dt2 = dist[1];
			mi = min(mi, dt1+dt2+p.d);
			swap(edg[i].a, edg[i].b);
			graph[edg[i].a].insert(i);
			graph[edg[i].b].erase(i);
		}
	}
	if(mi < 1e17)
	printf("%lld", mi);
	else printf("-1");

}

Compilation message

ho_t4.cpp: In function 'void usaco()':
ho_t4.cpp:25:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |  freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
ho_t4.cpp:36:2: note: in expansion of macro 'scd'
   36 |  scd(n);
      |  ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
ho_t4.cpp:37:2: note: in expansion of macro 'scd'
   37 |  scd(m);
      |  ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
ho_t4.cpp:46:3: note: in expansion of macro 'scd'
   46 |   scd(a);
      |   ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
ho_t4.cpp:47:3: note: in expansion of macro 'scd'
   47 |   scd(b);
      |   ^~~
ho_t4.cpp:6:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define sclld(t) scanf("%lld", &t);
      |                  ~~~~~^~~~~~~~~~~~
ho_t4.cpp:48:3: note: in expansion of macro 'sclld'
   48 |   sclld(c);
      |   ^~~~~
ho_t4.cpp:6:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define sclld(t) scanf("%lld", &t);
      |                  ~~~~~^~~~~~~~~~~~
ho_t4.cpp:49:3: note: in expansion of macro 'sclld'
   49 |   sclld(d);
      |   ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 604 KB Output is correct
2 Correct 7 ms 780 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Correct 7 ms 856 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 11 ms 860 KB Output is correct
11 Correct 20 ms 868 KB Output is correct
12 Correct 8 ms 856 KB Output is correct
13 Correct 11 ms 856 KB Output is correct
14 Correct 7 ms 860 KB Output is correct
15 Correct 7 ms 856 KB Output is correct
16 Correct 7 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 4188 KB Output is correct
2 Correct 30 ms 4188 KB Output is correct
3 Correct 35 ms 4156 KB Output is correct
4 Correct 10 ms 860 KB Output is correct
5 Correct 7 ms 600 KB Output is correct
6 Correct 7 ms 808 KB Output is correct
7 Correct 7 ms 692 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 45 ms 5416 KB Output is correct
10 Correct 39 ms 5212 KB Output is correct
11 Correct 32 ms 5232 KB Output is correct
12 Correct 32 ms 5460 KB Output is correct
13 Correct 31 ms 5420 KB Output is correct
14 Correct 31 ms 5456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 604 KB Output is correct
2 Correct 7 ms 780 KB Output is correct
3 Correct 25 ms 4232 KB Output is correct
4 Correct 8 ms 604 KB Output is correct
5 Correct 39 ms 5092 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 49 ms 5052 KB Output is correct
9 Correct 56 ms 5212 KB Output is correct
10 Correct 32 ms 5200 KB Output is correct
11 Correct 33 ms 5212 KB Output is correct
12 Correct 31 ms 5456 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 33 ms 5204 KB Output is correct
20 Correct 35 ms 5188 KB Output is correct
21 Correct 32 ms 5200 KB Output is correct
22 Correct 34 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 604 KB Output is correct
2 Correct 7 ms 780 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Correct 7 ms 856 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 11 ms 860 KB Output is correct
11 Correct 20 ms 868 KB Output is correct
12 Correct 8 ms 856 KB Output is correct
13 Correct 11 ms 856 KB Output is correct
14 Correct 7 ms 860 KB Output is correct
15 Correct 7 ms 856 KB Output is correct
16 Correct 7 ms 856 KB Output is correct
17 Correct 28 ms 4188 KB Output is correct
18 Correct 30 ms 4188 KB Output is correct
19 Correct 35 ms 4156 KB Output is correct
20 Correct 10 ms 860 KB Output is correct
21 Correct 7 ms 600 KB Output is correct
22 Correct 7 ms 808 KB Output is correct
23 Correct 7 ms 692 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 45 ms 5416 KB Output is correct
26 Correct 39 ms 5212 KB Output is correct
27 Correct 32 ms 5232 KB Output is correct
28 Correct 32 ms 5460 KB Output is correct
29 Correct 31 ms 5420 KB Output is correct
30 Correct 31 ms 5456 KB Output is correct
31 Correct 7 ms 604 KB Output is correct
32 Correct 7 ms 780 KB Output is correct
33 Correct 25 ms 4232 KB Output is correct
34 Correct 8 ms 604 KB Output is correct
35 Correct 39 ms 5092 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 49 ms 5052 KB Output is correct
39 Correct 56 ms 5212 KB Output is correct
40 Correct 32 ms 5200 KB Output is correct
41 Correct 33 ms 5212 KB Output is correct
42 Correct 31 ms 5456 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 33 ms 5204 KB Output is correct
50 Correct 35 ms 5188 KB Output is correct
51 Correct 32 ms 5200 KB Output is correct
52 Correct 34 ms 5204 KB Output is correct
53 Correct 30 ms 5208 KB Output is correct
54 Correct 30 ms 5200 KB Output is correct
55 Correct 30 ms 5208 KB Output is correct
56 Correct 10 ms 860 KB Output is correct
57 Correct 8 ms 604 KB Output is correct
58 Correct 33 ms 4420 KB Output is correct
59 Correct 40 ms 4188 KB Output is correct
60 Correct 37 ms 4280 KB Output is correct
61 Correct 35 ms 4344 KB Output is correct
62 Correct 138 ms 4188 KB Output is correct
63 Correct 297 ms 4184 KB Output is correct
64 Correct 91 ms 4800 KB Output is correct
65 Correct 222 ms 4796 KB Output is correct
66 Correct 422 ms 5048 KB Output is correct
67 Correct 22 ms 3932 KB Output is correct
68 Correct 48 ms 5448 KB Output is correct
69 Correct 42 ms 5460 KB Output is correct
70 Correct 32 ms 5456 KB Output is correct
71 Correct 35 ms 5436 KB Output is correct
72 Correct 32 ms 5204 KB Output is correct
73 Correct 33 ms 5284 KB Output is correct
74 Correct 35 ms 5392 KB Output is correct