#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
set<int> S;
struct Graph
{
struct edge
{
int v; ll weight; int id;
};
vector<vector<edge> > adj;
int n;
Graph(int _n)
{
adj.resize(_n);
n = _n;
}
void addedge(int u, int v, ll c, int id)
{
edge tmp;
tmp.v = v; tmp.weight = c; tmp.id=id;
adj[u].pb(tmp);
}
void reset()
{
adj.clear();
}
vector<ll> dist;
vi par;
void bfs(int s)
{
ll INFI = ll(1e18);
dist.assign(n, INFI);
par.assign(n, -1);
dist[s] = 0; par[s] = -1;
queue<int> q; q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = 0; i < adj[u].size(); i++)
{
int v = adj[u][i].v;
if(dist[v] >= INFI)
{
dist[v] = dist[u] + 1;
par[v] = u;
q.push(v);
}
}
}
}
void bfs01(int s)
{
ll INFI = ll(1e18);
dist.assign(n, INFI);
par.assign(n, -1);
dist[s] = 0; par[s] = -1;
deque<int> q; q.pb(s);
while(!q.empty())
{
int u = q.front(); q.pop_front();
for(int i = 0; i < adj[u].size(); i++)
{
int v = adj[u][i].v; ll w = adj[u][i].weight;
if(dist[v] >= INFI)
{
if(w == 1)
{
dist[v] = dist[u] + 1;
par[v] = u;
q.push_back(v);
}
else
{
dist[v] = dist[u];
par[v] = u;
q.push_front(v);
}
}
}
}
}
void dijkstra(int s, int mode=1)
{
ll INFI = ll(1e18);
dist.clear();
dist.assign(n, INFI);
par.assign(n, -1);
dist[s] = 0; par[s] = -1;
priority_queue<ii, vector<ii>, greater<ii> > pq;
pq.push(ii(0, s));
while(!pq.empty())
{
int u = pq.top().se; ll d = pq.top().fi; pq.pop();
for(int i = 0; i < adj[u].size(); i++)
{
int v = adj[u][i].v; ll w = adj[u][i].weight;
if(d + w < dist[v])
{
dist[v] = d + w;
par[v] = u;
//cerr<<"ADD "<<adj[u][i].id<<'\n';
//if(mode) S.insert(adj[u][i].id);
pq.push(ii(dist[v], v));
}
}
}
}
vector<vector<ll> > d;
void Floyd()
{
ll INFIN = ll(1e18);
d.resize(n);
for(int i = 0; i < n; i++)
{
d[i].assign(n, INFIN);
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < adj[i].size(); j++)
{
d[i][adj[i][j].v] = min(d[i][adj[i][j].v],adj[i][j].weight);
}
d[i][i] = 0;
}
for(int k = 0; k < n; k++)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
};
ll D[55555];
vector<pair<ii,ii> > edges;
int n,m;
ll res=ll(1e18);
void test(int id)
{
Graph G(n);
for(int i=0;i<m;i++)
{
int u=edges[i].fi.fi; int v=edges[i].fi.se;
if(i==id) swap(u,v);
G.addedge(u,v,edges[i].se.fi,edges[i].se.se);
}
G.dijkstra(0,0);
ll ans = G.dist[n-1];
G.dijkstra(n-1,0);
ans+=G.dist[0];
ans+=D[id];
res=min(res,ans);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("olympic-bus.in","r",stdin);
cin>>n>>m;
Graph ori(n),oriunw(n),oriunw2(n);
vector<ii> VV;
int all0=1;
map<ii,ii> ma;
for(int i=0;i<m;i++)
{
int u,v,c,d;
cin>>u>>v>>c>>d;
if(c!=0) all0=0;
VV.pb({d,i});
u--; v--;
if(ma.find({u,v})==ma.end())
{
ma[{u,v}]={c+d,i};
}
else
{
ma[{u,v}]=min(ma[{u,v}],{c+d,i});
}
D[i]=d;
edges.pb({{u,v},{c,d}});
ori.addedge(u,v,c,i);
oriunw.addedge(u,v,c,i);
oriunw.addedge(v,u,c+d,i);
oriunw2.addedge(v,u,c,i);
oriunw2.addedge(u,v,c+d,i);
}
int sub2=1;
if(m%2==0)
{
for(int i=0;i<m;i+=2)
{
int a1 = edges[i].se.se;
int a2 = edges[i+1].se.se;
edges[i].se.se=edges[i+1].se.se=0;
if(edges[i]!=edges[i+1]) sub2=0;
edges[i].se.se=a1; edges[i+1].se.se=a2;
}
}
else sub2=0;
ori.Floyd();
ll as = ori.d[0][n-1]+ori.d[n-1][0];
oriunw.dijkstra(0);
for(int i=0;i<n;i++)
{
if(i==0) continue;
if(oriunw.par[i]!=-1)
{
//cerr<<oriunw.par[i]<<' '<<i<<' '<<ma[{oriunw.par[i],i}].se<<endl;
S.insert(ma[{oriunw.par[i],i}].se);
}
}
oriunw.dijkstra(n-1);
for(int i=0;i<n;i++)
{
if(i==n-1) continue;
if(oriunw.par[i]!=-1)
{
//cerr<<oriunw.par[i]<<' '<<i<<' '<<ma[{oriunw.par[i],i}].se<<endl;
S.insert(ma[{oriunw.par[i],i}].se);
}
}
/*
oriunw2.dijkstra(n-1);
for(int i=0;i<n;i++)
{
if(i==n-1) continue;
if(oriunw2.par[i]!=-1)
{
//cerr<<oriunw2.par[i]<<' '<<i<<' '<<ma[{oriunw2.par[i],i}].se<<endl;
S.insert(ma[{oriunw2.par[i],i}].se);
}
}
*/
oriunw2.dijkstra(0);
for(int i=0;i<n;i++)
{
if(i==0) continue;
if(oriunw2.par[i]!=-1)
{
//cerr<<oriunw.par[i]<<' '<<i<<' '<<ma[{oriunw.par[i],i}].se<<endl;
S.insert(ma[{oriunw2.par[i],i}].se);
}
}
res=min(res,as);
//test(ma[{141,15}].se);
//assert(sub2);
if(sub2)
{
for(auto E:edges)
{
int u=E.fi.fi; int v=E.fi.se;
ll c=E.se.fi; ll d=E.se.se;
//should i add the edge v->u
res=min(res,d+ori.d[0][n-1]+ori.d[n-1][v]+c+ori.d[u][0]);
res=min(res,d+ori.d[0][v]+c+ori.d[u][n-1]+ori.d[n-1][0]);
res=min(res,d+ori.d[0][v]+c+ori.d[u][n-1]+ori.d[n-1][v]+c+ori.d[u][0]);
res=min(res,ori.d[0][n-1]+ori.d[n-1][0]);
}
}
else
{
set<int> nw;
for(int s:S)
{
int u=edges[s].fi.fi; int v=edges[s].fi.se;
nw.insert(ma[{u,v}].se);
}
//cerr<<nw.size()<<endl;
for(int s:nw)
{
test(s);
}
}
if(res>=ll(1e18)) res=-1;
cout<<res<<'\n';
}
Compilation message
ho_t4.cpp: In member function 'void Graph::bfs(int)':
ho_t4.cpp:62:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++)
~~^~~~~~~~~~~~~~~
ho_t4.cpp: In member function 'void Graph::bfs01(int)':
ho_t4.cpp:85:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++)
~~^~~~~~~~~~~~~~~
ho_t4.cpp: In member function 'void Graph::dijkstra(int, int)':
ho_t4.cpp:119:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++)
~~^~~~~~~~~~~~~~~
ho_t4.cpp: In member function 'void Graph::Floyd()':
ho_t4.cpp:146:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < adj[i].size(); j++)
~~^~~~~~~~~~~~~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:194:6: warning: variable 'all0' set but not used [-Wunused-but-set-variable]
int all0=1;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
1144 KB |
Output is correct |
2 |
Correct |
17 ms |
760 KB |
Output is correct |
3 |
Correct |
51 ms |
996 KB |
Output is correct |
4 |
Correct |
55 ms |
1020 KB |
Output is correct |
5 |
Correct |
11 ms |
632 KB |
Output is correct |
6 |
Correct |
17 ms |
760 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
46 ms |
1016 KB |
Output is correct |
11 |
Correct |
50 ms |
1016 KB |
Output is correct |
12 |
Correct |
57 ms |
1016 KB |
Output is correct |
13 |
Correct |
35 ms |
1016 KB |
Output is correct |
14 |
Correct |
48 ms |
1016 KB |
Output is correct |
15 |
Correct |
45 ms |
1016 KB |
Output is correct |
16 |
Correct |
42 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
11112 KB |
Output is correct |
2 |
Correct |
73 ms |
10808 KB |
Output is correct |
3 |
Correct |
73 ms |
10596 KB |
Output is correct |
4 |
Correct |
16 ms |
1144 KB |
Output is correct |
5 |
Correct |
17 ms |
1016 KB |
Output is correct |
6 |
Correct |
14 ms |
888 KB |
Output is correct |
7 |
Correct |
14 ms |
760 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
73 ms |
11108 KB |
Output is correct |
10 |
Correct |
74 ms |
11364 KB |
Output is correct |
11 |
Correct |
72 ms |
10976 KB |
Output is correct |
12 |
Correct |
70 ms |
10852 KB |
Output is correct |
13 |
Correct |
75 ms |
10980 KB |
Output is correct |
14 |
Correct |
72 ms |
12352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
1016 KB |
Output is correct |
2 |
Correct |
18 ms |
760 KB |
Output is correct |
3 |
Correct |
635 ms |
11276 KB |
Output is correct |
4 |
Correct |
18 ms |
760 KB |
Output is correct |
5 |
Correct |
747 ms |
13152 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
640 ms |
13432 KB |
Output is correct |
9 |
Correct |
727 ms |
13344 KB |
Output is correct |
10 |
Correct |
751 ms |
12828 KB |
Output is correct |
11 |
Correct |
790 ms |
12724 KB |
Output is correct |
12 |
Correct |
779 ms |
13804 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 |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
795 ms |
13604 KB |
Output is correct |
20 |
Correct |
661 ms |
13028 KB |
Output is correct |
21 |
Correct |
770 ms |
12988 KB |
Output is correct |
22 |
Correct |
785 ms |
12660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
1144 KB |
Output is correct |
2 |
Correct |
17 ms |
760 KB |
Output is correct |
3 |
Correct |
51 ms |
996 KB |
Output is correct |
4 |
Correct |
55 ms |
1020 KB |
Output is correct |
5 |
Correct |
11 ms |
632 KB |
Output is correct |
6 |
Correct |
17 ms |
760 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
46 ms |
1016 KB |
Output is correct |
11 |
Correct |
50 ms |
1016 KB |
Output is correct |
12 |
Correct |
57 ms |
1016 KB |
Output is correct |
13 |
Correct |
35 ms |
1016 KB |
Output is correct |
14 |
Correct |
48 ms |
1016 KB |
Output is correct |
15 |
Correct |
45 ms |
1016 KB |
Output is correct |
16 |
Correct |
42 ms |
1016 KB |
Output is correct |
17 |
Correct |
75 ms |
11112 KB |
Output is correct |
18 |
Correct |
73 ms |
10808 KB |
Output is correct |
19 |
Correct |
73 ms |
10596 KB |
Output is correct |
20 |
Correct |
16 ms |
1144 KB |
Output is correct |
21 |
Correct |
17 ms |
1016 KB |
Output is correct |
22 |
Correct |
14 ms |
888 KB |
Output is correct |
23 |
Correct |
14 ms |
760 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
73 ms |
11108 KB |
Output is correct |
26 |
Correct |
74 ms |
11364 KB |
Output is correct |
27 |
Correct |
72 ms |
10976 KB |
Output is correct |
28 |
Correct |
70 ms |
10852 KB |
Output is correct |
29 |
Correct |
75 ms |
10980 KB |
Output is correct |
30 |
Correct |
72 ms |
12352 KB |
Output is correct |
31 |
Correct |
40 ms |
1016 KB |
Output is correct |
32 |
Correct |
18 ms |
760 KB |
Output is correct |
33 |
Correct |
635 ms |
11276 KB |
Output is correct |
34 |
Correct |
18 ms |
760 KB |
Output is correct |
35 |
Correct |
747 ms |
13152 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
640 ms |
13432 KB |
Output is correct |
39 |
Correct |
727 ms |
13344 KB |
Output is correct |
40 |
Correct |
751 ms |
12828 KB |
Output is correct |
41 |
Correct |
790 ms |
12724 KB |
Output is correct |
42 |
Correct |
779 ms |
13804 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 |
6 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
795 ms |
13604 KB |
Output is correct |
50 |
Correct |
661 ms |
13028 KB |
Output is correct |
51 |
Correct |
770 ms |
12988 KB |
Output is correct |
52 |
Correct |
785 ms |
12660 KB |
Output is correct |
53 |
Execution timed out |
1086 ms |
13228 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |