이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_set>
#include <vector>
#include <climits>
#include <list>
#include <stack>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using llu = unsigned long long;
using str = string; // yay python!
// pairs
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdb = pair<db,db>;
#define mp make_pair
#define fr first
#define sc second
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pll>;
using vpd = V<pdb>;
#define ms0(x) memset(x , 0, sizeof(x))
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
tcT> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); }
// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = (int)1e9+7; // 998244353;
const int MX = (int)2e5+5;
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4] = {0,1,0,-1};
const int dy[4] = {-1,0,1,0};
map<int, vector<tuple<int,int,int>>>edges[100001];
unordered_map<int, ll>dp2[100001];
unordered_map<int,ll>sum[100001];
ll dp[100001];
long long Robot(int N, int M, vector<tuple<int,int,int,int>>roads){
for (int i = 0; i<M; i++){
int a,b,c,d;
tie(a,b,c,d) = roads[i];
edges[a][c].pb({b,c,d});
edges[b][c].pb({a,c,d});
sum[a][c]+=d;
sum[b][c]+=d;
}
for (int i = 0; i<=N; i++){
dp[i] = 1e18;
}
dp[1] = 0;
priority_queue<tuple<ll, int, int>>q;
q.push(make_tuple(0,1,0));
while(!q.empty()){
ll dist;
int node, state;
tie(dist, node, state) = q.top();
dist = -dist;
q.pop();
if (state){
if (dp2[node][state] != dist)continue;
for (auto edge : edges[node][state]){
ll new_cost = dist + sum[node][state] - get<2>(edge);
if (new_cost < dp[get<0>(edge)]){
dp[get<0>(edge)] = new_cost;
q.push(make_tuple(-new_cost, get<0>(edge), 0));
}
}
}else{
if (dp[node] != dist)continue;
for (auto color : edges[node]){
for (auto edge : color.sc){
int next = get<0>(edge);
int col = get<1>(edge);
int p = get<2>(edge);
ll new_cost = dist + p;
if (new_cost < dp[next]){
dp[next] = new_cost;
q.push(make_tuple(-new_cost, next, 0));
}
new_cost = dist + sum[node][col] - p;
if (new_cost < dp[next]){
dp[next] = new_cost;
q.push(make_tuple(-new_cost, next, 0));
}
if (!dp2[next].count(col) || dp2[next][col] > dist){
dp2[next][col] = dist;
q.push(make_tuple(-dist, next, col));
}
}
}
}
}
return (dp[N] == 1e18 ? -1 : dp[N]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int N,M;
cin >> N >> M;
vector<tuple<int,int,int,int>>roads;
for (int i = 0; i<M; i++){
int a,b,c,p;
cin >> a >> b >> c >> p;
roads.pb({a,b,c,p});
}
cout<<Robot(N,M,roads);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |