//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 50010;
int dis[222][222];
int dist[222];
bool vis[222];
int n,m,big;
struct EDGE{
int u,v,c,d;
} e[maxn];
vector <int> redge[222],edge[222];
pq <pii,vector<pii>,greater<pii> > q;
int ans = 2e9;
bool used[maxn];
int dijkstra(int st,int ed,int rev) {
memset(dist,inf,sizeof(dist));
dist[st] = 0;
while (!q.empty()) q.pop();
q.push({0,st});
while (!q.empty()) {
pii cur = q.top();
q.pop();
if (dist[cur.se]!=cur.fi) continue;
int u = cur.se;
if (u==ed) return cur.fi;
for (int i:edge[u]) {
int v=e[i].v;
if (i==rev) {
if (e[i].u==u) continue;
else v = e[i].u;
}
if (dist[v] > dist[u] + e[i].c) {
dist[v] = dist[u] + e[i].c;
q.push({dist[v],v});
}
}
}
return big;
}
bool dfs(int st,int ed,int u) {
vis[u]=true;
if (u==ed) return true;
for (int i:edge[u]) {
int v=e[i].v;
if (!vis[v] and dis[st][u] + e[i].c + dis[v][ed]==dis[st][ed] and dfs(st,ed,v)) {
used[i]=true;
return true;
}
}
return false;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
memset(dis,inf,sizeof(dis));
big = dis[0][0];
rep(i,1,n+1) dis[i][i] =0 ;
rep(i,0,m) {
cin>>e[i].u>>e[i].v>>e[i].c>>e[i].d;
dis[e[i].u][e[i].v] = min(dis[e[i].u][e[i].v],e[i].c);
redge[e[i].v].pb(i);
edge[e[i].u].pb(i);
}
rep(k,1,n+1)
rep(i,1,n+1)
rep(j,1,n+1) dis[i][j] = min(dis[i][k] + dis[k][j],dis[i][j]);
if (dis[1][n]!=big) dfs(1,n,1);
if (dis[n][1]!=big) memset(vis,false,sizeof(vis)),dfs(n,1,n);
if (dis[1][n]!=big and dis[n][1]!=big) ans = dis[1][n] + dis[n][1];
rep(i,0,m) {
int tmp1,tmp2;
if (used[i]) {
edge[e[i].v].pb(i);
tmp1 = dijkstra(1,n,i);
tmp2 = dijkstra(n,1,i);
edge[e[i].v].pop_back();
}
else {
tmp1 = min((ll)dis[1][n],(ll)dis[1][e[i].v] + (ll)e[i].c + (ll)dis[e[i].u][n]);
tmp2 = min((ll)dis[n][1],(ll)dis[n][e[i].v] + (ll)e[i].c + (ll)dis[e[i].u][1]);
}
if (tmp1==big or tmp2==big) continue;
ans = min(ans,tmp1+tmp2+e[i].d);
}
if (ans==2e9) cout<<-1;
else cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
468 KB |
Output is correct |
2 |
Correct |
9 ms |
564 KB |
Output is correct |
3 |
Correct |
10 ms |
572 KB |
Output is correct |
4 |
Correct |
9 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
8 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
23 ms |
596 KB |
Output is correct |
11 |
Correct |
26 ms |
604 KB |
Output is correct |
12 |
Correct |
26 ms |
604 KB |
Output is correct |
13 |
Correct |
8 ms |
596 KB |
Output is correct |
14 |
Correct |
10 ms |
620 KB |
Output is correct |
15 |
Correct |
9 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
1976 KB |
Output is correct |
2 |
Correct |
26 ms |
2992 KB |
Output is correct |
3 |
Correct |
28 ms |
3244 KB |
Output is correct |
4 |
Correct |
10 ms |
620 KB |
Output is correct |
5 |
Correct |
11 ms |
492 KB |
Output is correct |
6 |
Correct |
9 ms |
576 KB |
Output is correct |
7 |
Correct |
10 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
26 ms |
3120 KB |
Output is correct |
10 |
Correct |
25 ms |
3084 KB |
Output is correct |
11 |
Correct |
30 ms |
3184 KB |
Output is correct |
12 |
Correct |
27 ms |
3148 KB |
Output is correct |
13 |
Correct |
30 ms |
3100 KB |
Output is correct |
14 |
Correct |
31 ms |
3180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
468 KB |
Output is correct |
2 |
Correct |
9 ms |
552 KB |
Output is correct |
3 |
Correct |
36 ms |
1620 KB |
Output is correct |
4 |
Correct |
9 ms |
468 KB |
Output is correct |
5 |
Correct |
51 ms |
2020 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
24 ms |
2004 KB |
Output is correct |
9 |
Correct |
31 ms |
1896 KB |
Output is correct |
10 |
Correct |
53 ms |
1940 KB |
Output is correct |
11 |
Correct |
37 ms |
1992 KB |
Output is correct |
12 |
Correct |
34 ms |
2012 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
476 KB |
Output is correct |
19 |
Correct |
60 ms |
2920 KB |
Output is correct |
20 |
Correct |
46 ms |
2920 KB |
Output is correct |
21 |
Correct |
38 ms |
2904 KB |
Output is correct |
22 |
Correct |
39 ms |
2876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
468 KB |
Output is correct |
2 |
Correct |
9 ms |
564 KB |
Output is correct |
3 |
Correct |
10 ms |
572 KB |
Output is correct |
4 |
Correct |
9 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
8 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
23 ms |
596 KB |
Output is correct |
11 |
Correct |
26 ms |
604 KB |
Output is correct |
12 |
Correct |
26 ms |
604 KB |
Output is correct |
13 |
Correct |
8 ms |
596 KB |
Output is correct |
14 |
Correct |
10 ms |
620 KB |
Output is correct |
15 |
Correct |
9 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
596 KB |
Output is correct |
17 |
Correct |
25 ms |
1976 KB |
Output is correct |
18 |
Correct |
26 ms |
2992 KB |
Output is correct |
19 |
Correct |
28 ms |
3244 KB |
Output is correct |
20 |
Correct |
10 ms |
620 KB |
Output is correct |
21 |
Correct |
11 ms |
492 KB |
Output is correct |
22 |
Correct |
9 ms |
576 KB |
Output is correct |
23 |
Correct |
10 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
26 ms |
3120 KB |
Output is correct |
26 |
Correct |
25 ms |
3084 KB |
Output is correct |
27 |
Correct |
30 ms |
3184 KB |
Output is correct |
28 |
Correct |
27 ms |
3148 KB |
Output is correct |
29 |
Correct |
30 ms |
3100 KB |
Output is correct |
30 |
Correct |
31 ms |
3180 KB |
Output is correct |
31 |
Correct |
12 ms |
468 KB |
Output is correct |
32 |
Correct |
9 ms |
552 KB |
Output is correct |
33 |
Correct |
36 ms |
1620 KB |
Output is correct |
34 |
Correct |
9 ms |
468 KB |
Output is correct |
35 |
Correct |
51 ms |
2020 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
24 ms |
2004 KB |
Output is correct |
39 |
Correct |
31 ms |
1896 KB |
Output is correct |
40 |
Correct |
53 ms |
1940 KB |
Output is correct |
41 |
Correct |
37 ms |
1992 KB |
Output is correct |
42 |
Correct |
34 ms |
2012 KB |
Output is correct |
43 |
Correct |
0 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
476 KB |
Output is correct |
49 |
Correct |
60 ms |
2920 KB |
Output is correct |
50 |
Correct |
46 ms |
2920 KB |
Output is correct |
51 |
Correct |
38 ms |
2904 KB |
Output is correct |
52 |
Correct |
39 ms |
2876 KB |
Output is correct |
53 |
Correct |
29 ms |
3004 KB |
Output is correct |
54 |
Correct |
30 ms |
2920 KB |
Output is correct |
55 |
Correct |
28 ms |
2908 KB |
Output is correct |
56 |
Correct |
9 ms |
596 KB |
Output is correct |
57 |
Correct |
9 ms |
600 KB |
Output is correct |
58 |
Correct |
116 ms |
2556 KB |
Output is correct |
59 |
Correct |
115 ms |
2504 KB |
Output is correct |
60 |
Correct |
156 ms |
2496 KB |
Output is correct |
61 |
Correct |
106 ms |
2504 KB |
Output is correct |
62 |
Correct |
106 ms |
2512 KB |
Output is correct |
63 |
Correct |
138 ms |
2408 KB |
Output is correct |
64 |
Correct |
554 ms |
2792 KB |
Output is correct |
65 |
Correct |
598 ms |
2772 KB |
Output is correct |
66 |
Correct |
803 ms |
2860 KB |
Output is correct |
67 |
Correct |
13 ms |
2468 KB |
Output is correct |
68 |
Correct |
26 ms |
3016 KB |
Output is correct |
69 |
Correct |
26 ms |
3100 KB |
Output is correct |
70 |
Correct |
29 ms |
3132 KB |
Output is correct |
71 |
Correct |
29 ms |
3124 KB |
Output is correct |
72 |
Correct |
28 ms |
3020 KB |
Output is correct |
73 |
Correct |
35 ms |
3108 KB |
Output is correct |
74 |
Correct |
29 ms |
3092 KB |
Output is correct |