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>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
// #define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pip pair<int, pii>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const ll inf = 1ll<<60;
const int iinf=2147483647;
const ll mod = 1e9+7;
const ll maxn=205;
const ll maxm=5e4+5;
const double PI=acos(-1);
ll pw(ll x, ll p, ll m=mod){
ll ret=1;
while (p>0){
if (p&1){
ret*=x;
ret%=m;
}
x*=x;
x%=m;
p>>=1;
}
return ret;
}
ll inv(ll a, ll m=mod){
return pw(a,m-2,m);
}
struct edge{
int to, cap, rev;
int id;
};
struct inedge{
int u, v, c, d;
int id;
};
bool operator <(pip a, pip b){
return a.f<b.f;
}
int n, m;
vector<edge> graph[maxn];
int dis[maxn][4], preev[maxn][4];
int dd[maxn][maxn];
bool occ[maxm][2];
vector<inedge> e(maxm);
vector<int> cores;
void bck(bool typ){
cores = {1, n};
int cur = cores[typ^1];
if (dis[cur][typ] == iinf) return;
while(cur != cores[typ]){
occ[preev[cur][typ]][typ]=1;
cur=e[preev[cur][typ]].u;
}
}
void dij(int typ, int bn){ // bn is banned index
priority_queue<pip, vector<pip>, greater<pip> > pq;
if (typ%2 == 0) pq.push({0, {1, -1}});
else pq.push({0, {n, -1}});
while(pq.size()){
pip x=pq.top(); pq.pop();
if (dis[x.s.f][typ] != iinf) continue;
preev[x.s.f][typ] = x.s.s;
dis[x.s.f][typ] = x.f;
if (bn!=-1 && e[bn].v == x.s.f){
if (dis[e[bn].u][typ] == iinf){
pq.push({x.f+e[bn].c, {e[bn].u, bn}});
}
}
REP(i, SZ(graph[x.s.f])){
if (dis[graph[x.s.f][i].to][typ] == iinf && graph[x.s.f][i].id != bn){
pq.push({x.f+graph[x.s.f][i].cap, {graph[x.s.f][i].to, graph[x.s.f][i].id}});
}
}
}
if (typ<=1) bck(typ);
}
void floy(){
REP1(i, n) REP1(j, n) dd[i][j] = ((i==j)?0:iinf);
REP(i, m){
dd[e[i].u][e[i].v]=min(dd[e[i].u][e[i].v], e[i].c);
}
REP1(k, n){
REP1(i, n){
REP1(j, n){
dd[i][j] = min(dd[i][j], dd[i][k]+dd[k][j]);
}
}
}
}
void solve(){
cin>>n>>m;
REP1(i, n){
dis[i][0]=dis[i][1]=iinf;
}
REP(i, m){
cin>>e[i].u>>e[i].v>>e[i].c>>e[i].d;
e[i].id=i;
graph[e[i].u].pb({e[i].v, e[i].c, e[i].d, i});
}
floy();
dij(0, -1);
dij(1, -1);
int ret = dd[1][n]+dd[n][1];
REP(i, m){
int m0, m1;
if (!occ[i][0] && !occ[i][1]){
m0 = min(dd[1][n], dd[1][e[i].v]+e[i].c+dd[e[i].u][n]);
m1 = min(dd[n][1], dd[n][e[i].v]+e[i].c+dd[e[i].u][1]);
ret = min(ret, m0+m1+e[i].d);
continue;
}
REP1(j, n){
dis[j][2]=dis[j][3]=iinf;
}
dij(2, i);
dij(3, i);
ret = min(ret, e[i].d+dis[n][2]+dis[1][3]);
}
if (ret>=iinf) ret=-1;
cout<<ret<<endl;
}
signed main (){
IOS();
solve();
}
/*
test 1
output: 26
4 7
1 2 100 1000
2 3 21 2
3 4 100 1000
1 3 1 1000
2 4 1 1000
1 4 101 1000
4 1 1 1000
test 2
output: 16
4 6
1 4 10 1000
2 3 1 1
1 2 2 1000
3 4 2 1000
4 3 2 1000
2 1 2 1000
*/
# | 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... |