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 n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 1e16;
const int N = 205;
const int M = 1005;
const int ALPH = 26;
const int LGN = 25;
const int MOD = 1e9+7;
int n,m,q;
vector<array<int,4> > edges;
vector<int> SP[2][N], SPR[2][N];
int ans = INF;
void dijkstra(vector<vector<array<int,2> > > &adj, vector<int> &sp, int src) {
sp.assign(n+3, INF);
vector<bool> vis(n+3, 0);
sp[src] = 0;
while(src) {
int cur = src;
src = 0;
vis[cur] = 1;
for(auto c : adj[cur]) {
sp[c[0]] = min(sp[c[0]] , sp[cur] + c[1]);
}
for(int i = 1; i<=n; i++) {
if(vis[i]) continue;
if(sp[src] > sp[i]) {
src = i;
}
}
}
}
bool dfs(int node, int fin, vector<int> &spath, vector<int> &sp, vector<vector<array<int,3> > > &adj, vector<int> &vis) {
vis[node] = 1;
if(node == fin) {
return 1;
}
for(auto c : adj[node]) {
if(vis[c[0]] || sp[node] + c[1] != sp[c[0]]) continue;
if(dfs(c[0], fin, spath, sp, adj, vis)) {
spath[c[2]] = 1;
return 1;
}
}
return 0;
}
void start_dfs(vector<int> &spat0, vector<int> &spat1, vector<int> &sp0, vector<int> &sp1) {
vector<int> vis(n+3, 0);
vector<vector<array<int,3> > > adj(n+3, vector<array<int,3> > ());
spat0.assign(m+3, 0); spat1.assign(m + 3, 0);
for(int i = 0; i<m; i++) {
auto eg = edges[i];
adj[eg[0]].pb({eg[1], eg[2], i});
}
dfs(1, n, spat0, sp0, adj, vis);
vis.assign(n+3, 0);
dfs(n, 1, spat1, sp1, adj, vis);
}
void new_dijk(vector<int> &sp, int ind, int src) {
vector<vector<array<int,2> > > adj(n+3, vector<array<int,2> >());
// adj[edges[ind][1]].pb({edges[ind][0], edges[ind][2]});
for(int i = 0; i<m; i++) {
if(i == ind) continue;
auto &eg = edges[i];
adj[eg[0]].pb({eg[1], eg[2]});
}
dijkstra(adj, sp, src);
}
inline void solve() {
cin>>n>>m;
edges.resize(m);
REP(i,m) {
cin>>edges[i][0]>>edges[i][1]>>edges[i][2]>>edges[i][3];
}
for(int i = 0; i<=n; i++) {
vector<vector<array<int,2> > > adj(n+3, vector<array<int,2> >());
for(auto eg : edges) {
if(eg[0] == i || eg[1] == i) continue;
adj[eg[0]].pb({eg[1], eg[2]});
}
dijkstra(adj, SP[0][i], 1);
dijkstra(adj, SP[1][i], n);
adj.assign(n+3, vector<array<int,2> > ());
for(auto eg : edges) {
if(eg[0] == i || eg[1] == i) continue;
adj[eg[1]].pb({eg[0], eg[2]});
}
dijkstra(adj, SPR[0][i], 1);
dijkstra(adj, SPR[1][i], n);
}
vector<int> spath[2];
start_dfs(spath[0], spath[1], SP[0][0], SP[1][0]);
// dbg(SP[0][0]);
ans = min(ans , SP[0][0][n] + SP[1][0][1]); //initial state
// dbg(ans);
// dbg(spath[0]); dbg(spath[1]);
for(int i = 0; i<m; i++) {
auto eg = edges[i];
int cost;
// dbg(eg);
// cout<<"find AB\n";
if(spath[0][i]) {
vector<int> sp;
new_dijk(sp, i, 1);
cost = sp[n];
}
else {
cost = SP[0][0][n];
}
// dbg(cost);
int pathAB = min( (SP[0][eg[0]][eg[1]] + SPR[1][eg[1]][eg[0]] + eg[2]) , cost);
// cout<<"find BA\n";
if(spath[1][i]) {
vector<int> sp;
new_dijk(sp, i, n);
cost = sp[1];
// cout<<"CRIT\n";
}
else {
cost = SP[1][0][1];
}
// dbg(cost);
int pathBA = min(SP[1][eg[0]][eg[1]] + SPR[0][eg[1]][eg[0]] + eg[2], cost);
int ret = pathAB + pathBA;
// dbg(pathAB); dbg(pathBA);
ret += eg[3];
// dbg(ret);
ans = min(ans , ret);
}
if(ans == INF) ans = -1;
cout<<ans<<"\n";
}
signed main() {
fastio();
solve();
}
Compilation message (stderr)
ho_t4.cpp: In function 'std::string to_string(std::string, int, int)':
ho_t4.cpp:6:212: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
| ~~~^~~~~~~~~~
ho_t4.cpp: In function 'void solve()':
ho_t4.cpp:13:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
13 | #define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
| ^
ho_t4.cpp:94:9: note: in expansion of macro 'REP'
94 | REP(i,m) {
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |