/***Farhan132***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<ll> vll;
typedef pair<ll , ll> ii;
typedef vector< ii > vii;
typedef pair < pair < ll , ll > , pair < ll , ll > > cm;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define PI 3.14159265359
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcountll(n)
#define clean fflush(stdout)
const ll mod = (ll)1e9 + 7;
const ll maxn = (ll)2e5 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
//const ll INF = 1e18;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
struct edge{
ll x, y , cost, r , id;
};
ll n , m;
vector < edge > g;
vector < edge > v[205] , rev[205];
set < ll > cool;
ll par[10][205];
ll f[10][205];
ll dis[10][205];
edge W;
void shortest(ll x, ll w, ll lol, edge kk){
if(lol != -1) for(ll i = 1; i <= n; i++) dis[w][i] = -1;
priority_queue < cm , vector < cm > , greater < cm > > q;
q.push({{0 , x} , { -1, -1 } });
while(q.size()){
auto [P1, P2] = q.top();
q.pop();
auto [D , node] = P1;
auto [p , e] = P2;
if(dis[w][node] != -1) continue;
dis[w][node] = D;
par[w][node] = p;
f[w][node] = e;
if(w <= 1){
for(edge u : v[node]){
if(dis[w][u.y] == -1) q.push({{dis[w][node] + u.cost , u.y}, { node , u.id }});
}
}else if(w <= 3){
for(edge u : rev[node]){
if(dis[w][u.y] == -1) q.push({{dis[w][node] + u.cost , u.y}, { node , u.id }});
}
}else if(w <= 5){
for(edge u : v[node]){
if(dis[w][u.y] == -1 && u.id != lol) q.push({{dis[w][node] + u.cost , u.y}, { node , u.id }});
}
if(kk.x == node && dis[w][kk.y] == -1) q.push({ {dis[w][node] + kk.cost + kk.r, kk.y} , {node , kk.id} } );
}
}
if(w >= 2) return;
ll cur = 1;
if(x == 1) cur = n;
if(dis[w][cur] == -1) return;
while(1){
if(par[w][cur] == -1) break;
cool.in(f[w][cur]);
cur = par[w][cur];
}
return;
}
void solve(){
cin >> n >> m;
for(ll i = 1; i <= m; i++){
ll x , y , cost , r;
cin >> x >> y >> cost >> r;
v[x].pb({x,y,cost,r,i});
rev[y].pb({y,x,cost,r,i});
g.pb({x,y,cost,r,i});
}
mem(dis,-1);
shortest(1, 0, -1, W);
shortest(n, 1, -1, W);
shortest(1, 2, -1, W);
shortest(n, 3, -1, W);
ll ans = 1e18;
if(dis[0][n] != -1 && dis[1][1] != -1) ans = dis[1][1] + dis[0][n];
for(edge u : g){
if(cool.count(u.id)){
edge passing;
passing = u;
passing.y = u.x;
passing.x = u.y;
shortest(1, 4, u.id, passing);
shortest(n, 5, u.id, passing);
if(dis[4][n] == -1 || dis[5][1] == -1) continue;
ans = min(ans , dis[4][n] + dis[5][1]);
}else{
ll X = 1e18 , Y = 1e18;
if(dis[0][n] != -1) X = dis[0][n];
if(dis[1][1] != -1) Y = dis[1][1];
if(dis[0][u.y] != -1 && dis[3][u.x] != -1) X = min(X , dis[0][u.y] + dis[3][u.x] + u.cost );
if(dis[1][u.y] != -1 && dis[2][u.x] != -1) Y = min(Y, dis[1][u.y] + dis[2][u.x] + u.cost );
ans = min(ans , X + Y + u.r);
}
}
if(ans == 1e18) ans = -1;
cout << ans;
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
// freopen("cbarn2.in", "r", stdin);
// freopen("cbarn2.out", "w", stdout);
// ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//cout << fixed << setprecision(10);
// work(maxn);
// mem(b,0);
ll T;
T=1;
//cin >> T;
//scanf("%lld",&T);
// ll CT = 0;
//work();
while(T--){
//cout << "Case " << ++CT <<": " ;
//printf("Case %lld: ",++CT);
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
34 ms |
620 KB |
Output is correct |
11 |
Correct |
41 ms |
620 KB |
Output is correct |
12 |
Correct |
40 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
10048 KB |
Output is correct |
2 |
Correct |
158 ms |
10236 KB |
Output is correct |
3 |
Correct |
158 ms |
10108 KB |
Output is correct |
4 |
Correct |
5 ms |
748 KB |
Output is correct |
5 |
Correct |
4 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
73 ms |
9104 KB |
Output is correct |
10 |
Correct |
77 ms |
8956 KB |
Output is correct |
11 |
Correct |
153 ms |
10120 KB |
Output is correct |
12 |
Correct |
149 ms |
10368 KB |
Output is correct |
13 |
Correct |
131 ms |
10108 KB |
Output is correct |
14 |
Correct |
131 ms |
10620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
101 ms |
7424 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
111 ms |
9084 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
67 ms |
8316 KB |
Output is correct |
9 |
Correct |
66 ms |
8956 KB |
Output is correct |
10 |
Correct |
104 ms |
8968 KB |
Output is correct |
11 |
Correct |
104 ms |
9596 KB |
Output is correct |
12 |
Correct |
105 ms |
9596 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
108 ms |
9596 KB |
Output is correct |
20 |
Correct |
108 ms |
9348 KB |
Output is correct |
21 |
Correct |
118 ms |
9340 KB |
Output is correct |
22 |
Correct |
120 ms |
9340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
34 ms |
620 KB |
Output is correct |
11 |
Correct |
41 ms |
620 KB |
Output is correct |
12 |
Correct |
40 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
17 |
Correct |
149 ms |
10048 KB |
Output is correct |
18 |
Correct |
158 ms |
10236 KB |
Output is correct |
19 |
Correct |
158 ms |
10108 KB |
Output is correct |
20 |
Correct |
5 ms |
748 KB |
Output is correct |
21 |
Correct |
4 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
73 ms |
9104 KB |
Output is correct |
26 |
Correct |
77 ms |
8956 KB |
Output is correct |
27 |
Correct |
153 ms |
10120 KB |
Output is correct |
28 |
Correct |
149 ms |
10368 KB |
Output is correct |
29 |
Correct |
131 ms |
10108 KB |
Output is correct |
30 |
Correct |
131 ms |
10620 KB |
Output is correct |
31 |
Correct |
5 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
101 ms |
7424 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
111 ms |
9084 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
0 ms |
364 KB |
Output is correct |
38 |
Correct |
67 ms |
8316 KB |
Output is correct |
39 |
Correct |
66 ms |
8956 KB |
Output is correct |
40 |
Correct |
104 ms |
8968 KB |
Output is correct |
41 |
Correct |
104 ms |
9596 KB |
Output is correct |
42 |
Correct |
105 ms |
9596 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
108 ms |
9596 KB |
Output is correct |
50 |
Correct |
108 ms |
9348 KB |
Output is correct |
51 |
Correct |
118 ms |
9340 KB |
Output is correct |
52 |
Correct |
120 ms |
9340 KB |
Output is correct |
53 |
Correct |
143 ms |
9724 KB |
Output is correct |
54 |
Correct |
198 ms |
10236 KB |
Output is correct |
55 |
Correct |
178 ms |
9724 KB |
Output is correct |
56 |
Correct |
3 ms |
620 KB |
Output is correct |
57 |
Correct |
3 ms |
620 KB |
Output is correct |
58 |
Execution timed out |
1093 ms |
8188 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |