#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define emp emplace
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll)x.size()
#define f first
#define s second
const ll MAXN = 210;
const ll MAXE = 50100;
const ll INF = 1e15;
const ll MOD = 1e9+7;
typedef pair<pi,pi> pp;
typedef pair<ll,pi> pip;
vector<vector<pip>> V,O,S;
vi f1,fn,b1,bn;
ll N,E,a,b,w,r;
ll U[MAXE];
ll l[MAXN];
vector<pp> Edge;
priority_queue<pi,vpi,greater<pi>>pq;
ll BLOCK=-1;
void dijkstra(ll x, vector<vector<pip>> &A, vi &D){
D.resize(N+1);
vi vis;vis.resize(N+1,0);
for(ll i=1;i<=N;++i)D[i]=INF;
D[x]=0;
pq.emp(0,x);
while(1){
ll n=-1; ll d=INF;
for (int i=1;i<=N;++i)if(!vis[i]&&D[i]<d){
n=i;
d=D[i];
}
if(n==-1)break;
vis[n]=1;
if(n!=x&&BLOCK==-1)U[l[n]]=1;
for(auto t:A[n]){
ll c=t.f;ll nd=d+t.s.f;
if(nd>=D[c])continue;
if(t.s.s==BLOCK)continue;
D[c]=nd;
l[c]=t.s.s;
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>N>>E;
V.resize(N+1);O.resize(N+1);S.resize(N+1);
for (ll i=0;i<E;++i){
cin>>a>>b>>w>>r;
V[a].pb(b, mp(w,i));
O[b].pb(a, mp(w,i));
Edge.pb(mp(a,b),mp(w,r));
}
dijkstra(1,V,f1);
dijkstra(N,V,fn);
dijkstra(1,O,b1);
dijkstra(N,O,bn);
// for (ll i=0;i<E;++i)cout<<U[i]<<' ';cout<<'\n';
ll ans=f1[N]+fn[1];
for(ll i=0;i<E;++i){
if(U[i])continue;
ll a=Edge[i].f.f;ll b=Edge[i].f.s;ll w=Edge[i].s.f;
ll forward=min(f1[N],f1[b]+w+bn[a]);
ll back=min(fn[1],fn[b]+w+b1[a]);
ll tw=Edge[i].s.s+forward+back;
ans=min(ans,tw);
}
for (ll i=0;i<E;++i){
// cout<<U[i]<<'\n';
if(!U[i])continue;
ll a=Edge[i].f.f;ll b=Edge[i].f.s;ll w=Edge[i].s.f;
BLOCK=i;
vi d1,d2;
V[b].pb(a,mp(w,-INF));
dijkstra(1,V,d1);dijkstra(N,V,d2);
ll tw=Edge[i].s.s+d1[N]+d2[1];
// cout<<tw<<'\n';
ans=min(ans,tw);
V[b].pop_back();
// for (int i=1;i<=N;++i)for(auto x:V[i])cout<<x.f<<' ';
// cout<<'\n';
}
if(ans>=INF)cout<<-1;
else cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
112 ms |
540 KB |
Output is correct |
4 |
Correct |
114 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
57 ms |
548 KB |
Output is correct |
11 |
Correct |
76 ms |
644 KB |
Output is correct |
12 |
Correct |
70 ms |
512 KB |
Output is correct |
13 |
Correct |
36 ms |
512 KB |
Output is correct |
14 |
Correct |
77 ms |
512 KB |
Output is correct |
15 |
Correct |
75 ms |
512 KB |
Output is correct |
16 |
Correct |
69 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
5988 KB |
Output is correct |
2 |
Correct |
221 ms |
6108 KB |
Output is correct |
3 |
Correct |
212 ms |
5968 KB |
Output is correct |
4 |
Correct |
105 ms |
640 KB |
Output is correct |
5 |
Correct |
80 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
84 ms |
6024 KB |
Output is correct |
10 |
Correct |
79 ms |
6000 KB |
Output is correct |
11 |
Correct |
149 ms |
6000 KB |
Output is correct |
12 |
Correct |
146 ms |
6244 KB |
Output is correct |
13 |
Correct |
141 ms |
6132 KB |
Output is correct |
14 |
Correct |
129 ms |
6388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
512 KB |
Output is correct |
2 |
Correct |
12 ms |
384 KB |
Output is correct |
3 |
Correct |
169 ms |
5108 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
200 ms |
6120 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
83 ms |
5992 KB |
Output is correct |
9 |
Correct |
88 ms |
5992 KB |
Output is correct |
10 |
Correct |
144 ms |
6132 KB |
Output is correct |
11 |
Correct |
138 ms |
6120 KB |
Output is correct |
12 |
Correct |
151 ms |
6248 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
148 ms |
6376 KB |
Output is correct |
20 |
Correct |
139 ms |
6244 KB |
Output is correct |
21 |
Correct |
139 ms |
6120 KB |
Output is correct |
22 |
Correct |
155 ms |
5996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
112 ms |
540 KB |
Output is correct |
4 |
Correct |
114 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
57 ms |
548 KB |
Output is correct |
11 |
Correct |
76 ms |
644 KB |
Output is correct |
12 |
Correct |
70 ms |
512 KB |
Output is correct |
13 |
Correct |
36 ms |
512 KB |
Output is correct |
14 |
Correct |
77 ms |
512 KB |
Output is correct |
15 |
Correct |
75 ms |
512 KB |
Output is correct |
16 |
Correct |
69 ms |
512 KB |
Output is correct |
17 |
Correct |
205 ms |
5988 KB |
Output is correct |
18 |
Correct |
221 ms |
6108 KB |
Output is correct |
19 |
Correct |
212 ms |
5968 KB |
Output is correct |
20 |
Correct |
105 ms |
640 KB |
Output is correct |
21 |
Correct |
80 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
84 ms |
6024 KB |
Output is correct |
26 |
Correct |
79 ms |
6000 KB |
Output is correct |
27 |
Correct |
149 ms |
6000 KB |
Output is correct |
28 |
Correct |
146 ms |
6244 KB |
Output is correct |
29 |
Correct |
141 ms |
6132 KB |
Output is correct |
30 |
Correct |
129 ms |
6388 KB |
Output is correct |
31 |
Correct |
59 ms |
512 KB |
Output is correct |
32 |
Correct |
12 ms |
384 KB |
Output is correct |
33 |
Correct |
169 ms |
5108 KB |
Output is correct |
34 |
Correct |
8 ms |
384 KB |
Output is correct |
35 |
Correct |
200 ms |
6120 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
83 ms |
5992 KB |
Output is correct |
39 |
Correct |
88 ms |
5992 KB |
Output is correct |
40 |
Correct |
144 ms |
6132 KB |
Output is correct |
41 |
Correct |
138 ms |
6120 KB |
Output is correct |
42 |
Correct |
151 ms |
6248 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
384 KB |
Output is correct |
45 |
Correct |
4 ms |
384 KB |
Output is correct |
46 |
Correct |
4 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
148 ms |
6376 KB |
Output is correct |
50 |
Correct |
139 ms |
6244 KB |
Output is correct |
51 |
Correct |
139 ms |
6120 KB |
Output is correct |
52 |
Correct |
155 ms |
5996 KB |
Output is correct |
53 |
Correct |
206 ms |
5872 KB |
Output is correct |
54 |
Correct |
213 ms |
5992 KB |
Output is correct |
55 |
Correct |
196 ms |
5844 KB |
Output is correct |
56 |
Correct |
115 ms |
512 KB |
Output is correct |
57 |
Correct |
108 ms |
640 KB |
Output is correct |
58 |
Correct |
98 ms |
5108 KB |
Output is correct |
59 |
Correct |
107 ms |
4980 KB |
Output is correct |
60 |
Correct |
140 ms |
5112 KB |
Output is correct |
61 |
Correct |
95 ms |
5112 KB |
Output is correct |
62 |
Correct |
107 ms |
4980 KB |
Output is correct |
63 |
Correct |
139 ms |
5112 KB |
Output is correct |
64 |
Correct |
151 ms |
4972 KB |
Output is correct |
65 |
Correct |
151 ms |
4984 KB |
Output is correct |
66 |
Correct |
190 ms |
4976 KB |
Output is correct |
67 |
Correct |
27 ms |
4028 KB |
Output is correct |
68 |
Correct |
82 ms |
5868 KB |
Output is correct |
69 |
Correct |
79 ms |
5992 KB |
Output is correct |
70 |
Correct |
155 ms |
6120 KB |
Output is correct |
71 |
Correct |
148 ms |
5748 KB |
Output is correct |
72 |
Correct |
149 ms |
6120 KB |
Output is correct |
73 |
Correct |
150 ms |
5864 KB |
Output is correct |
74 |
Correct |
149 ms |
5996 KB |
Output is correct |