답안 #388323

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
388323 2021-04-10T22:45:27 Z Kepha Robot (JOI21_ho_t4) C++11
0 / 100
265 ms 33068 KB
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define MX 100000
#define ll long long
using namespace std;

struct Edge {
    int color, next;
    ll price;

    Edge (int _color = 0, int _next = 0, ll _price = 0) {
        color = _color, next = _next, price = _price;
    }
};

struct Node {
    ll dist;
    int color, cur;

    Node (ll _dist = 0, int _color = 0, int _cur = 0) {
        dist = _dist, color = _color, cur = _cur;
    }

    bool operator<(const Node& x) const { return dist > x.dist; }
};

int N, M;
map<int, vector<Edge>> adj[MX + 1];
ll dist1[MX + 1];
map<int, ll> dist2[MX + 1], sum[MX + 1];
priority_queue<Node> q;

int main() {;
	cin >> N >> M;
	for (int i = 1; i <= M; i++) {
		int a, b, c;
		ll d;
		cin >> a >> b >> c >> d;
		adj[a][c].push_back(Edge(c, b, d));
		adj[b][c].push_back(Edge(c, a, d));
		sum[a][c] += d;
		sum[b][c] += d;
	}

	fill(dist1 + 1, dist1 + 1 + N, INF);
	dist1[1] = 0;
	q.push(Node(0, 0, 1));

	while (q.size()) {
		ll cost = q.top().dist;
		int node = q.top().cur, color = q.top().color;
		q.pop();

		if (color) {
			if (dist2[node][color] != cost) continue;
			for (Edge edge : adj[node][color]) {
				// We can't flip i in this case
				ll case1 = sum[node][color] + edge.price;
				if (case1 + cost < dist1[edge.next]) {
					dist1[edge.next] = case1 + cost;
					q.push(Node(dist1[edge.next], 0, edge.next));
				}
			}
		}
		else {
			if (dist1[node] != cost) continue;
			for (auto& c : adj[node]) {
				for (Edge edge : c.second) {
					// Case 1: We don't flip j
					ll case1 = sum[node][edge.color] + edge.price + cost;
					if (case1 < dist1[edge.next]) {
						dist1[edge.next] = case1;
						q.push(Node(dist1[edge.next], 0, edge.next));
					}
					// Case 2: We flip j but not another edge of the same colour
					ll case2 = edge.price + cost;
					if (case2 < dist1[edge.next]) {
						dist1[edge.next] = case2;
						q.push(Node(dist1[edge.next], 0, edge.next));
					}
					// Case 3: We flip j and another edge of the same colour
					ll case3 = cost;
					if (!dist2[edge.next].count(edge.color) || case3 < dist2[edge.next][edge.color]) {
						dist2[edge.next][edge.color] = case3;
						q.push(Node(dist2[edge.next][edge.color], edge.color, edge.next));
					}
				}
			}
		}
	}

	if (dist1[N] >= INF) cout << "-1\n";
	else cout << dist1[N] << "\n";

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 14364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 265 ms 33068 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 14364 KB Output isn't correct
2 Halted 0 ms 0 KB -