제출 #813520

#제출 시각아이디문제언어결과실행 시간메모리
813520SteGGCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
278 ms33852 KiB
#include <bits/stdc++.h>
#define int long long
#define NOT_DONE 1
#define DONE 2

using namespace std;

void open(){
	if(fopen("input.inp", "r")){
		freopen("input.inp", "r", stdin);
		//freopen("output.out", "w", stdout);
	}
}

const int maxn = 1e5 + 5;
const int inf = 3e18;
const int oo = 1e9 + 7;

int n, m;
vector<pair<int, int>> G[maxn];
int S, T;
int U, V;
int min_cost;
int d_s[maxn], d_t[maxn], d_u[maxn], d_v[maxn];
bool in_road[maxn];
int state[maxn];
pair<int, int> dp[maxn]; // trollge

struct Node{
	int index, len;

	Node(){}

	Node(int _index, int _len) : index(_index), len(_len) {}

	bool operator>(const Node &other) const{
		if(len != other.len) return len > other.len;
		return index > other.index;
	}
};

bool check_if_in_road(int u, int v, int w){ // u(S) -> v(T)
	return d_s[u] + w + d_t[v] == min_cost;
}

pair<int, int> custom_min(const pair<int, int> &A, const pair<int, int> &B){
	pair<int, int> result = make_pair(min(A.first, B.first), min(A.second, B.second));
	return result;
}

pair<int, int> custom_cmp(const pair<int, int> &A, const pair<int, int> &B, const pair<int, int> &C){ // compare A < B
	pair<int, int> r1 = custom_min(A, C);
	pair<int, int> r2 = custom_min(B, C);

	if(r1.first + r1.second < r2.first + r2.second) return A;
	else return B;
}

pair<int, int> dfs(int u){
	state[u] = NOT_DONE;
	pair<int, int> result = make_pair(d_u[u], d_v[u]);
	pair<int, int> temp = make_pair(inf, inf);
	for(pair<int, int> node : G[u]){
		int v = node.first;
		int w = node.second;
		if(state[v] == NOT_DONE || !check_if_in_road(u, v, w)) continue;
		if(state[v] == DONE){
			temp = custom_cmp(temp, dp[v], result);
			continue;
		}
		temp = custom_cmp(temp, dfs(v), result);
	}
	state[u] = DONE;

	result = custom_min(result, temp);

	return dp[u] = result;
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	open();
	cin >> n >> m;
	cin >> S >> T;
	cin >> U >> V;
	for(int i = 1; i <= m; i++){
		int a, b, c;
		cin >> a >> b >> c;
		G[a].push_back({b, c});
		G[b].push_back({a, c});
	}

	// From s:
	priority_queue<Node, vector<Node>, greater<Node>> pq;
	memset(d_s, oo, sizeof(d_s));
	d_s[S] = 0;
	pq.push({S, 0});
	while(!pq.empty()){
		Node first = pq.top();
		pq.pop();
		int u = first.index;
		if(d_s[u] < first.len) continue;
		for(pair<int, int> node : G[u]){
			int v = node.first;
			int w = node.second;
			if(d_s[v] > d_s[u] + w){
				d_s[v] = d_s[u] + w;
				pq.push({v, d_s[v]});
			}
		}
	}

	// From t:
	memset(d_t, oo, sizeof(d_t));
	d_t[T] = 0;
	pq.push({T, 0});
	while(!pq.empty()){
		Node first = pq.top();
		pq.pop();
		int u = first.index;
		if(d_t[u] < first.len) continue;
		for(pair<int, int> node : G[u]){
			int v = node.first;
			int w = node.second;
			if(d_t[v] > d_t[u] + w){
				d_t[v] = d_t[u] + w;
				pq.push({v, d_t[v]});
			}
		}
	}

	// First handle:
	assert(d_s[T] == d_t[S]);
	min_cost = d_s[T];

	for(int i = 1; i <= n; i++){
		if(d_s[i] + d_t[i] == min_cost){
			in_road[i] = true;
		}
	}

	// From u:
	memset(d_u, oo, sizeof(d_u));
	d_u[U] = 0;
	pq.push({U, 0});
	while(!pq.empty()){
		Node first = pq.top();
		pq.pop();
		int u = first.index;
		if(d_u[u] < first.len) continue;
		for(pair<int, int> node : G[u]){
			int v = node.first;
			int w = node.second;
			if(d_u[v] > d_u[u] + w){
				d_u[v] = d_u[u] + w;
				pq.push({v, d_u[v]});
			}
		}
	}

	// From v:
	memset(d_v, oo, sizeof(d_v));
	d_v[V] = 0;
	pq.push({V, 0});
	while(!pq.empty()){
		Node first = pq.top();
		pq.pop();
		int u = first.index;
		if(d_v[u] < first.len) continue;
		for(pair<int, int> node : G[u]){
			int v = node.first;
			int w = node.second;
			if(d_v[v] > d_v[u] + w){
				d_v[v] = d_v[u] + w;
				pq.push({v, d_v[v]});
			}
		}
	}

	// Last handle:

	// Handle the vertex is in the route:
	pair<int, int> smallest = dfs(S);
	int result = smallest.first + smallest.second;
	for(int i = 1; i <= n; i++){
		if(!in_road[i]){
			result = min(result, d_u[i] + d_v[i]);
		}
	}

	cout << result << endl;

	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

commuter_pass.cpp: In function 'void open()':
commuter_pass.cpp:10:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |   freopen("input.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...