#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define vl vector<long long>
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct e
{
ll cost;
int vert,ind;
bool operator<(const e& o)
{
return cost < o.cost;
}
};
pii edge[50001];
ll cost[50001];
ll swap_cost[50001];
vector<e> dij_graph[201];
ll ans = 1e18;
ll dist[50001][6];
pii nxt[50001];
int sort_ptr[201];
int n,m;
void dij(int start, int type)
{
rep2(i,1,n) dist[i][type] = 1e18;
rep2(i,1,n) nxt[i] = {0,0};
rep2(i,1,n) sort_ptr[i] = 0;
dist[start][type] = 0;
rep(i,n-1)
{
pair<pll,pii> best = {{1e18,1},{1,1}};
rep2(j,1,n)
{
if(dist[j][type] == 1e18) continue;
while(sort_ptr[j] < siz(dij_graph[j]))
{
if(dij_graph[j][sort_ptr[j]].vert != edge[dij_graph[j][sort_ptr[j]].ind].ss || dist[dij_graph[j][sort_ptr[j]].vert][type] != 1e18)
{
sort_ptr[j]++;
}
else break;
}
if(sort_ptr[j] < siz(dij_graph[j]))
{
if(dij_graph[j][sort_ptr[j]].vert == edge[dij_graph[j][sort_ptr[j]].ind].ss)
{
best = min(best,{{dist[j][type] + dij_graph[j][sort_ptr[j]].cost,j},{dij_graph[j][sort_ptr[j]].vert,dij_graph[j][sort_ptr[j]].ind}});
}
}
}
if(best.ff.ff == 1e18) break;
// cout << best.ff.ff << " " << best.ff.ss << " " << " best\n";
sort_ptr[best.ff.ss]++;
dist[best.ss.ff][type] = best.ff.ff;
nxt[best.ss.ff] = {best.ff.ss,best.ss.ss};
}
// cout << "\n\ndij_end\n\n";
}
void solve(int s, int t)
{
rep(i,m)
{
swap(edge[i].ff,edge[i].ss);
}
dij(t,1);
dij(s,5);
rep(i,m)
{
swap(edge[i].ff,edge[i].ss);
}
dij(s,0);
set<int> path_set;
int cur = t;
while(cur != s && cur != 0)
{
path_set.insert(nxt[cur].ss);
cur = nxt[cur].ff;
}
dij(t,4);
ans = min(ans,dist[s][4] + dist[t][0]);
cur = s;
while(cur != t && cur != 0)
{
path_set.insert(nxt[cur].ss);
cur = nxt[cur].ff;
}
rep(i,m)
{
if(path_set.find(i) == path_set.end())
{
ans = min(ans,swap_cost[i] + min(dist[t][0],dist[edge[i].ss][0] + dist[edge[i].ff][1] + cost[i]) + min(dist[s][4],dist[edge[i].ss][4] + cost[i] + dist[edge[i].ff][5]));
}
else
{
swap(edge[i].ff,edge[i].ss);
dij(s,2);
dij(t,3);
swap(edge[i].ff,edge[i].ss);
ans = min(ans,dist[t][2] + dist[s][3] + swap_cost[i]);
}
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random();
cin >> n >> m;
rep(i,m)
{
int a,b;
cin >> a >> b >> cost[i] >> swap_cost[i];
e xd = {cost[i],b,i};
dij_graph[a].pb(xd);
xd = {cost[i],a,i};
dij_graph[b].pb(xd);
edge[i] = {a,b};
}
rep2(i,1,n) sort(all(dij_graph[i]));
solve(1,n);
if(ans != 1e18)
{
cout << ans << "\n";
}
else cout << "-1\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... |