This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e18;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<tuple<int, int, ll>>> v(n);
for(int i = 0; i < m; i++){
int a, b, c; ll p; cin >> a >> b >> c >> p; a--, b--, c--;;
v[a].pb({b, c, p});
v[b].pb({a, c, p});
}
vector<ll> dis(n, inf);
vector<bool> bl(n, 0);
priority_queue<pair<ll, int>> pq;
dis[0] = 0;
pq.push({0, 0});
while(!pq.empty()){
int nd = pq.top().sc; pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
map<int, ll> mp;
for(auto [x, c, p]: v[nd]) mp[c]+=p;
for(auto [x, c, p]: v[nd]){
ll w = min(p, mp[c]-p);
if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
}
cout << (dis[n-1]==inf?-1:dis[n-1]) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |