이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
const int N = 203;
vector<ar<ll,3>> adj[N];
ll dis[N];
vector<ar<ll,4>> v;
void Dijkstra(ll e , int idx){
priority_queue< pair<ll,ll>, vector <pair<ll,ll>> , greater<pair<ll,ll>> > pq;
dis[e]=0;
pq.push({0,e});
while(!pq.empty()){
ll node = pq.top().second,ww=pq.top().first;
pq.pop();
if(dis[node] < ww)continue;
for(auto i : adj[node]){
if(i[2] == idx)continue;
ll w = i[1];
if(dis[i[0]] > dis[node] + w){
dis[i[0]] = dis[node] + w;
pq.push({dis[i[0]],i[0]});
}
}
if(node == v[idx][1]){
ll w = v[idx][2];
if(dis[v[idx][0]] > dis[node] + w){
dis[v[idx][0]] = dis[node] + w;
pq.push({dis[v[idx][0]],v[idx][0]});
}
}
}
}
void solve(){
int n , m;
cin >> n >> m;
ll fw[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j)fw[i][j] = 0;
else fw[i][j] = 1e18;
}
}
for(int i = 0; i < m; i++){
ll a , b , c , d;
cin >> a >> b >> c >> d;
a--;
b--;
adj[a].push_back({b,c,i});
v.push_back({a,b,c,d});
fw[a][b] = min(fw[a][b] , c);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int k =0 ; k < n; k++){
fw[j][k] = min(fw[j][k] , fw[j][i] + fw[i][k]);
}
}
}
ll ans = fw[0][n-1] + fw[n-1][0];
for(int i = 0; i < m; i++){
ll res = min(fw[0][n-1] , fw[0][v[i][1]] + fw[v[i][0]][n-1]) + min(fw[n-1][0] , fw[n-1][v[i][1]] + fw[v[i][0]][0]) + v[i][3] + v[i][2];
if(res < ans){
for(int j = 0; j < n; j++)dis[j] = 1e18;
Dijkstra(0 , i);
res = dis[n-1];
for(int j = 0; j < n; j++)dis[j] = 1e18;
Dijkstra(n-1 , i);
res += dis[0];
res += v[i][3];
ans = min(ans , res);
}
}
cout << (ans >= 1e18 ? -1 : ans) << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# | 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... |