#include<bits/stdc++.h>
using namespace std;
const long long maxn=100000+10,maxm=200000+10;
struct yal{
long long u,v,c,w;
long long getad(long long fu){
return (u^v^fu);
}
}alle[maxm];
long long n,m,vis[maxn];
long long mainres=-1;
vector<long long>adj[maxn];
map<long long,long long>sum[maxn];
map<long long,vector<long long>>all[maxn];
void vorod(){
cin>>n>>m;
for(long long i=0;i<m;i++){
cin>>alle[i].u>>alle[i].v>>alle[i].c>>alle[i].w;
adj[alle[i].u].push_back(i);
adj[alle[i].v].push_back(i);
sum[alle[i].u][alle[i].c]+=alle[i].w;
sum[alle[i].v][alle[i].c]+=alle[i].w;
all[alle[i].u][alle[i].c].push_back(i);
all[alle[i].v][alle[i].c].push_back(i);
}
}
set<pair<long long,pair<long long,long long>>>st;
void aval(long long u,long long w){
for(auto x:adj[u]){
long long v=alle[x].getad(u);
st.insert(make_pair(w+min(alle[x].w,sum[u][alle[x].c]-alle[x].w),make_pair(v,-1)));
st.insert(make_pair(w,make_pair(v,x)));
}
}
void upd(long long u,long long ind,long long w){
if(ind==-1){
return ;
}
long long f=0;
for(auto x:all[u][alle[ind].c]){
if(x==ind){
f=1;
continue;
}
long long v=alle[x].getad(u);
st.insert(make_pair(w+sum[u][alle[x].c]-alle[x].w,make_pair(v,-1)));
}
all[u][alle[ind].c].clear();
if(f){
all[u][alle[ind].c].push_back(ind);
}
}
void solve(){
aval(1,0);
vis[1]=1;
while((long long)st.size()>0){
auto x=*st.begin();
st.erase(x);
long long u=x.second.first,w=x.first,from=x.second.second;
if(x.second.first==n&&from==-1){
mainres=x.first;
return ;
}
if(vis[u]==0&&from==-1){
aval(u,w);
vis[u]=1;
}
upd(u,from,w);
}
}
void khor(){
cout<<mainres<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("inp.txt","r",stdin);
vorod();
solve();
khor();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14996 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14960 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
15196 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
6 ms |
15452 KB |
Output is correct |
10 |
Correct |
7 ms |
15448 KB |
Output is correct |
11 |
Correct |
5 ms |
15452 KB |
Output is correct |
12 |
Correct |
5 ms |
15256 KB |
Output is correct |
13 |
Correct |
5 ms |
15452 KB |
Output is correct |
14 |
Correct |
5 ms |
15452 KB |
Output is correct |
15 |
Correct |
5 ms |
15224 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
5 ms |
15452 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
6 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
37276 KB |
Output is correct |
2 |
Correct |
113 ms |
24652 KB |
Output is correct |
3 |
Correct |
183 ms |
38944 KB |
Output is correct |
4 |
Correct |
115 ms |
28328 KB |
Output is correct |
5 |
Correct |
1053 ms |
83140 KB |
Output is correct |
6 |
Correct |
938 ms |
71760 KB |
Output is correct |
7 |
Correct |
534 ms |
71736 KB |
Output is correct |
8 |
Correct |
459 ms |
46676 KB |
Output is correct |
9 |
Correct |
488 ms |
46416 KB |
Output is correct |
10 |
Correct |
385 ms |
48356 KB |
Output is correct |
11 |
Correct |
103 ms |
34128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14996 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14960 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
15196 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
6 ms |
15452 KB |
Output is correct |
10 |
Correct |
7 ms |
15448 KB |
Output is correct |
11 |
Correct |
5 ms |
15452 KB |
Output is correct |
12 |
Correct |
5 ms |
15256 KB |
Output is correct |
13 |
Correct |
5 ms |
15452 KB |
Output is correct |
14 |
Correct |
5 ms |
15452 KB |
Output is correct |
15 |
Correct |
5 ms |
15224 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
5 ms |
15452 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
6 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15192 KB |
Output is correct |
21 |
Correct |
261 ms |
37276 KB |
Output is correct |
22 |
Correct |
113 ms |
24652 KB |
Output is correct |
23 |
Correct |
183 ms |
38944 KB |
Output is correct |
24 |
Correct |
115 ms |
28328 KB |
Output is correct |
25 |
Correct |
1053 ms |
83140 KB |
Output is correct |
26 |
Correct |
938 ms |
71760 KB |
Output is correct |
27 |
Correct |
534 ms |
71736 KB |
Output is correct |
28 |
Correct |
459 ms |
46676 KB |
Output is correct |
29 |
Correct |
488 ms |
46416 KB |
Output is correct |
30 |
Correct |
385 ms |
48356 KB |
Output is correct |
31 |
Correct |
103 ms |
34128 KB |
Output is correct |
32 |
Correct |
577 ms |
65880 KB |
Output is correct |
33 |
Correct |
408 ms |
51472 KB |
Output is correct |
34 |
Correct |
440 ms |
65992 KB |
Output is correct |
35 |
Correct |
313 ms |
53584 KB |
Output is correct |
36 |
Correct |
505 ms |
53992 KB |
Output is correct |
37 |
Correct |
523 ms |
62512 KB |
Output is correct |
38 |
Correct |
522 ms |
74532 KB |
Output is correct |
39 |
Correct |
437 ms |
58448 KB |
Output is correct |
40 |
Correct |
484 ms |
51796 KB |
Output is correct |
41 |
Correct |
585 ms |
52044 KB |
Output is correct |
42 |
Correct |
729 ms |
76408 KB |
Output is correct |
43 |
Correct |
240 ms |
46728 KB |
Output is correct |
44 |
Correct |
316 ms |
58108 KB |
Output is correct |
45 |
Correct |
376 ms |
47388 KB |
Output is correct |
46 |
Correct |
304 ms |
47124 KB |
Output is correct |
47 |
Correct |
486 ms |
67888 KB |
Output is correct |
48 |
Correct |
1420 ms |
111068 KB |
Output is correct |