// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define endl '\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define py cout << "YES" << endl;
#define pn cout << "NO" << endl;
#define pb push_back
#define int long long
typedef long double lld;
#define double lld
typedef long long ll;
typedef unsigned long long ull;
const ll inf = 1e18;
const ll mod = 1e9+7;
const int N = 2e5;
vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
// Check for queue, priorioty_queue, stack, ordered_set solutions
// stack => LIFO (whatever goes in last comes out last)
// queue => FIFO (whatever goes in first comes out first)
// priority_queue => Dynamic queries of minimum/maximum
// ordered_set => set in vector form
//[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element]
// To comment multiple lines : ctrl + /
// To find and replace : ctrl+H
void judge(){
srand(time(NULL));
#ifndef ONLINE_JUDGE
freopen("inputB.txt","r",stdin);
freopen("outputB.txt","w",stdout);
freopen("Error.txt", "w", stderr);
#define debug(x...) cerr << #x <<" = "; _print(x); cerr << endl;
#else
#define debug(x...)
#endif
}
void usaco(string s) {
freopen((s + ".in").c_str(),"r",stdin);
freopen((s + ".out").c_str(),"w",stdout);
}
int dijkstra(vector<set<pair<int,ll>>>&adj, vector<int>src){
int n = adj.size();
vector<ll>dist(n,inf);
priority_queue<pair<ll,int>>pq;
for(auto source : src){
dist[source] = 0;
pq.push({0,source});
}
while(!pq.empty()){
auto x = pq.top();
ll d = -1*x.first; int node = x.second;
pq.pop();
if(d!=dist[node]){
continue;
}
for(auto child : adj[node]){
int v = child.first; ll w = child.second;
if((d+w)<dist[v]){
dist[v] = d+w;
pq.push({-1*(d+w),v});
}
}
}
return dist[0]+dist[n-1];
}
signed main(){
fastio; //judge();
int n,m; cin >> n >> m;
vector<int> cost[n][n], invert[n][n];
vector<vector<int>>type(m);
for(int i = 0,u,v,c,d;i<m;i++){
cin >> u >> v >> c >> d; u--; v--;
cost[u][v].pb(c);
type[i] = {u,v,c,d};
}
int best = inf;
vector<set<pair<int,int>>>adj(n);
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
if(cost[i][j].size()>0){
sort(cost[i][j].begin(),cost[i][j].end());
adj[i].insert({j,cost[i][j][0]});
}
}
}
best = min(best,dijkstra(adj,{0})+dijkstra(adj,{n-1}));
for(int i = 0;i<m;i++){
int u = type[i][0], v = type[i][1], c = type[i][2], d = type[i][3];
if(cost[u][v][0]==c){
adj[u].erase(adj[u].find({v,c}));
if(cost[u][v].size()>1){
adj[u].insert({v,cost[u][v][1]});
}
if(cost[v][u].size()==0){
adj[v].insert({u,c});
best = min(best,d+dijkstra(adj,{0})+dijkstra(adj,{n-1}));
adj[v].erase(adj[v].find({u,c}));
}
else if(cost[v][u][0]>c){
adj[v].erase(adj[v].find({u,cost[v][u][0]}));
adj[v].insert({u,c});
best = min(best,d+dijkstra(adj,{0})+dijkstra(adj,{n-1}));
adj[v].erase(adj[v].find({u,c}));
adj[v].insert({u,cost[v][u][0]});
}
adj[u].insert({v,c});
}
else{
if(cost[v][u].size()==0){
adj[v].insert({u,c});
best = min(best,d+dijkstra(adj,{0})+dijkstra(adj,{n-1}));
adj[v].erase(adj[v].find({u,c}));
}
else if(cost[v][u][0]>c){
adj[v].erase(adj[v].find({u,cost[v][u][0]}));
adj[v].insert({u,c});
best = min(best,d+dijkstra(adj,{0})+dijkstra(adj,{n-1}));
adj[v].erase(adj[v].find({u,c}));
adj[v].insert({u,cost[v][u][0]});
}
}
}
if(best==inf){
cout << -1;
}
else{
cout << best;
}
}
Compilation message (stderr)
ho_t4.cpp: In function 'void judge()':
ho_t4.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen("inputB.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen("outputB.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen("Error.txt", "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'void usaco(std::string)':
ho_t4.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen((s + ".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen((s + ".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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |