#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];
bool ban[M + 2] = {};
int n , m;
vector<int> ke[N + 2];
void add_canh(int u , int v , int id){
ke[u].push_back(id);
return;
}
LL dist[2][N + 2];
LL djisktra(int source , int sink){
for(int i = 1; i <= n; ++i) ke[i].clear();
for(int i = 1; i <= n; ++i) dist[0][i] = dist[1][i] = inf;
for(int i =1; i <= m; ++i) {
if (ban[i]==false) add_canh(x[i] , y[i] , i);
else add_canh(y[i] , x[i] , i);
}
priority_queue<pair<LL,int> , vector<pair<LL,int>> , greater<pair<LL,int>>> pq;
dist[0][1] = 0;
pq.push({dist[0][1] , 1});
while (pq.size()){
int u = pq.top().second;
LL cost = pq.top().first;
pq.pop();
if (cost != dist[0][u]) continue;
for(int id : ke[u]){
int v = u ^ x[id] ^ y[id];
if (minimize(dist[0][v] , cost + c[id])){
pq.push({dist[0][v] , v});
}
}
}
dist[1][n] = 0;
pq.push({dist[1][n] , n});
while (pq.size()){
int u = pq.top().second;
LL cost = pq.top().first;
pq.pop();
if (cost != dist[1][u]) continue;
for(int id : ke[u]){
int v = u ^ x[id] ^ y[id];
if (minimize(dist[1][v] , cost + c[id])){
pq.push({dist[1][v] , v});
}
}
}
return dist[0][n] + dist[1][1];
}
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];
ban[i] = false;
}
LL ans = djisktra(1 , n);
for(int i = 1; i <= m; ++i){
ban[i] = true;
minimize(ans , djisktra(1 , n) + d[i]);
ban[i] = false;
}
if (ans >= inf) ans = -1;
cout << ans;
return 0;
}
Compilation message (stderr)
ho_t4.cpp: In function 'int main()':
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".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:88:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | 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... |