이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Dost SEFEROĞLU
#include <bits/stdc++.h>
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
int MOD = 1e9+7,inf = 2e18;
const int N = 1e5+50,Q = 2e5+50;
map<int,int> dp[N];
map<int,int> sm[N];
void solve() {
int n,m;
cin >> n >> m;
vector<pii> edges[n+1];
vi c(m+1),p(m+1);
for (int i=1;i<=m;i++) {
int a,b;
cin >> a >> b >> c[i] >> p[i];
edges[a].push_back({b,i});
edges[b].push_back({a,i});
sm[a][c[i]]+=p[i];
sm[b][c[i]]+=p[i];
dp[a][i] = dp[b][i] = inf;
}
for (int i=1;i<=n;i++) dp[i][0] = inf;
dp[1][0] = 0;
using state = pair<int,pii>;
priority_queue<state,vector<state>,greater<state>> pq;
pq.push({0,{1,0}});
while (!pq.empty()) {
int cost = pq.top().ff;
auto [city,last] = pq.top().ss;
pq.pop();
if (dp[city][last] < cost) continue;
for (auto& [go,id] : edges[city]) {
int costy = sm[city][c[id]];
if (last && c[id] == c[last]) costy-=p[last];
if (cost+costy-p[id] < dp[go][0]) {
dp[go][0] = cost+costy-p[id];
pq.push(state{dp[go][0],{go,0}});
}
if (cost+p[id] < dp[go][id]) {
dp[go][id] = cost+p[id];
pq.push(state{dp[go][id],{go,id}});
}
}
}
int ans = inf;
for (auto it : dp[n]) ans = min(ans,it.ss);
if (ans < inf) cout << ans << '\n';
else cout << -1 << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);/*
#else
freopen("shortcut.in","r",stdin);
freopen("shortcut.out","w",stdout); */
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |