#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define FOR(i,a,b) for(int i = (a) , _b = (b); i <= _b; ++i)
#define MASK(x) ((LL)(1) << (x))
#define BIT(mask , x) (((mask) >> (x)) & (1))
#define sz(x) (int)(x).size()
#define TIME_USED cerr << "\n Time lapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
template<class T1 , class T2>
bool maximize(T1 &a , T2 b){
if (a < b) return a = b , true; else return false;
}
template<class T1 , class T2>
bool minimize(T1 &a , T2 b){
if (a > b) return a = b , true; else return false;
}
template<class T>
void compress(vector<T>&data){
sort(data.begin() , data.end());
data.resize(unique(data.begin() , data.end()) - data.begin());
}
template<class T1 , class T2>
T2 Find(const vector<T1>&data , T2 y){
return upper_bound(data.begin() , data.end() , y) - data.begin();
}
const int N = (int) 200;
const int M = (int) 5e4;
const LL inf = (LL)1e18;
int x[M + 2] , y[M + 2] , c[M + 2] , d[M + 2];
int n , m;
vector<int> ke[N + 2][2];
void add_canh(int u , int v , int id , int t){
ke[u][t].push_back(id);
return;
}
int belong_tree[M + 2] = {};
int trace[N + 2] = {};
bool ban[M + 2] = {};
void djisktra(int source , int sink , int t , bool regis , LL dist[]){
for(int i = 1; i <= n; ++i) dist[i] = inf;
for(int i = 1; i <= n; ++i) trace[i] = false;
priority_queue<pair<LL,int>,vector<pair<LL,int>>,greater<pair<LL,int>>>pq;
dist[source] = 0;
pq.push({dist[source] , source});
while (pq.size()){
int u = pq.top().second;
LL cost = pq.top().first;
pq.pop();
if (cost != dist[u]) continue;
for(int id : ke[u][!t]){
if (ban[id] == false) continue;
int v = u ^ x[id] ^ y[id];
if (minimize(dist[v] , dist[u] + c[id])) {
trace[v] = id;
pq.push({dist[v] , v});
}
}
for(int id : ke[u][t]){
if (ban[id] == true) continue;
int v = u ^ x[id] ^ y[id];
if (minimize(dist[v] , dist[u] + c[id])) {
trace[v] = id;
pq.push({dist[v] , v});
}
}
}
for(int i = 1; i <= n; ++i) belong_tree[trace[i]] |= (regis & true);
return;
}
LL dist[2][N + 2] = {};
LL rev_dist[2][N + 2] = {};
LL tmp[2][N + 2] = {};
int main(){
ios::sync_with_stdio(false);
cin.tie(0) ; cout.tie(0) ;
#define name "main"
if (fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
cin >> x[i] >> y[i] >> c[i] >> d[i];
add_canh(x[i] , y[i] , i , 0);
add_canh(y[i] , x[i] , i , 1);
}
djisktra(1 , n , 0 , 1 , dist[0]) , djisktra(1 , n , 1 , 1 , rev_dist[0]);
djisktra(n , 1 , 0 , 1 , dist[1]) , djisktra(n , 1 , 1 , 1 , rev_dist[1]);
LL ans = dist[0][n] + dist[1][1];
// for(int i = 1; i <= n; ++i) cout << rev_dist[0][i] << ' ' ; cout << '\n';
for(int i = 1; i <= m; ++i){
if (belong_tree[i]){
ban[i] = true;
djisktra(1 , n , 0 , 0 , tmp[0]) ,
djisktra(n , 1 , 0 , 0 , tmp[1]);
LL cost = tmp[0][n] + tmp[1][1] + d[i];
minimize(ans , cost);
ban[i] = false;
}
else {
int u = y[i] , v = x[i];
LL cost1 = min({dist[0][n] , dist[0][u] + c[i] + rev_dist[1][v] , dist[0][v] + c[i] + rev_dist[1][u]});
LL cost2 = min({dist[1][1] , dist[1][u] + c[i] + rev_dist[0][v] , dist[1][v] + c[i] + rev_dist[0][u]});
minimize(ans , cost1 + cost2 + d[i]);
// cout << i << ' ' << dist[0][n] << ' ' << cost2 + d[i] << '\n';
}
}
if (ans >= inf) ans = -1;
cout << ans;
return 0;
}
Compilation message (stderr)
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:86:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:87:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(name".out","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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |