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 = 1e17;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<array<int, 3>>> v(n);
for(int i = 0; i < m; i++){
int a, b, c, p; cin >> a >> b >> c >> p; a--, b--;
v[a].pb({b, c, p});
v[b].pb({a, c, p});
}
vector<pair<int, int>> nodes;
map<pair<int, int>, int> node_id;
for(int i = 0; i < n; i++) nodes.pb({i, -1});
for(int i = 0; i < n; i++){
for(auto [x, c, p]: v[i]) nodes.pb({i, x});
}
for(int i = 0; i < nodes.size(); i++) node_id[nodes[i]] = i;
vector<vector<pair<int, ll>>> g(nodes.size());
for(int i = 0; i < n; i++){
unordered_map<int, ll> cost;
for(auto [x, c, p]: v[i]) cost[c]+=p;
for(auto [x, c, p]: v[i]){
g[i].pb({x, min((ll)p, cost[c]-p)});
g[i].pb({node_id[{x, i}], p});
}
}
for(int i = 0; i < n; i++){
unordered_map<int, vector<pair<int, int>>> mp;
for(auto [x, c, p]: v[i]) mp[c].pb({p, x});
for(auto [cc, ss]: mp){
if(ss.size() < 2) continue;
sort(ss.rbegin(), ss.rend());
ll s = 0;
for(auto [p, x]: ss) s+=p;
for(int j = 0; j < ss.size(); j++){
ll cur = s-ss[j].f;
if(j == 0){
cur-=ss[1].f;
g[node_id[{i, ss[j].sc}]].pb({ss[1].sc, cur});
}
else{
cur-=ss[0].f;
g[node_id[{i, ss[j].sc}]].pb({ss[0].sc, cur});
}
}
}
}
vector<ll> dis(nodes.size(), inf);
vector<bool> bl(nodes.size(), 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;
for(auto [x, w]: g[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
ll ans = inf;
for(int i = 0; i < nodes.size(); i++) if(nodes[i].f == n-1) ans = min(ans, dis[i]);
if(ans >= inf) ans = -1;
cout << ans << "\n";
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:25:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i = 0; i < nodes.size(); i++) node_id[nodes[i]] = i;
| ~~^~~~~~~~~~~~~~
Main.cpp:43:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int j = 0; j < ss.size(); j++){
| ~~^~~~~~~~~~~
Main.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0; i < nodes.size(); i++) if(nodes[i].f == n-1) ans = min(ans, dis[i]);
| ~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |