이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<vector<pair<ll,pair<ll,ll>>>> graph(n+1);
int a,b,c,d;
vector<vector<ll>> dist(n+1,vector<ll>(n+1,1e18));
for(int i=1;i<=n;i++) dist[i][i]=0;
for(int i=0;i<m;i++){
cin>>a>>b>>c>>d;
graph[a].push_back(make_pair(b,make_pair(c,d)));
dist[a][b]=min(dist[a][b],1ll*c);
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
ll ans=dist[1][n]+dist[n][1];
for(int i=1;i<=n;i++){
for(auto x:graph[i]){
ans=min(ans,min(dist[1][n],dist[1][x.first]+x.second.first+dist[i][n])+min(dist[n][1],dist[n][x.first]+x.second.first+dist[i][1])+x.second.second);
}
}
if(ans>=1e18) cout<<-1;
else cout<<ans;
//for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cout<<i<<" "<<j<<" "<<dist[i][j]<<"\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |