#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] == inf) 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] != inf) 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] == inf){
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] == inf && 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:inf);
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]=inf;
}
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]=inf;
}
dij(2, i);
dij(3, i);
ret = min(ret, e[i].d+dis[n][2]+dis[1][3]);
}
if (ret>=inf) 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 |
1 |
Correct |
13 ms |
2644 KB |
Output is correct |
2 |
Correct |
9 ms |
2644 KB |
Output is correct |
3 |
Correct |
10 ms |
2696 KB |
Output is correct |
4 |
Correct |
10 ms |
2700 KB |
Output is correct |
5 |
Correct |
3 ms |
2444 KB |
Output is correct |
6 |
Correct |
8 ms |
2560 KB |
Output is correct |
7 |
Correct |
1 ms |
2260 KB |
Output is correct |
8 |
Correct |
1 ms |
2308 KB |
Output is correct |
9 |
Correct |
2 ms |
2316 KB |
Output is correct |
10 |
Correct |
35 ms |
2712 KB |
Output is correct |
11 |
Correct |
42 ms |
2644 KB |
Output is correct |
12 |
Correct |
42 ms |
2644 KB |
Output is correct |
13 |
Correct |
9 ms |
2644 KB |
Output is correct |
14 |
Correct |
10 ms |
2700 KB |
Output is correct |
15 |
Correct |
9 ms |
2712 KB |
Output is correct |
16 |
Correct |
9 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
7452 KB |
Output is correct |
2 |
Correct |
100 ms |
7368 KB |
Output is correct |
3 |
Correct |
97 ms |
7744 KB |
Output is correct |
4 |
Correct |
11 ms |
2772 KB |
Output is correct |
5 |
Correct |
14 ms |
2700 KB |
Output is correct |
6 |
Correct |
10 ms |
2568 KB |
Output is correct |
7 |
Correct |
9 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2260 KB |
Output is correct |
9 |
Correct |
29 ms |
6584 KB |
Output is correct |
10 |
Correct |
28 ms |
6684 KB |
Output is correct |
11 |
Correct |
91 ms |
7424 KB |
Output is correct |
12 |
Correct |
78 ms |
7700 KB |
Output is correct |
13 |
Correct |
76 ms |
7632 KB |
Output is correct |
14 |
Correct |
76 ms |
7564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
2696 KB |
Output is correct |
2 |
Correct |
8 ms |
2556 KB |
Output is correct |
3 |
Correct |
50 ms |
5816 KB |
Output is correct |
4 |
Correct |
8 ms |
2656 KB |
Output is correct |
5 |
Correct |
109 ms |
6760 KB |
Output is correct |
6 |
Correct |
2 ms |
2304 KB |
Output is correct |
7 |
Correct |
2 ms |
2276 KB |
Output is correct |
8 |
Correct |
29 ms |
6312 KB |
Output is correct |
9 |
Correct |
35 ms |
6188 KB |
Output is correct |
10 |
Correct |
99 ms |
6576 KB |
Output is correct |
11 |
Correct |
63 ms |
6732 KB |
Output is correct |
12 |
Correct |
68 ms |
6784 KB |
Output is correct |
13 |
Correct |
1 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2260 KB |
Output is correct |
16 |
Correct |
1 ms |
2260 KB |
Output is correct |
17 |
Correct |
1 ms |
2260 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
98 ms |
6820 KB |
Output is correct |
20 |
Correct |
80 ms |
6548 KB |
Output is correct |
21 |
Correct |
66 ms |
6636 KB |
Output is correct |
22 |
Correct |
72 ms |
6592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2644 KB |
Output is correct |
2 |
Correct |
9 ms |
2644 KB |
Output is correct |
3 |
Correct |
10 ms |
2696 KB |
Output is correct |
4 |
Correct |
10 ms |
2700 KB |
Output is correct |
5 |
Correct |
3 ms |
2444 KB |
Output is correct |
6 |
Correct |
8 ms |
2560 KB |
Output is correct |
7 |
Correct |
1 ms |
2260 KB |
Output is correct |
8 |
Correct |
1 ms |
2308 KB |
Output is correct |
9 |
Correct |
2 ms |
2316 KB |
Output is correct |
10 |
Correct |
35 ms |
2712 KB |
Output is correct |
11 |
Correct |
42 ms |
2644 KB |
Output is correct |
12 |
Correct |
42 ms |
2644 KB |
Output is correct |
13 |
Correct |
9 ms |
2644 KB |
Output is correct |
14 |
Correct |
10 ms |
2700 KB |
Output is correct |
15 |
Correct |
9 ms |
2712 KB |
Output is correct |
16 |
Correct |
9 ms |
2644 KB |
Output is correct |
17 |
Correct |
103 ms |
7452 KB |
Output is correct |
18 |
Correct |
100 ms |
7368 KB |
Output is correct |
19 |
Correct |
97 ms |
7744 KB |
Output is correct |
20 |
Correct |
11 ms |
2772 KB |
Output is correct |
21 |
Correct |
14 ms |
2700 KB |
Output is correct |
22 |
Correct |
10 ms |
2568 KB |
Output is correct |
23 |
Correct |
9 ms |
2644 KB |
Output is correct |
24 |
Correct |
1 ms |
2260 KB |
Output is correct |
25 |
Correct |
29 ms |
6584 KB |
Output is correct |
26 |
Correct |
28 ms |
6684 KB |
Output is correct |
27 |
Correct |
91 ms |
7424 KB |
Output is correct |
28 |
Correct |
78 ms |
7700 KB |
Output is correct |
29 |
Correct |
76 ms |
7632 KB |
Output is correct |
30 |
Correct |
76 ms |
7564 KB |
Output is correct |
31 |
Correct |
12 ms |
2696 KB |
Output is correct |
32 |
Correct |
8 ms |
2556 KB |
Output is correct |
33 |
Correct |
50 ms |
5816 KB |
Output is correct |
34 |
Correct |
8 ms |
2656 KB |
Output is correct |
35 |
Correct |
109 ms |
6760 KB |
Output is correct |
36 |
Correct |
2 ms |
2304 KB |
Output is correct |
37 |
Correct |
2 ms |
2276 KB |
Output is correct |
38 |
Correct |
29 ms |
6312 KB |
Output is correct |
39 |
Correct |
35 ms |
6188 KB |
Output is correct |
40 |
Correct |
99 ms |
6576 KB |
Output is correct |
41 |
Correct |
63 ms |
6732 KB |
Output is correct |
42 |
Correct |
68 ms |
6784 KB |
Output is correct |
43 |
Correct |
1 ms |
2260 KB |
Output is correct |
44 |
Correct |
1 ms |
2260 KB |
Output is correct |
45 |
Correct |
1 ms |
2260 KB |
Output is correct |
46 |
Correct |
1 ms |
2260 KB |
Output is correct |
47 |
Correct |
1 ms |
2260 KB |
Output is correct |
48 |
Correct |
1 ms |
2260 KB |
Output is correct |
49 |
Correct |
98 ms |
6820 KB |
Output is correct |
50 |
Correct |
80 ms |
6548 KB |
Output is correct |
51 |
Correct |
66 ms |
6636 KB |
Output is correct |
52 |
Correct |
72 ms |
6592 KB |
Output is correct |
53 |
Correct |
88 ms |
7376 KB |
Output is correct |
54 |
Correct |
137 ms |
7424 KB |
Output is correct |
55 |
Correct |
122 ms |
7420 KB |
Output is correct |
56 |
Correct |
11 ms |
2736 KB |
Output is correct |
57 |
Correct |
10 ms |
2772 KB |
Output is correct |
58 |
Execution timed out |
1090 ms |
6540 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |