Submission #364063

#TimeUsernameProblemLanguageResultExecution timeMemory
364063Lam_lai_cuoc_doiOlympic Bus (JOI20_ho_t4)C++17
100 / 100
756 ms3180 KiB
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#include <algorithm>
#define task "busline"
using namespace std;
using ll = long long;
using ld = long double;

const int N = 2e2 + 5;
const int M = 5e4 + 5;
const ll Inf = 1e17;
struct Edge
{
    int u, v, c, d;
} e[M];
vector<int> adj[N], nadj[N];
int n, m;
int par[N];
ll d[7][N];
bool ok[N];
bitset<M> ck[7];
ll ans(Inf);

void Read()
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        cin >> e[i].u >> e[i].v >> e[i].c >> e[i].d;
        adj[e[i].u].push_back(i);
        nadj[e[i].v].push_back(i);
    }
}

void Dijkstra(int x, vector<int> adj[N], ll d[N], bitset<M> &ck, int except)
{
    for (int i = 0; i <= n; ++i)
    {
        d[i] = Inf;
        ok[i] = 0;
        par[i] = 0;
    }
    d[x] = 0;
    while (1)
    {
        int v(0);
        for (int i = 1; i <= n; ++i)
            if (!ok[i] && d[v] > d[i])
                v = i;
        if (v == 0)
            break;
        ok[v] = 1;
        for (auto i : adj[v])
            if (i != except && d[e[i].v ^ e[i].u ^ v] > d[v] + e[i].c)
            {
                d[e[i].v ^ e[i].u ^ v] = d[v] + e[i].c;
                par[e[i].v ^ e[i].u ^ v] = i;
            }
    }
    for (int i = 1; i <= n; ++i)
        if (i != x && d[i] != Inf)
            ck[par[i]] = 1;
}

ll Proc(int i, int x, int y, const vector<int> &s)
{
    int tmp1(s[0]), tmp2(s[3]);
    if (ck[s[0]][i])
    {
        Dijkstra(x, adj, d[5], ck[5], i);
        tmp1 = 5;
    }
    if (ck[s[3]][i])
    {
        Dijkstra(y, nadj, d[6], ck[6], i);
        tmp2 = 6;
    }
    if (d[tmp1][e[i].v] == Inf || d[tmp2][e[i].u] == Inf)
        return Inf;
    ll res = d[tmp1][e[i].v] + e[i].c + e[i].d + d[tmp2][e[i].u];
    tmp1 = s[2];
    tmp2 = s[1];
    if (ck[s[2]][i])
    {
        Dijkstra(y, adj, d[5], ck[5], i);
        tmp1 = 5;
    }
    if (ck[s[1]][i])
    {
        Dijkstra(x, nadj, d[6], ck[6], i);
        tmp2 = 6;
    }
    if ((d[tmp1][e[i].v] == Inf || d[tmp2][e[i].u] == Inf) && d[tmp1][x] == Inf)
        return Inf;
    res += min(d[tmp1][e[i].v] + e[i].c + d[tmp2][e[i].u], d[tmp1][x]);
    return res;
}

void Solve()
{
    Dijkstra(1, adj, d[1], ck[1], 0);
    Dijkstra(1, nadj, d[2], ck[2], 0);
    Dijkstra(n, adj, d[3], ck[3], 0);
    Dijkstra(n, nadj, d[4], ck[4], 0);
    if (d[1][n] != Inf && d[3][1] != Inf)
        ans = d[1][n] + d[3][1];
    for (int i = 1; i <= m; ++i)
    {
        ans = min(ans, Proc(i, 1, n, {1, 2, 3, 4}));
        ans = min(ans, Proc(i, n, 1, {3, 4, 1, 2}));
    }
    cout << (ans == Inf ? -1 : ans);
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen(task ".INP", "r"))
    {
        freopen(task ".INP", "r", stdin);
        freopen(task ".OUT", "w", stdout);
    }
    Read();
    Solve();
}

Compilation message (stderr)

ho_t4.cpp: In function 'int32_t main()':
ho_t4.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  124 |         freopen(task ".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  125 |         freopen(task ".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...