#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=205;
const int M=50050;
const int inf=2e9+7;
struct Edge{int u,v,c,d;};
Edge edges[M];
bool bridge[M];
struct Bridges{
static const int NSZ=205;
vector<pair<int,int>> E[NSZ];
vector<int> ans;
int n,disc[NSZ],low[NSZ],tid;
void init(int _n){n=_n;tid=0;ans.clear();for(int i=1;i<=n;i++)E[i].clear(),disc[i]=low[i]=0;}
void AddEdge(int u,int v,int e){E[u].pb({v,e});E[v].pb({u,e});}
void DFS(int u,int p){
disc[u]=low[u]=++tid;
for(auto e:E[u])if(e.second!=p){
int v=e.first;
if(!disc[v]){
DFS(v,e.second);
if(low[v]>disc[u])ans.pb(e.second);
low[u]=min(low[u],low[v]);
}else low[u]=min(low[u],disc[v]);
}
}
}GPH;
void FindBridges(vector<pair<int,int>> go[],int st,int n){
GPH.init(n);
vector<bool> was(n+1,0);
function<void(int)> CNS=[&](int u){
was[u]=1;
for(auto e:go[u]){
GPH.AddEdge(u,e.first,e.second);
if(!was[e.first])CNS(e.first);
}
};
CNS(st);
GPH.DFS(st,-1);
//printf(":D\n");
for(int i:GPH.ans)bridge[i]=1;
}
struct DijkstraPQ{
static const int N=205;
int a[N],idx[N],val[N],n;
void init(int _n,int st,int inf){
n=_n;
for(int i=1;i<=n;i++)a[i]=i,val[i]=inf;
swap(a[st],a[1]);
for(int i=1;i<=n;i++)idx[a[i]]=i;
val[st]=0;
}
int size(){return n;}
void Change(int x,int f){
val[x]=f;
int p=idx[x];
while(p>1 && f<val[a[p/2]])a[p]=a[p/2],idx[a[p]]=p,p/=2;
a[p]=x;idx[x]=p;
}
int top(){return a[1];}
void pop(){
int now=1;
a[1]=a[n];n--;
idx[a[1]]=1;
while(now*2<=n){
int ch=now*2;
if(ch+1<=n && val[a[ch+1]]<val[a[ch]])ch++;
if(val[a[now]]<=val[a[ch]])break;
swap(a[now],a[ch]);
idx[a[now]]=now;
idx[a[ch]]=ch;
now=ch;
}
}
};
void Dijkstra(vector<pair<int,int>> E[],int n,int st,int dist[],vector<pair<int,int>> go[]){
for(int i=1;i<=n;i++)dist[i]=inf,go[i].clear();
DijkstraPQ pq;pq.init(n,st,inf);
dist[st]=0;
while(pq.size()){
int u=pq.top();
pq.pop();
for(auto e:E[u]){
int v=e.first,w=edges[e.second].c;
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
go[v].clear();
//pq.push({-dist[v],v});
pq.Change(v,dist[v]);
}
if(dist[v]==dist[u]+w)go[v].pb({u,e.second});
}
}
}
vector<pair<int,int>> E[N],R[N],T[N],go[N];
void BuildRev(int j,int m,int n){
for(int i=1;i<=n;i++)T[i].clear();
for(int i=1;i<=m;i++){
if(i!=j)T[edges[i].u].pb({edges[i].v,i});
else T[edges[i].v].pb({edges[i].u,i});
}
}
int dist[2][2][N],tmp[N];
int main(){
int n,m;
scanf("%i %i",&n,&m);
for(int i=1;i<=m;i++){
scanf("%i %i %i %i",&edges[i].u,&edges[i].v,&edges[i].c,&edges[i].d);
E[edges[i].u].pb({edges[i].v,i});
R[edges[i].v].pb({edges[i].u,i});
}
Dijkstra(E,n,1,dist[0][0],go);
FindBridges(go,n,n);
Dijkstra(R,n,n,dist[0][1],go);
Dijkstra(E,n,n,dist[1][0],go);
FindBridges(go,1,n);
Dijkstra(R,n,1,dist[1][1],go);
ll ans=(ll)dist[0][0][n]+dist[1][0][1];
for(int i=1;i<=m;i++){
if(bridge[i]){
BuildRev(i,m,n);
Dijkstra(T,n,1,tmp,go);
ll now=tmp[n];
if(now==inf)continue;
//printf("%i ",tmp[n]);
Dijkstra(T,n,n,tmp,go);
now+=tmp[1];
//printf("%i ",tmp[1]);
now+=edges[i].d;
ans=min(ans,now);
//printf("%i %lld\n",i,now);
}else{
int u=edges[i].u,v=edges[i].v,c=edges[i].c,d=edges[i].d;
ll d1=dist[0][0][n];
if(dist[0][0][v]<dist[0][0][u])d1=min(d1,(ll)dist[0][0][v]+c+dist[0][1][u]);
ll d2=dist[1][0][1];
if(dist[1][0][v]<dist[1][0][u])d2=min(d2,(ll)dist[1][0][v]+c+dist[1][1][u]);
ans=min(ans,d1+d2+d);
}
}
if(ans<inf)printf("%lld\n",ans);
else printf("-1\n");
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&m);
~~~~~^~~~~~~~~~~~~~~
ho_t4.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i %i",&edges[i].u,&edges[i].v,&edges[i].c,&edges[i].d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
7 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
22 ms |
504 KB |
Output is correct |
11 |
Correct |
25 ms |
504 KB |
Output is correct |
12 |
Correct |
25 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
2424 KB |
Output is correct |
2 |
Correct |
31 ms |
2424 KB |
Output is correct |
3 |
Correct |
33 ms |
2424 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
33 ms |
2424 KB |
Output is correct |
10 |
Correct |
33 ms |
2424 KB |
Output is correct |
11 |
Correct |
33 ms |
2424 KB |
Output is correct |
12 |
Correct |
33 ms |
2424 KB |
Output is correct |
13 |
Correct |
33 ms |
2424 KB |
Output is correct |
14 |
Correct |
33 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
380 KB |
Output is correct |
3 |
Correct |
30 ms |
3192 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
36 ms |
4088 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
34 ms |
3188 KB |
Output is correct |
9 |
Correct |
35 ms |
3192 KB |
Output is correct |
10 |
Correct |
36 ms |
4600 KB |
Output is correct |
11 |
Correct |
36 ms |
4604 KB |
Output is correct |
12 |
Correct |
38 ms |
4856 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
35 ms |
4344 KB |
Output is correct |
20 |
Correct |
35 ms |
4088 KB |
Output is correct |
21 |
Correct |
34 ms |
4216 KB |
Output is correct |
22 |
Correct |
36 ms |
4088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
7 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
22 ms |
504 KB |
Output is correct |
11 |
Correct |
25 ms |
504 KB |
Output is correct |
12 |
Correct |
25 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
30 ms |
2424 KB |
Output is correct |
18 |
Correct |
31 ms |
2424 KB |
Output is correct |
19 |
Correct |
33 ms |
2424 KB |
Output is correct |
20 |
Correct |
6 ms |
504 KB |
Output is correct |
21 |
Correct |
6 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
33 ms |
2424 KB |
Output is correct |
26 |
Correct |
33 ms |
2424 KB |
Output is correct |
27 |
Correct |
33 ms |
2424 KB |
Output is correct |
28 |
Correct |
33 ms |
2424 KB |
Output is correct |
29 |
Correct |
33 ms |
2424 KB |
Output is correct |
30 |
Correct |
33 ms |
2552 KB |
Output is correct |
31 |
Correct |
6 ms |
504 KB |
Output is correct |
32 |
Correct |
5 ms |
380 KB |
Output is correct |
33 |
Correct |
30 ms |
3192 KB |
Output is correct |
34 |
Correct |
5 ms |
380 KB |
Output is correct |
35 |
Correct |
36 ms |
4088 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
34 ms |
3188 KB |
Output is correct |
39 |
Correct |
35 ms |
3192 KB |
Output is correct |
40 |
Correct |
36 ms |
4600 KB |
Output is correct |
41 |
Correct |
36 ms |
4604 KB |
Output is correct |
42 |
Correct |
38 ms |
4856 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
35 ms |
4344 KB |
Output is correct |
50 |
Correct |
35 ms |
4088 KB |
Output is correct |
51 |
Correct |
34 ms |
4216 KB |
Output is correct |
52 |
Correct |
36 ms |
4088 KB |
Output is correct |
53 |
Correct |
42 ms |
2936 KB |
Output is correct |
54 |
Correct |
44 ms |
3064 KB |
Output is correct |
55 |
Correct |
41 ms |
2936 KB |
Output is correct |
56 |
Correct |
6 ms |
376 KB |
Output is correct |
57 |
Correct |
6 ms |
376 KB |
Output is correct |
58 |
Correct |
171 ms |
2296 KB |
Output is correct |
59 |
Correct |
200 ms |
2632 KB |
Output is correct |
60 |
Correct |
262 ms |
2300 KB |
Output is correct |
61 |
Correct |
170 ms |
2296 KB |
Output is correct |
62 |
Correct |
196 ms |
2552 KB |
Output is correct |
63 |
Correct |
256 ms |
2424 KB |
Output is correct |
64 |
Correct |
281 ms |
2424 KB |
Output is correct |
65 |
Correct |
304 ms |
2552 KB |
Output is correct |
66 |
Correct |
396 ms |
2424 KB |
Output is correct |
67 |
Correct |
26 ms |
2408 KB |
Output is correct |
68 |
Correct |
35 ms |
2424 KB |
Output is correct |
69 |
Correct |
33 ms |
2428 KB |
Output is correct |
70 |
Correct |
43 ms |
2936 KB |
Output is correct |
71 |
Correct |
41 ms |
2936 KB |
Output is correct |
72 |
Correct |
38 ms |
2936 KB |
Output is correct |
73 |
Correct |
40 ms |
3068 KB |
Output is correct |
74 |
Correct |
42 ms |
2936 KB |
Output is correct |