이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |