This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<algorithm>
#include<queue>
#include<utility>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
struct edge{
int a,b,c;
ll p;
edge(int _a,int _b,int _c,ll _p){
a=_a,b=_b,c=_c,p=_p;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
int a,b,c;
ll p;
vector<edge> edges;
vector<map<int,ll> > sum(n+1),rep(n+1);
for(int i=0;i<m;i++){
cin>>a>>b>>c>>p;
edges.emplace_back(a,b,c,p);
sum[a][c]+=p;
sum[b][c]+=p;
}
int cnt=1;
for(int i=1;i<=n;i++) for(auto x:sum[i]) rep[i][x.first]=cnt++;
vector<vector<pair<int,ll> > > graph(cnt+n+1);
for(auto &[d,e,f,q]:edges){
graph[cnt+d].push_back(make_pair(cnt+e,min(q,sum[d][f]-q)));
graph[cnt+e].push_back(make_pair(cnt+d,min(q,sum[e][f]-q)));
graph[cnt+d].push_back(make_pair(rep[e][f],0));
graph[cnt+e].push_back(make_pair(rep[d][f],0));
graph[rep[d][f]].push_back(make_pair(cnt+e,sum[d][f]-q));
graph[rep[e][f]].push_back(make_pair(cnt+d,sum[e][f]-q));
}
vector<ll> dist(cnt+n+1,1e18);
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > pq;
dist[cnt+1]=0;
pq.push(make_pair(0,cnt+1));
while(!pq.empty()){
auto [d,node]=pq.top();
pq.pop();
if(dist[node]!=d) continue;
for(auto &[x,l]:graph[node]){
if(d+l<dist[x]){
dist[x]=d+l;
pq.push(make_pair(d+l,x));
}
}
}
if(dist[cnt+n]==1e18) cout<<"-1\n";
else cout<<dist[cnt+n]<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |