#pragma GCC optimize("Ofast,no-stack-protector")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl "\n"
using ull=unsigned long long;
using pii=pair<int,int>;
const int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1};
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int maxn = 200+100;
const int mod = 1e9+7;
template < int MOD = 1000000007, typename T = int > struct ModInt {
T val;
ModInt(T V = 0) : val(V) { val %= MOD; }
ModInt& operator += (const ModInt& rhs) {
if ((val += rhs.val) >= MOD) val -= MOD;
return *this;
}
ModInt& operator += (const T rhs) {
if ((val += rhs) >= MOD) val -= MOD;
return *this;
}
ModInt& operator -= (const ModInt& rhs) {
if ((val += MOD - rhs.val) >= MOD) val -= MOD;
return *this;
}
ModInt& operator -= (const T rhs) {
if ((val += MOD - rhs) >= MOD) val -= MOD;
return *this;
}
ModInt& operator *= (const ModInt& rhs) { val = (1ll * val * rhs.val) % MOD; return *this; }
ModInt& operator *= (const T rhs) { val = (1ll * val * rhs) % MOD; return *this; }
ModInt& operator /= (const ModInt& rhs) { return *this *= rhs.inverse(); }
ModInt& operator /= (const T rhs) { return *this *= ModInt(rhs).inverse(); }
ModInt& operator %= (const ModInt& rhs) { val %= rhs.val; return *this; }
ModInt& operator %= (const T rhs) { val %= rhs; return *this; }
ModInt& operator ++() { return *this += 1; }
ModInt& operator --() { return *this -= 1; }
ModInt operator + (const ModInt& rhs) const { ModInt res(*this); return res += rhs; }
ModInt operator + (const T rhs) const { ModInt res(*this); return res += rhs; }
ModInt operator % (const ModInt& rhs) const { ModInt res(*this); return res %= rhs; }
ModInt operator % (const T rhs) const { ModInt res(*this); return res %= rhs; }
ModInt operator - (const ModInt& rhs) const { ModInt res(*this); return res -= rhs; }
ModInt operator - (const T rhs) const { ModInt res(*this); return res -= rhs; }
ModInt operator * (const ModInt& rhs) const { ModInt res(*this); return res *= rhs; }
ModInt operator * (const T rhs) const { ModInt res(*this); return res *= rhs; }
ModInt operator / (const ModInt& rhs) const { ModInt res(*this); return res /= rhs; }
ModInt operator / (const T rhs) const { ModInt res(*this); return res /= rhs; }
ModInt& operator = (const ModInt& rhs) { val = rhs.val; return *this; }
ModInt& operator = (const T rhs) { val = rhs; return *this; }
T operator ~ () { return ~val; }
bool operator ! () { return !val; }
bool operator == (const ModInt& rhs) const { return val == rhs.val; }
bool operator == (const T rhs) const { return val == rhs; }
bool operator != (const ModInt& rhs) const { return val != rhs.val; }
bool operator != (const T rhs) const { return val != rhs; }
bool operator < (const ModInt& rhs) const { return val < rhs.val; }
bool operator < (const T rhs) const { return val < rhs; }
bool operator <= (const ModInt& rhs) const { return val <= rhs.val; }
bool operator <= (const T rhs) const { return val <= rhs; }
bool operator > (const ModInt& rhs) const { return val > rhs.val; }
bool operator > (const T rhs) const { return val > rhs; }
bool operator >= (const ModInt& rhs) const { return val >= rhs.val; }
bool operator >= (const T rhs) const { return val >= rhs; }
T operator () () const { return val; }
ModInt inverse() const { return power(MOD - 2); }
ModInt power(T n) const {
ModInt a = *this, res = 1;
while (n > 0) {
if (n & 1) res *= a;
a *= a, n >>= 1;
}
return res;
}
ModInt power(ModInt n) const {
ModInt a = *this, res = 1;
T e = n();
while (e > 0) {
if (e & 1) res *= a;
a *= a, e >>= 1;
}
return res;
}
friend ModInt operator ^ (ModInt rhs, T n) { return rhs.power(n); }
friend ModInt operator ^ (ModInt rhs, ModInt n) { return rhs.power(n); }
friend std::istream& operator>>(std::istream& is, ModInt& x) noexcept { return is >> x.val; }
friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
};
using mint = ModInt < (int)1e9+7 >;
set<pair<int,int>> v[maxn];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin>>n>>m;
vector<vector<int>> edges;
for(int i = 0;i<m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
edges.pb({a,b,c,d});
v[a].insert({b,c});
}
int dp[n+1][n+1];
for(int i = 0;i<=n;i++){
for(int j = 0;j<=n;j++){
if(i==j) dp[i][j] = 0;
else dp[i][j] = 1e18;
}
}
for(auto x : edges){
dp[x[0]][x[1]] = min(dp[x[0]][x[1]],x[2]);
}
for(int k = 1;k<=n;k++){
for(int i = 1;i<=n;i++){
for(int j =1;j<=n;j++){
dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]);
}
}
}
int ans = 1e18;
ans = min(ans,dp[1][n]+dp[n][1]);
for(auto x : edges){
int a = x[0];
int b = x[1];
int c = x[2];
int d = x[3];
int cost1 = dp[1][n];
int c2 = cost1+dp[n][b]+c+dp[a][1]+d;
ans = min(ans,c2);
int c3 = d+dp[1][b]+c+dp[a][n]+dp[n][1];
ans = min(ans,c3);
int c4 = d+dp[1][n]+dp[n][a]+c+dp[b][1];
ans = min(ans,c4);
}
if(ans == 1e18) ans = -1;
cout<<ans<<endl;
}
# | 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... |