이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int MX = 205;
const long long INF = 1e17;
const int MXM = 5e4 + 5;
struct Edge {
int from, to, len, invert, id;
bool vip;
Edge() {vip = 0;}
Edge(int _from, int _to, int _len, int _invert, int _id) {
from = _from, to = _to, len = _len, invert = _invert, id = _id;
vip = 0;
}
bool operator< (const Edge &ot) const {
return make_pair(vip, id) < make_pair(ot.vip, ot.id);
}
} List[MXM];
int n, m;
vector<Edge>g[MX], gt[MX];
long long to_one[MX], to_n[MX], from_one[MX], from_n[MX];
int dad[MX];
void dijkstra(int sc, int graph, int ban, long long *a) {
fill(a+1, a+n+1, INF);
a[sc] = 0;
priority_queue<pair<long long,int>>pq;
pq.emplace(0, sc);
while(!pq.empty()) {
int v; long long cost;
tie(cost, v) = pq.top();
pq.pop();
cost = -cost;
if(a[v]!=cost) continue;
for(auto e : (graph==0?g:gt)[v]) if(e.id!=ban) {
if(cost+e.len<a[e.to]) {
a[e.to] = cost+e.len;
pq.emplace(-a[e.to], e.to);
dad[e.to] = e.id;
}
}
}
}
void mark(int f, int t) {
int cur = f;
while(cur!=t) {
List[dad[cur]].vip = 1;
cur = List[dad[cur]].from;
}
}
void PlayGround() {
cin>>n>>m;
for(int i=0; i<m; ++i) {
int u, v, c, d;
cin>>u>>v>>c>>d;
g[u].push_back(Edge(u, v, c, d, i));
gt[v].push_back(Edge(v, u, c, d, i));
List[i] = {u, v, c, d, i};
}
dijkstra(1, 0, -1, from_one);
if(from_one[n]!=INF) mark(n, 1);
dijkstra(n, 0, -1, from_n);
if(from_n[1]!=INF) mark(1, n);
dijkstra(1, 1, -1, to_one);
dijkstra(n, 1, -1, to_n);
for(int i=1; i<=n; ++i) {
for(auto &e : g[i]) {
e = List[e.id];
}
}
long long ans = min(INF, from_one[n] + from_n[1]);
sort(List, List+m);
for(int i=0; i<m; ++i) {
auto e = List[i];
if(e.vip) {
dijkstra(1, 0, e.id, from_one);
dijkstra(n, 0, e.id, from_n);
}
long long u, v, c, d;
u = e.from, v = e.to, c = e.len, d = e.invert;
//1->v->u->n->1
ans = min(ans, from_one[v]+to_n[u]+from_n[1]+c+d);
//1->v->u->n->v->u->1
ans = min(ans, from_one[v]+c+to_n[u]+from_n[v]+c+to_one[u]+d);
//1->n->v->u->1
ans = min(ans, from_one[n]+from_n[v]+to_one[u]+c+d);
}
if(ans==INF) ans = -1;
cout<<ans<<'\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
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... |