#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define MP make_pair
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=2e2+10, maxm=5e4+10, lg=21, mod=1e9+7, inf=1e18;
ll n,m,V[maxm],U[maxm],C[maxm],ans[maxm];
bool mk[maxn];
void dij(ll sr,vector<ll> adj[],ll dis[],ll par[]){
for(int i=0;i<=n;i++) dis[i]=inf, par[i]=mk[i]=0;
dis[sr]=0;
while(1){
ll v=0;
for(int i=1;i<=n;i++)if(!mk[i] && dis[i]<dis[v]) v=i;
if(!v) return;
mk[v]=1;
for(auto i:adj[v]){
ll u=V[i]^U[i]^v;
if(dis[v]+C[i]<dis[u]) dis[u]=dis[v]+C[par[u]=i];
}
}
}
ll dis[3][maxn],py[3][maxn];
vector<ll> g[maxn],rg[maxn],gg[maxn];
bool dd[maxm];
void solve(ll a,ll b){
dij(a,g,dis[0],py[0]), dij(b,rg,dis[1],py[1]);
ans[0]+=dis[0][b];
memset(dd,0,sizeof(dd)); for(int i=1;i<=n;i++) dd[py[0][i]]=dd[py[1][i]]=1;
for(int i=1;i<=m;i++){
if(dis[0][U[i]]>=inf || dis[1][V[i]]>=inf){
ans[i]+=inf;
continue;
}
if(!dd[i]) ans[i]+=min(dis[0][b],dis[0][U[i]]+C[i]+dis[1][V[i]]);
else{
for(int i=1;i<=n;i++) gg[i].clear();
for(int j=1;j<=m;j++)if(i!=j) gg[V[j]].pb(j);
gg[U[i]].pb(i);
dij(a,gg,dis[2],py[2]);
ans[i]+=dis[2][b];
}
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//memset(ans,69,sizeof(ans));
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>V[i]>>U[i]>>C[i]>>ans[i];
g[V[i]].pb(i), rg[U[i]].pb(i);
}
solve(1,n), solve(n,1);
ll jj=inf;
for(int i=0;i<=m;i++) jj=min(jj,ans[i]);
cout<<(jj>=inf ? -1:jj);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
632 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
86 ms |
512 KB |
Output is correct |
4 |
Correct |
80 ms |
584 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
416 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
48 ms |
632 KB |
Output is correct |
11 |
Correct |
47 ms |
512 KB |
Output is correct |
12 |
Correct |
48 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
40 ms |
512 KB |
Output is correct |
15 |
Correct |
40 ms |
512 KB |
Output is correct |
16 |
Correct |
41 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
396 ms |
5040 KB |
Output is correct |
2 |
Correct |
397 ms |
4856 KB |
Output is correct |
3 |
Correct |
403 ms |
5240 KB |
Output is correct |
4 |
Correct |
85 ms |
640 KB |
Output is correct |
5 |
Correct |
60 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
24 ms |
4344 KB |
Output is correct |
10 |
Correct |
25 ms |
4352 KB |
Output is correct |
11 |
Correct |
214 ms |
4992 KB |
Output is correct |
12 |
Correct |
212 ms |
4984 KB |
Output is correct |
13 |
Correct |
228 ms |
5112 KB |
Output is correct |
14 |
Correct |
214 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
664 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
364 ms |
3832 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
442 ms |
4728 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
23 ms |
4088 KB |
Output is correct |
9 |
Correct |
25 ms |
4096 KB |
Output is correct |
10 |
Correct |
234 ms |
4736 KB |
Output is correct |
11 |
Correct |
238 ms |
4736 KB |
Output is correct |
12 |
Correct |
236 ms |
4856 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
242 ms |
4984 KB |
Output is correct |
20 |
Correct |
235 ms |
4728 KB |
Output is correct |
21 |
Correct |
236 ms |
4732 KB |
Output is correct |
22 |
Correct |
235 ms |
4736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
632 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
86 ms |
512 KB |
Output is correct |
4 |
Correct |
80 ms |
584 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
416 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
48 ms |
632 KB |
Output is correct |
11 |
Correct |
47 ms |
512 KB |
Output is correct |
12 |
Correct |
48 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
40 ms |
512 KB |
Output is correct |
15 |
Correct |
40 ms |
512 KB |
Output is correct |
16 |
Correct |
41 ms |
632 KB |
Output is correct |
17 |
Correct |
396 ms |
5040 KB |
Output is correct |
18 |
Correct |
397 ms |
4856 KB |
Output is correct |
19 |
Correct |
403 ms |
5240 KB |
Output is correct |
20 |
Correct |
85 ms |
640 KB |
Output is correct |
21 |
Correct |
60 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
24 ms |
4344 KB |
Output is correct |
26 |
Correct |
25 ms |
4352 KB |
Output is correct |
27 |
Correct |
214 ms |
4992 KB |
Output is correct |
28 |
Correct |
212 ms |
4984 KB |
Output is correct |
29 |
Correct |
228 ms |
5112 KB |
Output is correct |
30 |
Correct |
214 ms |
5368 KB |
Output is correct |
31 |
Correct |
78 ms |
664 KB |
Output is correct |
32 |
Correct |
7 ms |
512 KB |
Output is correct |
33 |
Correct |
364 ms |
3832 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
442 ms |
4728 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
23 ms |
4088 KB |
Output is correct |
39 |
Correct |
25 ms |
4096 KB |
Output is correct |
40 |
Correct |
234 ms |
4736 KB |
Output is correct |
41 |
Correct |
238 ms |
4736 KB |
Output is correct |
42 |
Correct |
236 ms |
4856 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
242 ms |
4984 KB |
Output is correct |
50 |
Correct |
235 ms |
4728 KB |
Output is correct |
51 |
Correct |
236 ms |
4732 KB |
Output is correct |
52 |
Correct |
235 ms |
4736 KB |
Output is correct |
53 |
Correct |
414 ms |
4768 KB |
Output is correct |
54 |
Correct |
409 ms |
4856 KB |
Output is correct |
55 |
Correct |
408 ms |
4860 KB |
Output is correct |
56 |
Correct |
82 ms |
512 KB |
Output is correct |
57 |
Correct |
82 ms |
512 KB |
Output is correct |
58 |
Correct |
220 ms |
3960 KB |
Output is correct |
59 |
Correct |
210 ms |
3840 KB |
Output is correct |
60 |
Correct |
211 ms |
3960 KB |
Output is correct |
61 |
Correct |
213 ms |
3960 KB |
Output is correct |
62 |
Correct |
209 ms |
3840 KB |
Output is correct |
63 |
Correct |
207 ms |
3964 KB |
Output is correct |
64 |
Correct |
299 ms |
3960 KB |
Output is correct |
65 |
Correct |
292 ms |
4088 KB |
Output is correct |
66 |
Correct |
273 ms |
3968 KB |
Output is correct |
67 |
Correct |
19 ms |
3956 KB |
Output is correct |
68 |
Correct |
24 ms |
4344 KB |
Output is correct |
69 |
Correct |
25 ms |
4344 KB |
Output is correct |
70 |
Correct |
213 ms |
4968 KB |
Output is correct |
71 |
Correct |
211 ms |
4856 KB |
Output is correct |
72 |
Correct |
212 ms |
4856 KB |
Output is correct |
73 |
Correct |
213 ms |
5244 KB |
Output is correct |
74 |
Correct |
222 ms |
5176 KB |
Output is correct |