Cod sursa(job #2090571)

Utilizator lulian23Tiganescu Iulian lulian23 Data 18 decembrie 2017 14:27:56
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>
#define inf INT_MAX
#define nmax 16000 + 1

using namespace std;

char s[ 3 ][ nmax ];
int size[ 4 ], dist[ nmax ];

priority_queue < pair < int, int > > q;

int Abs(int x){
    if (x < 0)
      return -x;
    return x;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ifstream cin("base3.in");
    ofstream cout("base3.out");

    memset(dist, 0x3f, sizeof(dist));

    for (int i = 0; i < 3; i++){
        cin >> s[ i ];
        size[ i ] = strlen(s[ i ]);

        for (int j = 0; j < size[ i ]; j++)
            if (s[ i ][ j ] == '1'){
                int diff = Abs(size[ i ] - 2 * j - 1);
                if (size[ i ] < dist[ diff ]){
                  dist[ diff ] = size[ i ];
                  q.push(make_pair(diff, size[ i ]));
                }
            }
    }

    while (!q.empty()) {
        int diff = q.top().first, cost = q.top().second;
        q.pop();

        if (cost != dist[ diff ])
            continue;

        for (int i = 0; i < 3; i++) {
            int new_diff = abs(diff - size[ i ]), new_cost = cost + size[ i ];

            if (new_cost < dist[new_diff]) {
                dist[new_diff] = new_cost;
                q.push(make_pair(new_diff, new_cost));
            }
        }
    }

    if (dist[ 0 ] < 0x3f3f3f3f)
       cout << dist[ 0 ] << '\n';
    else
        cout << 0 << '\n';
}