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>
using namespace std;
#define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
#define SPEED {ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);}
#define fi first
#define se second
template<class T1, class T2> bool minimize(T1 &a, T2 b){return a > b ? a = b, true : false;}
template<class T1, class T2> bool maximize(T1 &a, T2 b){return a < b ? a = b, true : false;}
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 3;
const ll oo = 1e18;
struct adjNode{
pii v;
int c, p;
};
struct elem_in_PQ{
ll dist;
int vtx, c;
bool operator < (const elem_in_PQ &oth) const{
return dist > oth.dist;
}
};
int n, m;
map<int, ll> sump[N], d[N];
priority_queue<elem_in_PQ> pq;
map<int, vector<adjNode> > adj[N];
void add_edge(pii a, pii b, int c, int p){
adj[a.fi][a.se].push_back({b, c, p});
adj[b.fi][b.se].push_back({a, c, p});
}
int main(){
SPEED; fileIO("text");
cin >> n >> m;
for(int i = 1; i <= m; ++i){
int u, v, c, p;
cin >> u >> v >> c >> p;
add_edge({u, 0}, {v, 0}, c, p);
add_edge({u, 0}, {v, c}, c, p);
add_edge({u, c}, {v, 0}, c, p);
d[u][c] = oo; d[v][c] = oo;
sump[u][c] += p; sump[v][c] += p;
}
for(int u = 1; u <= n; ++u) d[u][0] = oo;
d[1][0] = 0; pq.push({0LL, 1, 0});
while(!pq.empty()){
pii u = {pq.top().vtx, pq.top().c};
ll du = pq.top().dist; pq.pop();
if(du != d[u.fi][u.se]) continue;
for(auto [v, c, p] : adj[u.fi][u.se]){
if(u.se == 0){
if(v.se == 0 && minimize(d[v.fi][v.se], min(1LL * p, sump[u.fi][c] - p) + du)) pq.push({d[v.fi][v.se], v.fi, v.se});
if(v.se != 0 && minimize(d[v.fi][v.se], du)) pq.push({d[v.fi][v.se], v.fi, v.se});
}
else if(minimize(d[v.fi][v.se], sump[u.fi][c] - p + du)) pq.push({d[v.fi][v.se], v.fi, v.se});
}
}
cout << (d[n][0] == oo ? -1 : d[n][0]);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:4:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
4 | #define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:35:12: note: in expansion of macro 'fileIO'
35 | SPEED; fileIO("text");
| ^~~~~~
Main.cpp:4:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
4 | #define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:35:12: note: in expansion of macro 'fileIO'
35 | SPEED; fileIO("text");
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |