[2020 Niuke Multi-School] 2020 Niuke Summer Multi-School Training Camp(2番目のセッション)Iインターバル-最短経路を求める最大循環デュアル図

トピックリンク

題名

間隔を与える[l、r] [l、r][ l r ]、次の操作を許可します。

  1. PUT [L、R]、[L、R][ l r ][l − 1、r] [l-1、 r ]に変わります[ l1 r ]または[l + 1、r] [l + 1、r][ l+1 r ]
  2. PUT [L、R]、[L、R][ l r ][l、r − 1] [l、r-1]に変わります[ l r1 ]または[l、r + 1] [l、r + 1][ l r+1 ]

そして证l≤r およびl> 0 r≤nl \ leq r \ spaceおよび\ space l> 0 \ space r \ leq nlR n個のD L  >0 r 

ただし、一連の制限l、r、dir、cl、r、dir、cl r D iのrはcは、現在の間隔が[l、r] [l、r]であることを意味します[ l r ]、現在の間隔は操作できません1 11dir = L)または操作2 22dir = R)、およびこの制限を有効にするにはccが必要cコスト
この制限を有効にするかどうかを選択できます

それは、少なくとも達成するのにかかるどのくらい掲載していない区間に[1、n]は、[1、 n]は[ 1 n ]l = rl = rになりますl=rの間隔。

分析

1から、n 1、n1 n = l = rl = rに変換できますl=rは最短経路で計算できます。
しかし、最短経路に到達できない場合(つまり、問題変更できない場合)に必要な制限の数、およびこれらの条件を知ることは不可能です。
したがって、最大フローを使用して解決します

最大流量

グリッドダイアグラムを描画
します。エッジで変換できる2つの状態すべてを接続します。制限がある場合は、フローをコストに制限します。制限がない場合は、INFに設定しますINFF。
マトリックス全体については、図3の構築のために中間点のみが必要であり、それは他の半分の点に沈む。すべてl = rl = rl=rのポイントはシンクポイントに接続され、ソースポイントは[1、n] [1、n]です。[ 1 n ]
サンプルについて次の図が得られます

例:
3 4
1 3 L 10
1 3 R 3
1 2 L 1
1 2 R 1

図1
また、写真省略[2、3]→[2、2] [2、3] \ rightarrow [2、2][ 2 3 ][ 2 2 ]接続、そのフローはINF INFですI N F

最大フローから直接答えを見つけることができます

しかし、TLE

二重グラフ

デュアルグラフウィキペディア(https://en.wikipedia.org/wiki/Dual_graph)

デュアルグラフにより、グリッドネットワークグラフを最大フローの検索から最短経路の検索にすばやく変換できます。

二重グラフの説明については、情報を参照してください

元の画像に二重グラフを描画して取得します

二重グラフ

取得するデュアルグラフの有用な要素を分離する

二重グラフ
(図のエッジウェイトのないエッジはすべて0 0です。0

最短経路で迅速に解決できます

ACコード

#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int maxn = 510;

int n, m;
ll dis[maxn * maxn];
char si;
vector<pair<ll, int>> G[maxn * maxn];

void addedge(int u, int v, int cost) {
    
    
    G[u].push_back({
    
    cost, v});
}

ll dijkstra(int s, int t) {
    
    
    memset(dis, 0x3f, sizeof(dis));
    dis[s] = 0;
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
    q.push({
    
    0ll, s});
    while (!q.empty()) {
    
    
        ll u = q.top().second, c = q.top().first;
        q.pop();
        if (dis[u] < c)continue;
        for (auto i : G[u]) {
    
    
            ll cc = i.first, v = i.second;
            if (dis[v] > dis[u] + cc) {
    
    
                dis[v] = dis[u] + cc;
                q.push({
    
    dis[v], v});
            }
        }
    }
    return dis[t];
}

inline int id(int x, int y) {
    
    
    return x * (n + 3) + y;
}

void solve() {
    
    
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
    
    
        int u, v, w;
        char c;
        cin >> u >> v >> c >> w;
        if (c == 'L') {
    
    
            addedge(id(u, v), id(u, v + 1), w);
            addedge(id(u, v + 1), id(u, v), w);
        } else {
    
    
            addedge(id(u, v), id(u - 1, v), w);
            addedge(id(u - 1, v), id(u, v), w);
        }
    }

    for (int i = 1; i <= n; ++i) {
    
    
        addedge(id(0, 0), id(0, i), 0);
        addedge(id(i, n + 1), id(n + 1, n + 1), 0);
    }

    dijkstra(id(0, 0), id(n + 1, n + 1));
    if (dis[id(n + 1, n + 1)] >= 0x3f3f3f3f3f3f3f3f)
        cout << -1 << endl;
    else
        cout << dis[id(n + 1, n + 1)] << endl;
}

signed main() {
    
    
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef ACM_LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int test_index_for_debug = 1;
    char acm_local_for_debug;
    while (cin >> acm_local_for_debug) {
    
    
        if (acm_local_for_debug == '$') exit(0);
        cin.putback(acm_local_for_debug);
        if (test_index_for_debug > 20) {
    
    
            throw runtime_error("Check the stdin!!!");
        }
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    }
#else
    solve();
#endif
    return 0;
}

おすすめ

転載: blog.csdn.net/m0_43448982/article/details/107381264