-
Notifications
You must be signed in to change notification settings - Fork 2
15-mjj111 #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
15-mjj111 #226
Conversation
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ²μ λΈ μ½λκ° μκ°μ΄κ³Ό λκΈΈλ μ°μ νλλ°, λ€μ 보λ ν΅μ¬μ "μ΅μ μν"μ 빨리 μ°Ύλ κ²μ΄μκ΅°μ...
λ€μ΅μ€νΈλΌλ ν΄λ΄€μ 16λ²λ°μ λ°μνμ§ μμ μ°μ μμν μ μ΄
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
constexpr int INF = 1e9;
int Q, Cmd; cin >> Q;
int N, M;
vector<vector<pair<int, int>>> Graph;
vector<int> Distances;
priority_queue<pair<int, int>> PriorityofProducts;
unordered_map<int, pair<int, int>> Products;
auto Dijkstra = [&](int Start)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> PQ;
Distances.assign(N, INF);
PQ.emplace(0, Start);
Distances[Start] = 0;
while(!PQ.empty())
{
const auto [du, u] = PQ.top(); PQ.pop();
if(Distances[u] < du) continue;
for(const auto& [v, dv] : Graph[u])
{
int Distance = du + dv;
if(Distance < Distances[v])
{
Distances[v] = Distance;
PQ.emplace(Distance, v);
}
}
}
};
auto Construction = [&]()
{
cin >> N >> M;
Graph.resize(N);
while(M--)
{
int V, U, W; cin >> V >> U >> W;
Graph[V].emplace_back(U, W);
Graph[U].emplace_back(V, W);
}
Dijkstra(0);
};
auto Creation = [&]()
{
int Id, Revenue, Dest; cin >> Id >> Revenue >> Dest;
Products[Id] = {Revenue, Dest};
int Cost = Distances[Dest];
int Benefit = Revenue - Cost;
PriorityofProducts.emplace(Benefit, -Id);
};
auto Cancel = [&]()
{
int Id; cin >> Id;
if(Products.count(Id) > 0)
{
Products.erase(Id);
}
};
auto Sale = [&]()
{
int BestProductId = -1;
while(!PriorityofProducts.empty())
{
auto [Benefit, Id] = PriorityofProducts.top(); PriorityofProducts.pop();
Id *= -1;
if(Products.count(Id) == 0) continue;
const auto& [Revenue, Dest] = Products[Id];
int Cost = Distances[Dest];
if(Cost == INF || Cost > Revenue)
{
BestProductId = -1;
break;
}
Products.erase(Id);
BestProductId = Id;
break;
}
return BestProductId;
};
auto Change = [&]()
{
int S; cin >> S;
Dijkstra(S);
PriorityofProducts = priority_queue<pair<int, int>>();
for(const auto& [Id, Product] : Products)
{
const auto& [Revenue, Dest] = Product;
int Cost = Distances[Dest];
PriorityofProducts.emplace(Revenue - Cost, -Id);
}
};
while(Q--)
{
cin >> Cmd;
switch(Cmd)
{
case 100: Construction(); break;
case 200: Creation(); break;
case 300: Cancel(); break;
case 400: cout << Sale() << "\n"; break;
case 500: Change();
default: break;
}
}
return 0;
}
gjsk132
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ§κΈκΉμ§λ ꡬνμ΄ μ¬λ°λ κ² κ°μλ°, μλ₯Ό μ§ν€κ³ μλ 루λνκ° λλ €μμ π₯²
μ 체 μ½λ
from heapq import *
input = open("input.txt").readline
cmd_cnt = int(input())
command = list(map(int,input().split()))[1:]
n, m = command[0], command[1]
limit = float("inf")
load_info = [[limit]*n for _ in range(n)]
dist = [limit]*n
p_id = {}
p_cost = []
for i in range(m):
n1, n2, w = command[i*3+2:i*3+5]
load_info[n1][n2] = min(w, load_info[n1][n2])
load_info[n2][n1] = min(w, load_info[n2][n1])
def update_node_dist(s_node):
hq = [(0, s_node)]
check = [False]*n
while hq and not all(check):
w, current = heappop(hq)
if check[current]:
continue
check[current] = True
if w >= dist[current]:
continue
dist[current] = w
for next in range(n):
if next == current or load_info[current][next] == limit:
continue
heappush(hq, (w + load_info[current][next], next))
def update_product():
update_cost = []
for k, v in p_id.items():
heappush(update_cost, ((dist[v[1]]-v[0]), k))
return update_cost
update_node_dist(0)
for _ in range(cmd_cnt-1):
command = list(map(int, input().split()))
order = command[0]
if order == 200:
# 1 : id / 2 : revenue / 3 : dest
cost = command[2] - dist[command[3]]
heappush(p_cost, (-(command[2] - dist[command[3]]), command[1]))
p_id[command[1]] = command[2:]
elif order == 300:
if command[1] in p_id:
del(p_id[command[1]])
elif order == 400:
while True:
if not p_cost:
print(-1)
break
cost, id = heappop(p_cost)
cost = -cost
if not id in p_id.keys():
continue
elif cost < 0:
heappush(p_cost, (-cost, id))
print(-1)
else:
print(id)
del(p_id[id])
break
else:
dist = [limit]*n
update_node_dist(command[1])
p_cost = update_product()
π λ¬Έμ λ§ν¬
https://www.codetree.ai/training-field/frequent-problems/problems/codetree-tour/description?page=1&pageSize=5
μ½λνΈλ¦¬ ν¬μ΄
βοΈ μμλ μκ°
1μκ° 20λΆ
β¨ μλ μ½λ
5κ°μ§ λͺ λ Ήμ λν΄ μλμ κ°μ΄ λμν©λλ€.
λ€λ¦¬ κ±΄μ€ -> λ€ μ°κ²°νλ©΄ 0λΆν° λ€μ΅μ€νΈλΌλ‘ κ° κ³μ°
1λμμ 2λμλ 3κ°μ€μΉ κ°μ μΌλ‘ μ΄λ£¨μ΄ μ§λλ€.
μ¬ν μν μμ±(3λ§λ²) -> νμ¬ μ£Όμ΄μ§ λ€μ΅μ€νΈλΌ κ²°κ³Όλ‘ μνμ λ§λλλ€.
1κ³ μ μλ³μλ‘ 2λ§€μΆ 3λμ°©μ§
μ¬ν μν μ·¨μ(3λ§λ²) -> μ·¨μν μνμ κΈ°λ‘ν©λλ€.
1κ³ μ μλ³μμ ν΄λΉνλ μ¬νμνμ λͺ©λ‘μμ μμ
μ΅μ μ μ¬ν μν νλ§€(3λ§λ²) κ΄λ¦¬ λͺ©λ‘μ heapκ³Ό dictλ‘ κ΄λ¦¬νλ€. -> λ§μ½ λ½μ μνμ΄ νλ§€νμ§ μμμ΄λΌλ©΄ λ²λ¦½λλ€.
μ΄λ - λΉμ©μ΄ μ΅λμΉμΈ μνμ°μ κ³ λ €(+id μ€λ¦μ°¨μ)
λΉμ©μ μΆλ°μ§λ‘λΆν° id μνμ λμ°©μ§ κΉμ§ μ΅λ¨κ±°λ¦¬
λ§μ½ λͺ©μ μ§μ λλ¬νλκ² λΆκ°λ₯νκ±°λ λΉμ©μ΄ μ΄λλ³΄λ€ ν¬λ€λ©΄ νλ§€λΆκ°
νλ§€ κ°λ₯ν μνμ€ κ°μ₯ μ°μ μμκ° λμ 1κ°λ₯Ό νλ§€νμ¬ ν΄λΉ idμν μμ
λ§μ½ μλ€λ©΄ -1 μΆλ ₯νκ³ μ κ±° x
μν μΆλ°μ§ λ³κ²½(15λ²) μΆλ°μ§λΆν° λ€μ λ€μ΅μ€νΈλΌλ‘ κ° κ³μ° ν μν λ€μ κ°±μ / λ§μ½ ν΄λμ€κ° νλ§€νμ§ μμμ΄λΌλ©΄ μ€ν΅
λͺ¨λ μΆλ°μ§λ₯Ό sλ‘ λ³κ²½ (λ³κ²½ν¨μ λ°λΌ κ° μνμ λΉμ©μ΄ λ³κ²½λλ€)
μ¬ν λ§€λμ ν΄λμ€λ₯Ό λ¨Όμ μ μΈν΄μ€λλ€.
100 (λ€λ¦¬κ±΄μ€)λͺ λ Ήμ΄κ° λ¨Όμ μ£Όμ΄μ§κΈ° λλ¬Έμ μ΄μ λν΄μ Travel λ§€λμ λ₯Ό μ€μ ν΄μ£Όκ³
λ°λ κ°λ§λ€ μ°κ²°ν λ€ λ§μ§λ§μ λ€μ΅μ€νΈλΌλ₯Ό ν΅ν΄ 0λΆν° κ°μ₯ μ§§μ κΈΈμ κ°±μ ν©λλ€.
μνμ΄ μΆκ°λλ€λ©΄ μ΄μλν΄μ μ΄λ―Έ κ°μ ꡬν΄λ¨κΈ° λλ¬Έμ μνμ κ°±μ ν΄μ€λλ€.
μνμ μ·¨μν κ²½μ° μ·¨μν λͺ©λ‘μ λ£μ΄μ€λλ€.
κ°μ₯ μ΅κ³ μνμ νλ§€νκΈ° μν΄μ heapμ ν΅ν΄ μΆλ ₯ν΄μ€λλ€.
μ¬κΈ°μ λ§μ½ μ΄λ―Έ λ²λ¦° μνμ΄λΌλ©΄ continueλ₯Ό ν΅ν΄ μ§λμΉκ²λ©λλ€.
λμ°©μ§κ° λ³κ²½λλ€λ©΄, λ€μ λ€μ΅μ€νΈλΌλ₯Ό ν΅ν΄ κ°μ κ°±μ ν΄μ£Όκ³
Heapaμ μλ λ΄μ©λ λ³κ²½ν΄μ€λλ€.
π μλ‘κ² μκ²λ λ΄μ©
빑ꡬνμ νκ° λμ§λ§ λ€ νλ©΄ μΎκ°μ΄ μλ€μ..γ
μ¬λ°λ λ¬Έμ μμ΅λλ€.