#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
#define pb push_back
const int MN = 202, MM = 5e4+5;
int N, M, i, j, x, y, w[MM], c[MM], u[MM], vs[MN], ban[MM], par[MN]; ll ans, dis[MN], a[MM][MN], b[MM][MN], cost[MM];
vector<pii> adj[MN], rev[MN];
vi tree; pii ed[MM];
queue<int> q; int st[MN];
void dij(int src,bool dir){
q.push(src);
memset(par,-1,sizeof(par));
for(i=1;i<=N;i++)
dis[i]=1LL<<55;
dis[src]=0;
while(q.size()){
int cur=q.front(); q.pop();
if(vs[cur]) continue;
st[cur]=0;
if(dir==1){
for(auto v : adj[cur]){
if(ban[v.second]) continue;
if(dis[cur]+w[v.second]<dis[v.first]){
dis[v.first]=dis[cur]+w[v.second];
par[v.first]=v.second;
if(!st[v.first]){
st[v.first]=1;
q.push(v.first);
}
}
}
}
else{
for(auto v : rev[cur]){
if(ban[v.second]) continue;
if(dis[cur]+w[v.second]<dis[v.first]){
dis[v.first]=dis[cur]+w[v.second];
par[v.first]=v.second;
if(!st[v.first]){
st[v.first]=1;
q.push(v.first);
}
}
}
}
}
for(i=1;i<=N;i++){
if(par[i]!=-1) u[par[i]]=1;
}
}
int main(){
mt19937 mt(time(0));
for(scanf("%d%d",&N,&M),i=1;i<=M;i++){
scanf("%d%d%d%d",&x,&y,&w[i],&c[i]);
ed[i]={x,y};
adj[x].pb({y,i});
rev[y].pb({x,i});
}
for(i=1;i<=N;i++){
shuffle(adj[i].begin(),adj[i].end(),mt);
shuffle(rev[i].begin(),rev[i].end(),mt);
}
for(int k=0;k<2;k++){
memset(u,0,sizeof(u));
dij(1,1^k);
tree.clear();
for(i=1;i<=M;i++){
if(u[i]){
tree.pb(i);
x=ed[i].first,y=ed[i].second;
for(j=0;j<adj[x].size()&&adj[x][j].first!=y;j++){}
adj[x].erase(adj[x].begin()+j);
adj[x].insert(adj[x].begin(),{y,i});
for(j=0;j<rev[y].size()&&rev[y][j].first!=x;j++){}
rev[y].erase(rev[y].begin()+j);
rev[y].insert(rev[y].begin(),{x,i});
}
else{
for(j=1;j<=N;j++)
a[i][j]=dis[j];
}
}
assert(tree.size()<=N);
for(auto v : tree){
ban[v]=1;
dij(1,1^k);
for(j=1;j<=N;j++)
a[v][j]=dis[j];
ban[v]=0;
}
memset(u,0,sizeof(u));
dij(N,k);
tree.clear();
for(i=1;i<=M;i++){
if(u[i]){
tree.pb(i);
x=ed[i].first,y=ed[i].second;
for(j=0;j<adj[x].size()&&adj[x][j].first!=y;j++){}
adj[x].erase(adj[x].begin()+j);
adj[x].insert(adj[x].begin(),{y,i});
for(j=0;j<rev[y].size()&&rev[y][j].first!=x;j++){}
rev[y].erase(rev[y].begin()+j);
rev[y].insert(rev[y].begin(),{x,i});
}
else{
for(j=1;j<=N;j++)
b[i][j]=dis[j];
}
}
assert(tree.size()<=N);
for(auto v : tree){
ban[v]=1;
dij(N,k);
for(j=1;j<=N;j++)
b[v][j]=dis[j];
ban[v]=0;
}
for(j=1;j<=M;j++){
if(!k) cost[j]+=min(a[j][N],a[j][ed[j].second]+w[j]+b[j][ed[j].first]);
else cost[j]+=min(a[j][N],a[j][ed[j].first]+w[j]+b[j][ed[j].second]);
}
}
dij(1,1);
ans+=dis[N];
dij(N,1);
ans+=dis[1];
for(j=1;j<=M;j++)
ans=min(ans,cost[j]+c[j]);
if(ans>(1LL<<50)) printf("-1\n");
else printf("%lld\n",ans);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:79:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<adj[x].size()&&adj[x][j].first!=y;j++){}
~^~~~~~~~~~~~~~
ho_t4.cpp:82:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<rev[y].size()&&rev[y][j].first!=x;j++){}
~^~~~~~~~~~~~~~
In file included from /usr/include/c++/7/cassert:44:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
from ho_t4.cpp:1:
ho_t4.cpp:91:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
assert(tree.size()<=N);
~~~~~~~~~~~^~~
ho_t4.cpp:106:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<adj[x].size()&&adj[x][j].first!=y;j++){}
~^~~~~~~~~~~~~~
ho_t4.cpp:109:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<rev[y].size()&&rev[y][j].first!=x;j++){}
~^~~~~~~~~~~~~~
In file included from /usr/include/c++/7/cassert:44:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
from ho_t4.cpp:1:
ho_t4.cpp:118:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
assert(tree.size()<=N);
~~~~~~~~~~~^~~
ho_t4.cpp:61:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(scanf("%d%d",&N,&M),i=1;i<=M;i++){
~~~~~~~~~~~~~~~~~~~^~~~
ho_t4.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d",&x,&y,&w[i],&c[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
3840 KB |
Output is correct |
2 |
Correct |
6 ms |
1408 KB |
Output is correct |
3 |
Correct |
15 ms |
3712 KB |
Output is correct |
4 |
Correct |
15 ms |
3840 KB |
Output is correct |
5 |
Correct |
7 ms |
3840 KB |
Output is correct |
6 |
Correct |
5 ms |
1408 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
7 ms |
3840 KB |
Output is correct |
10 |
Correct |
40 ms |
3840 KB |
Output is correct |
11 |
Correct |
39 ms |
3840 KB |
Output is correct |
12 |
Correct |
34 ms |
3840 KB |
Output is correct |
13 |
Correct |
9 ms |
3840 KB |
Output is correct |
14 |
Correct |
12 ms |
3840 KB |
Output is correct |
15 |
Correct |
12 ms |
3840 KB |
Output is correct |
16 |
Correct |
12 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
161336 KB |
Output is correct |
2 |
Correct |
349 ms |
161400 KB |
Output is correct |
3 |
Correct |
358 ms |
161272 KB |
Output is correct |
4 |
Correct |
21 ms |
7040 KB |
Output is correct |
5 |
Correct |
12 ms |
3840 KB |
Output is correct |
6 |
Correct |
7 ms |
2560 KB |
Output is correct |
7 |
Correct |
6 ms |
1792 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
162 ms |
161300 KB |
Output is correct |
10 |
Correct |
162 ms |
161272 KB |
Output is correct |
11 |
Correct |
293 ms |
161272 KB |
Output is correct |
12 |
Correct |
257 ms |
161400 KB |
Output is correct |
13 |
Correct |
264 ms |
161484 KB |
Output is correct |
14 |
Correct |
306 ms |
161528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
3840 KB |
Output is correct |
2 |
Correct |
7 ms |
1408 KB |
Output is correct |
3 |
Correct |
177 ms |
125816 KB |
Output is correct |
4 |
Correct |
6 ms |
1408 KB |
Output is correct |
5 |
Correct |
223 ms |
161272 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
145 ms |
161272 KB |
Output is correct |
9 |
Correct |
145 ms |
161272 KB |
Output is correct |
10 |
Correct |
199 ms |
161272 KB |
Output is correct |
11 |
Correct |
192 ms |
161400 KB |
Output is correct |
12 |
Correct |
191 ms |
161528 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
191 ms |
161308 KB |
Output is correct |
20 |
Correct |
189 ms |
161272 KB |
Output is correct |
21 |
Correct |
189 ms |
161328 KB |
Output is correct |
22 |
Correct |
192 ms |
161400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
3840 KB |
Output is correct |
2 |
Correct |
6 ms |
1408 KB |
Output is correct |
3 |
Correct |
15 ms |
3712 KB |
Output is correct |
4 |
Correct |
15 ms |
3840 KB |
Output is correct |
5 |
Correct |
7 ms |
3840 KB |
Output is correct |
6 |
Correct |
5 ms |
1408 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
7 ms |
3840 KB |
Output is correct |
10 |
Correct |
40 ms |
3840 KB |
Output is correct |
11 |
Correct |
39 ms |
3840 KB |
Output is correct |
12 |
Correct |
34 ms |
3840 KB |
Output is correct |
13 |
Correct |
9 ms |
3840 KB |
Output is correct |
14 |
Correct |
12 ms |
3840 KB |
Output is correct |
15 |
Correct |
12 ms |
3840 KB |
Output is correct |
16 |
Correct |
12 ms |
3840 KB |
Output is correct |
17 |
Correct |
371 ms |
161336 KB |
Output is correct |
18 |
Correct |
349 ms |
161400 KB |
Output is correct |
19 |
Correct |
358 ms |
161272 KB |
Output is correct |
20 |
Correct |
21 ms |
7040 KB |
Output is correct |
21 |
Correct |
12 ms |
3840 KB |
Output is correct |
22 |
Correct |
7 ms |
2560 KB |
Output is correct |
23 |
Correct |
6 ms |
1792 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
162 ms |
161300 KB |
Output is correct |
26 |
Correct |
162 ms |
161272 KB |
Output is correct |
27 |
Correct |
293 ms |
161272 KB |
Output is correct |
28 |
Correct |
257 ms |
161400 KB |
Output is correct |
29 |
Correct |
264 ms |
161484 KB |
Output is correct |
30 |
Correct |
306 ms |
161528 KB |
Output is correct |
31 |
Correct |
12 ms |
3840 KB |
Output is correct |
32 |
Correct |
7 ms |
1408 KB |
Output is correct |
33 |
Correct |
177 ms |
125816 KB |
Output is correct |
34 |
Correct |
6 ms |
1408 KB |
Output is correct |
35 |
Correct |
223 ms |
161272 KB |
Output is correct |
36 |
Correct |
5 ms |
512 KB |
Output is correct |
37 |
Correct |
5 ms |
512 KB |
Output is correct |
38 |
Correct |
145 ms |
161272 KB |
Output is correct |
39 |
Correct |
145 ms |
161272 KB |
Output is correct |
40 |
Correct |
199 ms |
161272 KB |
Output is correct |
41 |
Correct |
192 ms |
161400 KB |
Output is correct |
42 |
Correct |
191 ms |
161528 KB |
Output is correct |
43 |
Correct |
5 ms |
512 KB |
Output is correct |
44 |
Correct |
5 ms |
512 KB |
Output is correct |
45 |
Correct |
5 ms |
512 KB |
Output is correct |
46 |
Correct |
5 ms |
512 KB |
Output is correct |
47 |
Correct |
5 ms |
640 KB |
Output is correct |
48 |
Correct |
5 ms |
512 KB |
Output is correct |
49 |
Correct |
191 ms |
161308 KB |
Output is correct |
50 |
Correct |
189 ms |
161272 KB |
Output is correct |
51 |
Correct |
189 ms |
161328 KB |
Output is correct |
52 |
Correct |
192 ms |
161400 KB |
Output is correct |
53 |
Correct |
381 ms |
161272 KB |
Output is correct |
54 |
Correct |
337 ms |
161400 KB |
Output is correct |
55 |
Correct |
354 ms |
161272 KB |
Output is correct |
56 |
Correct |
15 ms |
3840 KB |
Output is correct |
57 |
Correct |
17 ms |
3840 KB |
Output is correct |
58 |
Correct |
574 ms |
129144 KB |
Output is correct |
59 |
Correct |
609 ms |
129144 KB |
Output is correct |
60 |
Correct |
665 ms |
129148 KB |
Output is correct |
61 |
Correct |
504 ms |
129144 KB |
Output is correct |
62 |
Correct |
610 ms |
129016 KB |
Output is correct |
63 |
Correct |
618 ms |
129016 KB |
Output is correct |
64 |
Execution timed out |
1095 ms |
129016 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |