#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e12;
struct edge{
int first,second,d;
edge(int f,int s,int c):first(f),second(s),d(c){}
};
vector<edge> adj[201];
bool visited[201];
int n;
struct comp{
bool operator()(pair<int,int> a,pair<int,int> b){
return a.second==b.second ? a.first>b.first : a.second>b.second;
}
};
int dijkastra(){
priority_queue<pair<int,int>,vector<pair<int,int>>,comp> q;
int c = INF;
for(int i=1;i<=n;i++)visited[i]=false;
q.emplace(1,0);
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.first])continue;
visited[curr.first]=true;
if(curr.first==n){
c = curr.second;
break;
}
for(auto&x:adj[curr.first])if(!visited[x.first])q.emplace(x.first,x.second+curr.second);
}
if(!visited[n])return INF;
for(int i=1;i<=n;i++)visited[i]=false;
q.emplace(n,0);
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.first])continue;
visited[curr.first]=true;
if(curr.first==1){
c += curr.second;
break;
}
for(auto&x:adj[curr.first])if(!visited[x.first])q.emplace(x.first,x.second+curr.second);
}
if(!visited[1])return INF;
return c;
}
int32_t main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a,b,c,d;cin>>a>>b>>c>>d;
adj[a].emplace_back(b,c,d);
}
for(int i=1;i<=n;i++)adj[i].emplace_back(i,INF,INF);
int ans = dijkastra();
for(int i=1;i<=n;i++){
for(auto&x:adj[i]){
if(x.second==INF)continue;
adj[x.first].emplace_back(i,x.second,x.d);
x.second=INF;
ans = min(ans,dijkastra()+x.d);
x.second=adj[x.first].back().second;
adj[x.first].back() = edge(0,INF,INF);
}
}
cout << (ans>=INF ? -1 : ans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
33 ms |
600 KB |
Output is correct |
4 |
Correct |
48 ms |
600 KB |
Output is correct |
5 |
Correct |
30 ms |
604 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1066 ms |
4148 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1094 ms |
2436 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
33 ms |
600 KB |
Output is correct |
4 |
Correct |
48 ms |
600 KB |
Output is correct |
5 |
Correct |
30 ms |
604 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |