题解 | #神经网络#

神经网络

https://ac.nowcoder.com/acm/problem/16679

神经网络

思路

  • 算法:BFS
  • 注意:题目描述错误,数据范围n开20是不够的,我开了1000才过;最后输出的不是非零状态,而是大于0的状态

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
const double eps = 1e-6;
const int N = 1010, M = N * N;
int n, m;
int h[N], e[M], ne[M], w[M], idx;
int U[N], C[N];
bool st_out[N], st_in[N], st[N];
vector<int> output;

void add(int a, int b, int c)
{
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx++;
}

void bfs()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        if (!st_in[i])
        {
            q.push(i);
            st[i] = true;
        }
        else // 不是输入层, 状态均先减去阈值
        {
            C[i] -= U[i];
        }
    }

    while (!q.empty())
    {
        int t = q.front();
        q.pop();

        if (C[t] > 0)
            for (int i = h[t]; i != -1; i = ne[i])
            {
                int j = e[i];
                C[j] += C[t] * w[i];

                if (!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    memset(h, -1, sizeof h);
    cin >> n >> m;

    for (int i = 1; i <= n; i++)
    {
        cin >> C[i] >> U[i];
    }
    while (m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
        st_out[a] = true;
        st_in[b] = true;
    }

    for (int i = 1; i <= n; i++)
    {
        if (!st_out[i])
            output.push_back(i);
    }

    bfs();

    bool flag = false;

    for (auto i : output)
    {
        if (C[i] > 0)
        {
            flag = true;
            break;
        }
    }

    if (flag)
    {
        for (auto i : output)
        {
            if (C[i] > 0)
                cout << i << ' ' << C[i] << '\n';
        }
    }
    else
        cout << "NULL";

    return 0;
}
全部评论
确实是1000,我一开始t了,我还不知道哪里错了,哈哈哈
点赞 回复 分享
发布于 2022-04-27 13:23

相关推荐

写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
07-02 10:39
门头沟学院 Java
Steven267:说点真实的,都要秋招了,还没有实习,早干嘛去了,本来学历就差,现在知道急了,而且你这个简历完全可以写成一页,劣势太大了,建议转测试
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-30 11:32
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务