神经网络
思路
- 算法: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;
}