#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define len(s) (int)((s).size())
#define MASK(k)(1LL<<(k))
#define int long long
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 200005;
struct Edge{
int u, v, c, col;
Edge(int u=0, int v=0, int col=0, int c=0){
this->u = u;
this->v = v;
this->col = col;
this->c = c;
}
int other(int x){
return u ^ v ^ x;
}
};
int n, m;
Edge edges[MX];
vector<int> G[MX];
map<int, vector<int>> T[MX];
void nhap(){
cin >> n >> m;
for(int i=1; i<=m; i++){
int u, v, col, c;
cin >> u >> v >> col >> c;
edges[i] = Edge(u, v, col, c);
G[u].push_back(i);
G[v].push_back(i);
T[u][col].push_back(i);
T[v][col].push_back(i);
}
}
int d[MX];
map<int, int> sum[MX];
map<int, int> f[MX];
void solve(){
for(int i=1; i<=m; i++){
int u = edges[i].u;
int v = edges[i].v;
int c = edges[i].c;
int col = edges[i].col;
sum[u][col] += c;
sum[v][col] += c;
}
priority_queue<tpiii, vector<tpiii>, greater<tpiii>> Q;
memset(d, 0x3f, sizeof d);
d[1] = 0;
Q.push({0, -1, 1});
while(Q.size()){
int du, cl, u;
tie(du, cl, u) = Q.top();
Q.pop();
if(cl == -1 && d[u] != du) continue;
if(cl != -1 && f[u][cl] != du) continue;
if(cl == -1){
for(int i: G[u]){
int c = edges[i].c;
int cl = edges[i].col;
int v = edges[i].other(u);
int scl = sum[u][cl];
if(f[v].find(cl) == f[v].end()) f[v][cl] = 1e18;
if(d[v] > min(du + c , du + scl - c)){
d[v] = min(du + c , du + scl - c);
Q.push({d[v], -1, v});
}
if(f[v][cl] > du){
f[v][cl] = du;
Q.push({f[v][cl], cl, v});
}
}
}
else{
for(int i: T[u][cl]){
int c = edges[i].c;
int v = edges[i].other(u);
int scl = sum[u][cl];
if(d[v] > du + scl - c){
d[v] = du + scl - c;
Q.push({d[v], -1, v});
}
}
}
}
cout << (d[n] == d[0]? -1 : d[n]);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("robot1.inp","r",stdin);
//freopen("robot1.out","w",stdout);
nhap();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
41048 KB |
Output is correct |
2 |
Correct |
17 ms |
41140 KB |
Output is correct |
3 |
Correct |
17 ms |
41048 KB |
Output is correct |
4 |
Correct |
17 ms |
41012 KB |
Output is correct |
5 |
Correct |
17 ms |
41136 KB |
Output is correct |
6 |
Correct |
17 ms |
41052 KB |
Output is correct |
7 |
Correct |
22 ms |
41360 KB |
Output is correct |
8 |
Correct |
18 ms |
41052 KB |
Output is correct |
9 |
Correct |
20 ms |
41740 KB |
Output is correct |
10 |
Correct |
19 ms |
41564 KB |
Output is correct |
11 |
Correct |
18 ms |
41308 KB |
Output is correct |
12 |
Correct |
18 ms |
41308 KB |
Output is correct |
13 |
Correct |
19 ms |
41560 KB |
Output is correct |
14 |
Correct |
22 ms |
41560 KB |
Output is correct |
15 |
Correct |
19 ms |
41308 KB |
Output is correct |
16 |
Correct |
18 ms |
41304 KB |
Output is correct |
17 |
Correct |
19 ms |
41564 KB |
Output is correct |
18 |
Correct |
17 ms |
41244 KB |
Output is correct |
19 |
Correct |
18 ms |
41308 KB |
Output is correct |
20 |
Correct |
18 ms |
41308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
62052 KB |
Output is correct |
2 |
Correct |
71 ms |
52104 KB |
Output is correct |
3 |
Correct |
133 ms |
58632 KB |
Output is correct |
4 |
Correct |
90 ms |
55304 KB |
Output is correct |
5 |
Correct |
700 ms |
111792 KB |
Output is correct |
6 |
Correct |
599 ms |
103084 KB |
Output is correct |
7 |
Correct |
243 ms |
79772 KB |
Output is correct |
8 |
Correct |
290 ms |
78160 KB |
Output is correct |
9 |
Correct |
324 ms |
78264 KB |
Output is correct |
10 |
Correct |
224 ms |
76676 KB |
Output is correct |
11 |
Correct |
93 ms |
60240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
41048 KB |
Output is correct |
2 |
Correct |
17 ms |
41140 KB |
Output is correct |
3 |
Correct |
17 ms |
41048 KB |
Output is correct |
4 |
Correct |
17 ms |
41012 KB |
Output is correct |
5 |
Correct |
17 ms |
41136 KB |
Output is correct |
6 |
Correct |
17 ms |
41052 KB |
Output is correct |
7 |
Correct |
22 ms |
41360 KB |
Output is correct |
8 |
Correct |
18 ms |
41052 KB |
Output is correct |
9 |
Correct |
20 ms |
41740 KB |
Output is correct |
10 |
Correct |
19 ms |
41564 KB |
Output is correct |
11 |
Correct |
18 ms |
41308 KB |
Output is correct |
12 |
Correct |
18 ms |
41308 KB |
Output is correct |
13 |
Correct |
19 ms |
41560 KB |
Output is correct |
14 |
Correct |
22 ms |
41560 KB |
Output is correct |
15 |
Correct |
19 ms |
41308 KB |
Output is correct |
16 |
Correct |
18 ms |
41304 KB |
Output is correct |
17 |
Correct |
19 ms |
41564 KB |
Output is correct |
18 |
Correct |
17 ms |
41244 KB |
Output is correct |
19 |
Correct |
18 ms |
41308 KB |
Output is correct |
20 |
Correct |
18 ms |
41308 KB |
Output is correct |
21 |
Correct |
125 ms |
62052 KB |
Output is correct |
22 |
Correct |
71 ms |
52104 KB |
Output is correct |
23 |
Correct |
133 ms |
58632 KB |
Output is correct |
24 |
Correct |
90 ms |
55304 KB |
Output is correct |
25 |
Correct |
700 ms |
111792 KB |
Output is correct |
26 |
Correct |
599 ms |
103084 KB |
Output is correct |
27 |
Correct |
243 ms |
79772 KB |
Output is correct |
28 |
Correct |
290 ms |
78160 KB |
Output is correct |
29 |
Correct |
324 ms |
78264 KB |
Output is correct |
30 |
Correct |
224 ms |
76676 KB |
Output is correct |
31 |
Correct |
93 ms |
60240 KB |
Output is correct |
32 |
Correct |
103 ms |
54164 KB |
Output is correct |
33 |
Correct |
102 ms |
57052 KB |
Output is correct |
34 |
Correct |
274 ms |
78076 KB |
Output is correct |
35 |
Correct |
186 ms |
69220 KB |
Output is correct |
36 |
Correct |
216 ms |
73156 KB |
Output is correct |
37 |
Correct |
247 ms |
75696 KB |
Output is correct |
38 |
Correct |
262 ms |
85916 KB |
Output is correct |
39 |
Correct |
105 ms |
58108 KB |
Output is correct |
40 |
Correct |
283 ms |
79432 KB |
Output is correct |
41 |
Correct |
306 ms |
79872 KB |
Output is correct |
42 |
Correct |
334 ms |
89464 KB |
Output is correct |
43 |
Correct |
127 ms |
64288 KB |
Output is correct |
44 |
Correct |
252 ms |
70044 KB |
Output is correct |
45 |
Correct |
234 ms |
75376 KB |
Output is correct |
46 |
Correct |
207 ms |
75092 KB |
Output is correct |
47 |
Correct |
255 ms |
75000 KB |
Output is correct |
48 |
Correct |
842 ms |
113112 KB |
Output is correct |