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;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#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 each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
return b > a ? a=b,1 : 0; }
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int MX = 1e5+5;
struct Edge {
int to, c;
ll p;
};
struct Dat {
ll curdist;
int u, c;
bool operator<(const Dat&rhs) const {
return curdist > rhs.curdist;
}
};
map<int,V<Edge>> adj[MX];
ll dist[MX];
map<int,ll> dist2[MX], psum[MX];
int N, M;
void solve() {
cin >> N >> M;
F0R(i, M) {
int u, v, c;
ll p;
cin >> u >> v >> c >> p;
adj[u][c].pb({v, c, p});
adj[v][c].pb({u, c, p});
psum[u][c] += p;
psum[v][c] += p;
}
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<Dat> pq;
pq.push({0, 1, 0});
while(!pq.empty()) {
ll du = pq.top().curdist;
int u = pq.top().u, c = pq.top().c;
pq.pop();
if(c) {
if(dist2[u][c] != du) continue;
each(ed, adj[u][c]) {
int v = ed.to, col = ed.c;
ll dv = du + psum[u][col] - ed.p;
// not flip color c
if(ckmin(dist[v], dv)) {
pq.push({dist[v], v, 0});
}
}
}
else {
if(dist[u] != du) continue;
each(edmp, adj[u]) {
each(ed, edmp.se) {
//CASE 1 : not change this color
int v = ed.to;
ll dv = du + psum[u][ed.c] - ed.p;
if(ckmin(dist[v], dv)) {
pq.push({dist[v], v, 0});
}
//CASE 2 != 1
dv = du + ed.p;
if(ckmin(dist[v], dv)) {
pq.push({dist[v], v, 0});
}
//CASE 3
dv = du;
if(dist2[v].find(ed.c) == dist2[v].end() || dv < dist2[v][ed.c]) {
dist2[v][ed.c] = dv;
pq.push({dv, v, ed.c});
}
}
}
}
}
ll ans = dist[N];
if(ans >= INF) ans = -1;
cout << ans << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |