#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 inf=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][4];
vector<inedge> e(maxm);
vector<int> cores;
void bck(bool typ){
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){
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;
REP(i, SZ(graph[x.s.f])){
if (dis[graph[x.s.f][i].to][typ] == inf){
pq.push({x.f+graph[x.s.f][i].cap, {graph[x.s.f][i].to, graph[x.s.f][i].id}});
}
}
}
bck(typ);
}
void dij2(int typ, int bn){ // bn is banned index
dis[cores[typ%2]][typ] = 0;
REP1(i, n){
pii cmn = {inf, -1};
REP1(j, n){
if (dis[j][typ] < cmn.f && !occ[j][typ]){
cmn.f=dis[j][typ];
cmn.s=j;
}
}
if (cmn.s == -1) break;
occ[cmn.s][typ]=1;
if (cmn.s == e[bn].v) dis[e[bn].u][typ]=min(dis[e[bn].u][typ], dis[cmn.s][typ]+e[bn].c);
REP(j, SZ(graph[cmn.s])){
if (graph[cmn.s][j].id != bn && occ[graph[cmn.s][j].to][typ] == 0){
dis[graph[cmn.s][j].to][typ] = min(dis[graph[cmn.s][j].to][typ], dis[cmn.s][typ]+graph[cmn.s][j].cap);
}
}
}
}
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){
if (dd[i][k] == inf || dd[k][j] == inf) continue;
dd[i][j] = min(dd[i][j], dd[i][k]+dd[k][j]);
}
}
}
}
int ret;
void solve(){
cores={1, n};
floy();
dij(0);
dij(1);
if (dd[1][n] == inf || dd[n][1] == inf) ret=inf;
else ret = dd[1][n]+dd[n][1];
REP(i, m){
int m0, m1;
if (!occ[i][0] && !occ[i][1]){
m0 = dd[1][n];
if (dd[1][e[i].v] != inf && dd[e[i].u][n] != inf) m0=min(m0, dd[1][e[i].v]+e[i].c+dd[e[i].u][n]);
m1 = dd[n][1];
if (dd[n][e[i].v] != inf && dd[e[i].u][1] != inf) m1=min(m1, dd[n][e[i].v]+e[i].c+dd[e[i].u][1]);
if (m0!=inf && m1!=inf) ret = min(ret, m0+m1+e[i].d);
continue;
}
REP1(j, n){
dis[j][2]=dis[j][3]=inf;
occ[j][2] = occ[j][3] = 0;
}
dij2(2, i);
dij2(3, i);
if (dis[n][2]!=inf && dis[1][3]!=inf) ret = min(ret, e[i].d+dis[n][2]+dis[1][3]);
}
if (ret>=inf) ret=-1;
}
signed main (){
IOS();
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});
}
solve();
cout<<ret<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1540 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
10 ms |
1492 KB |
Output is correct |
4 |
Correct |
10 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1364 KB |
Output is correct |
6 |
Correct |
2 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
2 ms |
1236 KB |
Output is correct |
9 |
Correct |
2 ms |
1364 KB |
Output is correct |
10 |
Correct |
42 ms |
1540 KB |
Output is correct |
11 |
Correct |
45 ms |
1492 KB |
Output is correct |
12 |
Correct |
38 ms |
1544 KB |
Output is correct |
13 |
Correct |
5 ms |
1496 KB |
Output is correct |
14 |
Correct |
6 ms |
1492 KB |
Output is correct |
15 |
Correct |
6 ms |
1544 KB |
Output is correct |
16 |
Correct |
7 ms |
1464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
4472 KB |
Output is correct |
2 |
Correct |
47 ms |
4384 KB |
Output is correct |
3 |
Correct |
40 ms |
4584 KB |
Output is correct |
4 |
Correct |
8 ms |
1508 KB |
Output is correct |
5 |
Correct |
6 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
1464 KB |
Output is correct |
7 |
Correct |
2 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1332 KB |
Output is correct |
9 |
Correct |
27 ms |
4172 KB |
Output is correct |
10 |
Correct |
26 ms |
4084 KB |
Output is correct |
11 |
Correct |
41 ms |
4440 KB |
Output is correct |
12 |
Correct |
48 ms |
4440 KB |
Output is correct |
13 |
Correct |
45 ms |
4604 KB |
Output is correct |
14 |
Correct |
32 ms |
4572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1472 KB |
Output is correct |
2 |
Correct |
2 ms |
1492 KB |
Output is correct |
3 |
Correct |
35 ms |
3428 KB |
Output is correct |
4 |
Correct |
2 ms |
1492 KB |
Output is correct |
5 |
Correct |
40 ms |
3972 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1336 KB |
Output is correct |
8 |
Correct |
40 ms |
3828 KB |
Output is correct |
9 |
Correct |
34 ms |
3788 KB |
Output is correct |
10 |
Correct |
33 ms |
3996 KB |
Output is correct |
11 |
Correct |
36 ms |
3948 KB |
Output is correct |
12 |
Correct |
35 ms |
4024 KB |
Output is correct |
13 |
Correct |
1 ms |
1324 KB |
Output is correct |
14 |
Correct |
1 ms |
1328 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1324 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
37 ms |
4080 KB |
Output is correct |
20 |
Correct |
36 ms |
3964 KB |
Output is correct |
21 |
Correct |
31 ms |
4020 KB |
Output is correct |
22 |
Correct |
30 ms |
3908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1540 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
10 ms |
1492 KB |
Output is correct |
4 |
Correct |
10 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1364 KB |
Output is correct |
6 |
Correct |
2 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
2 ms |
1236 KB |
Output is correct |
9 |
Correct |
2 ms |
1364 KB |
Output is correct |
10 |
Correct |
42 ms |
1540 KB |
Output is correct |
11 |
Correct |
45 ms |
1492 KB |
Output is correct |
12 |
Correct |
38 ms |
1544 KB |
Output is correct |
13 |
Correct |
5 ms |
1496 KB |
Output is correct |
14 |
Correct |
6 ms |
1492 KB |
Output is correct |
15 |
Correct |
6 ms |
1544 KB |
Output is correct |
16 |
Correct |
7 ms |
1464 KB |
Output is correct |
17 |
Correct |
46 ms |
4472 KB |
Output is correct |
18 |
Correct |
47 ms |
4384 KB |
Output is correct |
19 |
Correct |
40 ms |
4584 KB |
Output is correct |
20 |
Correct |
8 ms |
1508 KB |
Output is correct |
21 |
Correct |
6 ms |
1492 KB |
Output is correct |
22 |
Correct |
2 ms |
1464 KB |
Output is correct |
23 |
Correct |
2 ms |
1492 KB |
Output is correct |
24 |
Correct |
1 ms |
1332 KB |
Output is correct |
25 |
Correct |
27 ms |
4172 KB |
Output is correct |
26 |
Correct |
26 ms |
4084 KB |
Output is correct |
27 |
Correct |
41 ms |
4440 KB |
Output is correct |
28 |
Correct |
48 ms |
4440 KB |
Output is correct |
29 |
Correct |
45 ms |
4604 KB |
Output is correct |
30 |
Correct |
32 ms |
4572 KB |
Output is correct |
31 |
Correct |
10 ms |
1472 KB |
Output is correct |
32 |
Correct |
2 ms |
1492 KB |
Output is correct |
33 |
Correct |
35 ms |
3428 KB |
Output is correct |
34 |
Correct |
2 ms |
1492 KB |
Output is correct |
35 |
Correct |
40 ms |
3972 KB |
Output is correct |
36 |
Correct |
1 ms |
1236 KB |
Output is correct |
37 |
Correct |
1 ms |
1336 KB |
Output is correct |
38 |
Correct |
40 ms |
3828 KB |
Output is correct |
39 |
Correct |
34 ms |
3788 KB |
Output is correct |
40 |
Correct |
33 ms |
3996 KB |
Output is correct |
41 |
Correct |
36 ms |
3948 KB |
Output is correct |
42 |
Correct |
35 ms |
4024 KB |
Output is correct |
43 |
Correct |
1 ms |
1324 KB |
Output is correct |
44 |
Correct |
1 ms |
1328 KB |
Output is correct |
45 |
Correct |
1 ms |
1236 KB |
Output is correct |
46 |
Correct |
1 ms |
1324 KB |
Output is correct |
47 |
Correct |
1 ms |
1236 KB |
Output is correct |
48 |
Correct |
1 ms |
1236 KB |
Output is correct |
49 |
Correct |
37 ms |
4080 KB |
Output is correct |
50 |
Correct |
36 ms |
3964 KB |
Output is correct |
51 |
Correct |
31 ms |
4020 KB |
Output is correct |
52 |
Correct |
30 ms |
3908 KB |
Output is correct |
53 |
Correct |
43 ms |
4400 KB |
Output is correct |
54 |
Correct |
47 ms |
4392 KB |
Output is correct |
55 |
Correct |
43 ms |
4372 KB |
Output is correct |
56 |
Correct |
8 ms |
1492 KB |
Output is correct |
57 |
Correct |
8 ms |
1548 KB |
Output is correct |
58 |
Correct |
124 ms |
3764 KB |
Output is correct |
59 |
Correct |
148 ms |
3660 KB |
Output is correct |
60 |
Correct |
181 ms |
3800 KB |
Output is correct |
61 |
Correct |
144 ms |
3756 KB |
Output is correct |
62 |
Correct |
137 ms |
3736 KB |
Output is correct |
63 |
Correct |
181 ms |
3736 KB |
Output is correct |
64 |
Correct |
108 ms |
3852 KB |
Output is correct |
65 |
Correct |
86 ms |
3896 KB |
Output is correct |
66 |
Correct |
103 ms |
3920 KB |
Output is correct |
67 |
Correct |
20 ms |
4004 KB |
Output is correct |
68 |
Correct |
28 ms |
4172 KB |
Output is correct |
69 |
Correct |
37 ms |
4104 KB |
Output is correct |
70 |
Correct |
41 ms |
4380 KB |
Output is correct |
71 |
Correct |
38 ms |
4404 KB |
Output is correct |
72 |
Correct |
33 ms |
4496 KB |
Output is correct |
73 |
Correct |
34 ms |
4496 KB |
Output is correct |
74 |
Correct |
37 ms |
4336 KB |
Output is correct |