#include #include #include #include using namespace std;

struct Node { int index; double state; Node(int i, double s) : index(i), state(s) {} };

struct Connection { int from; int to; double weight; Connection(int f, int t, double w) : from(f), to(t), weight(w) {} };

boolean compare(const pair<int, double>& a, const pair<int, double>& b) { return a.first < b.first; }

void neural_network(int n, int p, vector& initial_states, vector& thresholds, vector& connections) { // 构建神经网络的图 vector<vector<pair<int, double>>> graph(n); for (auto& conn : connections) { graph[conn.from - 1].push_back(make_pair(conn.to - 1, conn.weight)); }

// 初始化神经元的状态
vector<double> C(n, 0);
for (int i = 0; i < initial_states.size(); i++) {
    C[i] = initial_states[i];
}

// BFS 模拟神经网络的运行
vector<pair<int, double>> output;
queue<Node> q;
for (int i = 0; i < n; i++) {
    if (i < initial_states.size()) {
        q.push(Node(i, initial_states[i]));
    }
}

while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    
    for (auto& next : graph[cur.index]) {
        C[next.first] += next.second * cur.state;
        
        if (C[next.first] > 0) {
            if (next.first >= n - p) {
                output.push_back(make_pair(next.first + 1, C[next.first]));
            }
            q.push(Node(next.first, C[next.first]));
        }
    }
}

// 对结果进行排序并输出
sort(output.begin(), output.end(), compare);
if (!output.empty()) {
    for (auto& result : output) {
        cout << result.first << " " << result.second << endl;
    }
} else {
    cout << "NULL" << endl;
}

}

int main() { int n, p; cin >> n >> p; vector initial_states; vector thresholds; vector connections; for (int i = 0; i < n; i++) { int c, u; cin >> c >> u; initial_states.push_back(c); thresholds.push_back(u); } for (int i = 0; i < p; i++) { int from, to, weight; cin >> from >> to >> weight; connections.push_back(Connection(from, to, weight)); }

neural_network(n, p, initial_states, thresholds, connections);

return 0;

}

C++14 Implementation of Neural Network Simulation using BFS

原文地址: https://www.cveoy.top/t/topic/vlX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录