제출 #1338090

#제출 시각아이디문제언어결과실행 시간메모리
1338090manCommuter Pass (JOI18_commuter_pass)C++20
31 / 100
175 ms23020 KiB
// Day Created: mar 16th 2026
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

#define fname ""
#define ll long long
#define pii pair<int, int>
#define pli pair<ll, int>
#define fi first
#define se second

using namespace std;
int n, m, S, T, U, V;
struct road {
	int v, w, id;
};
vector<road> edge[100005];


vector<int> dag[100005], rdag[100005];
ll distU[100005], distV[100005], dp[100005];
queue<int> q;
priority_queue<pli, vector<pli>, greater<pli>> pq;
bool f[200005];

void dijktra(ll dist[], const int &S) {
	for(int i=1; i<=n; i++) dist[i]=1e16;
	dist[S]=0;
	pq.push({0, S});
	while(!pq.empty()) {
		auto [d, u]=pq.top();
		pq.pop();
		if(d!=dist[u]) continue;
		for(const auto &[v, w, id]:edge[u]) {
			int n_w=(f[id]?0:w);
			if(d+n_w<dist[v]) 
				dist[v]=d+n_w, pq.push({d+n_w, v});
		}
	}
}

bool vis[100005];
ll find_path(ll dU[], ll dV[]) {
	for(int i=1; i<=n; i++) vis[i]=0;
	ll res=1e16;
	for(int i=1; i<=n; i++) dp[i]=dU[i];
	vis[S]=1;
	q.push(S);
	while(!q.empty()) {
		int u=q.front();
		q.pop();
		for(const auto &v:rdag[u]) 
			dp[u]=min(dp[u], dp[v]);
		for(const auto &v:dag[u]) {
			if(!vis[v]) vis[v]=1, q.push(v);
			dp[v]=min(dp[v], dp[u]);
		}
	}
	for(int i=1; i<=n; i++) res=min(res, dp[i]+dV[i]);
	return res;
}

void solve() {
	cin>>n>>m>>S>>T>>U>>V;
	while(m--) {
		int u, v, w;
		cin>>u>>v>>w;
		edge[u].push_back({v, w, m});
		edge[v].push_back({u, w, m});
	}

	// train process
	dijktra(distU, S);
	q.push(T);
	while(!q.empty()) {
		int v=q.front();
		q.pop();
		for(const auto &[u, w, id]:edge[v]) 
			if(distU[u]+w==distU[v]) {
				dag[u].emplace_back(v);
				rdag[v].emplace_back(u);
				f[id]=1;
				if(!vis[u]) vis[u]=1, q.push(u);
			}
	}

	// student process
	dijktra(distU, U);
	dijktra(distV, V);
	// // cout<<distU[2];
	// for(int i=1; i<=n; i++) cout<<distV[i]<<' ';
	// cout<<'\n';
	cout<<min(find_path(distU, distV), find_path(distV, distU));
}

main() {
	if(fopen(fname".inp", "r")) {
		freopen(fname".inp", "r", stdin);
		freopen(fname".out", "w", stdout);
	}
	cin.tie(0)->sync_with_stdio(0);
	int tc=1;
	// cin>>tc;
	while(tc--) {
		solve();
		cout<<'\n';
	}
}

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

commuter_pass.cpp:98:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   98 | main() {
      | ^~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:100:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |                 freopen(fname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:101:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |                 freopen(fname".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...