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>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const int N = 2e2 + 10;
const int M = 2e5 + 10;
const ll Inf = 1e15;
ll n, m;
ll u[M], v[M], c[M], d[M], ans[M];
ll G[N][N], W[N][N], mk[N], dis[N];
void Build(){
memset(G, 31, sizeof G);
memset(mk, 0, sizeof mk);
memset(dis, 31, sizeof dis);
memset(W, -1, sizeof W);
for(int i = 0; i < m; i++){
G[u[i]][v[i]] = min(G[u[i]][v[i]], c[i]);
if(G[u[i]][v[i]] == c[i]) W[u[i]][v[i]] = i;
}
}
ll Solve(int idx){
swap(u[idx], v[idx]);
Build();
dis[1] = 0;
for(int i = 0; i < n; i++){
ll mn = Inf, id = -1;
for(int j = 1; j <= n; j++)
if(!mk[j] && dis[j] < mn){
mn = dis[j];
id = j;
}
if(id == -1) break;
mk[id] = 1;
for(int j = 1; j <= n; j++)
if(dis[j] > dis[id] + G[id][j]){
dis[j] = dis[id] + G[id][j];
}
}
swap(u[idx], v[idx]);
return dis[n];
}
void Main(){
for(int i = 0; i < m; i++){
ans[i] += Solve(i);
}
return ;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) cin >> u[i] >> v[i] >> c[i] >> d[i];
ll Ans = Solve(m);
Main();
for(int i = 0; i < m; i++){
if(u[i] == 1 || u[i] == n) u[i] = n + 1 - u[i];
if(v[i] == 1 || v[i] == n) v[i] = n + 1 - v[i];
}
Ans += Solve(m);
Main();
for(int i = 0; i < m; i++) Ans = min(Ans, d[i] + ans[i]);
cout << (Ans < Inf ? Ans : -1) << '\n';
return 0;
}
/*
4 10
1 2 4 4
1 2 4 4
1 3 2 1
1 3 2 1
4 3 1 2
4 3 1 2
4 1 6 1
4 1 6 1
2 4 2 5
2 4 2 5
*/
# | 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... |