Facebook
From Gruff Agouti, 1 Year ago, written in C++.
This paste is a reply to Untitled from Lousy Partdridge - go back
Embed
Viewing differences between Untitled and Re: Untitled
#include 
#include 
#include 

struct Edge {
    Edge(int dest, 

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int we) {
        this->dest = dest;
        this->weight = we;
    }
    int dest, weight;
};

const 
nn, mm;
int MAXN = 54321;
bool used[MAXN];
1e6 * 2;


int count = 0, tin[MAXN], fup[MAXN];

void DfsBriges(int vertex, 
IndexToNumber(int ii, int parent, std::vector> &graph, int &answ) jj) {
    used[vertex] = true;
    tin[vertex] = count;
    fup[vertex] = count++;
    
return ii * mm + jj;
}

int BitByIndex(int table, int ii, int jj) {
    return (table >> IndexToNumber(ii, jj)) % 2;
}

bool Check(int table) {
    
for (Edge edge:graph[vertex]) (int ii = 0; ii < nn; ++ii) {
        int dest for (int jj edge.dest;
        if (dest == parent) 
0; jj < mm; ++jj) {
            continue;
if (ii > 0) {
                if (BitByIndex(table, ii, jj) == BitByIndex(table, ii - 1, jj)) {
                    return false;
                }
            }
            if (jj > 0) {
                if (BitByIndex(table, ii, jj) == BitByIndex(table, ii, jj - 1)) {
                    return false;
                }
            }
        }
        if (used[dest])     }
    return true;
}


int XorByIndex(int table, int ii, int jj) {
    return table xor (1 << IndexToNumber(ii, jj));
}

void PushNew(std::vector &used, std::queue> &qu, int table, int num) {
    for (int ii = 0; ii < nn; ++ii) {
        for (int jj = 0; jj < mm; ++jj) 
{
            fup[vertex] = std::min(fup[vertex], fup[dest]);
        } else {
            DfsBriges(dest, vertex, graph, answ);
            fup[vertex] = std::min(fup[vertex], fup[dest]);
            
if (fup[dest] > tin[vertex]) (ii + 1 != nn) {
                answ int new_table std::min(answ, edge.weight);
XorByIndex(table, ii, jj);
                new_table = XorByIndex(new_table, ii + 1, jj);

                if (!used[new_table]) {
                    used[new_table] = true;
                    qu.push({new_table, num + 1});
                }
            }

            if (jj + 1 != mm) {
                int new_table = XorByIndex(table, ii, jj);
                new_table = XorByIndex(new_table, ii, jj + 1);

                if (!used[new_table]) {
                    used[new_table] = true;
                    qu.push({new_table, num + 1});
                }
            }
        }
    }
}

int Bfs(int table) {
    std::vector used(MAXN);
    std::queue> qu;
    qu.push({table, 0});
    used[table] = true;
    while (!qu.empty()) {
        auto item = qu.front();
        if (Check(item.first)) {
            return item.second;
        }
        qu.pop();
        PushNew(used, qu, item.first, item.second);
    }
    return -1;
}

int main() {
    int nn, mm;\n    std::cin >> nn >> mm;
    std::vector> graph(nn + 1);
    
int table = 0;
    
for (int i = 0; i < mm; nn; ++i) {
        int source, dest, weight;
        
for (int j = 0; j < mm; ++j) {
            char sim;
            
std::cin >> source >> dest >> weight;
        graph[source].push_back(Edge(dest, weight));
        graph[dest].push_back(Edge(source, weight));
sim;
            if (sim == '1') {
                table = table | (1 << (i * mm + j));
            }
        }
    }
    int answ = 1e9;
    DfsBriges(1, -1, graph, answ);
    if (answ < 1e9) {
        
std::cout << answ;
    } else {
        std::cout << -1;
    }
Bfs(table);
}