Submission #1233381

#TimeUsernameProblemLanguageResultExecution timeMemory
1233381clemmy14자매 도시 (APIO20_swap)C++20
0 / 100
2092 ms15792 KiB
#include "swap.h"
#include<bits/stdc++.h>
using namespace std;

int n;
// weight then node
vector<vector<pair<int, int>>> adj;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	n=N;
	adj = vector<vector<pair<int, int>>>(N);
	for(int i=0; i<M; i++) {
		adj[U[i]].push_back({W[i], V[i]});
		adj[V[i]].push_back({W[i], U[i]});
	}
	for(int i=0; i<N; i++) sort(adj[i].begin(), adj[i].end());
	
}

int ans=0;
set<pair<int, int>> noUse;

vector<int> dijkstra(int s, int e) {
	vector<int> par(n, 0), dis(n, 1e9); dis[s]=0;
	vector<bool> vis(n, false);
	priority_queue<pair<int, int>> q; q.push({0, s});
	while(!q.empty()) {
		int node = q.top().second; q.pop();
		if(vis[node]) continue;
		vis[node]=true;
		if(node == e) {
			ans=dis[e]; break;
		}
		for(auto [w, child] : adj[node]) if(!noUse.count({node, child})) {
			if(dis[child] > max(dis[node], w)) {
				dis[child]=max(dis[node], w);
				par[child]=node;
				q.push({-dis[child], child});
			}
		}
	}
	vector<int> path;
	if(vis[e]) {
		path.push_back(e);
		for(int i=e; i!=s; i=par[i]) {
			path.push_back(par[i]);
		}
	}
	//reverse(path.begin(), path.end());
	return path;
}

int getMinimumFuelCapacity(int X, int Y) {
	ans=-1;	
	vector<int> path=dijkstra(X, Y);
	// for(auto x : path) cout << x << ' ';
	// cout << endl;
	int newAns=-1;
	for(int i=0; i<path.size(); i++) {
		if(path[i] == X || path[i] == Y) {
			int cnt=0;
			for(int j=0; j<adj[path[i]].size(); j++) {
				if(i != 0 && adj[path[i]][j].second == path[i-1]) continue;
				if(i != path.size()-1 && adj[path[i]][j].second == path[i+1]) continue;
				cnt++;
				if(cnt == 2) {
					if(newAns != -1) newAns=min(newAns, adj[path[i]][j].first);
					else newAns = adj[path[i]][j].first;
				}
			}
		} else {
			int cnt=0;
			for(int j=0; j<adj[path[i]].size(); j++) {
				if(i != 0 && adj[path[i]][j].second == path[i-1]) continue;
				if(i != path.size()-1 && adj[path[i]][j].second == path[i+1]) continue;
				cnt++;
				if(cnt == 1) {
					if(newAns != -1) newAns=min(newAns, adj[path[i]][j].first);
					else newAns = adj[path[i]][j].first;
				}
			}
		}
	}
	noUse.clear();
	for(int i=1; i<path.size(); i++) {
		noUse.insert({path[i], path[i-1]});
		noUse.insert({path[i-1], path[i]});
	}

	int oldAns=ans;
	ans=-1;
	path=dijkstra(X, Y); 

	noUse.clear();
	
	if(newAns == -1) newAns=ans;
	else if(ans != -1) newAns=min(newAns, ans);


	if(newAns == -1) return -1;
	return max(newAns, oldAns);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...