P1038 神经网络

#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
struct node
{
	int situation, yuzhi;
}ns[maxn];
std::vector<int> g[maxn];
int main() {
	int n, p;
	cin >> n >> p;
	queue<int> q;
	int vis[maxn];
	bool qidian[maxn];
	memset(vis, 0, sizeof(vis));
	for(int i = 1; i <= n; i++) {
		cin >> ns[i].situation >> ns[i].yuzhi;
		if(ns[i].situation) {
			q.push(i);
			qidian[i] = 1;
			vis[i] = 1;
		}
	}
	int weigh[maxn][maxn];
	for(int i = 1; i <= p; i++) {
		int x, y, z;
		cin >> x >> y >> z;
		g[x].push_back(y);
		weigh[x][y] = z;
	}
	while(!q.empty()) {
		int x = q.front();
		q.pop();
		if(!qidian[x]) ns[x].situation -= ns[x].yuzhi;
		if(ns[x].situation > 0) {
			vector<int>::iterator it;
			for(it = g[x].begin(); it != g[x].end(); it++) {
				ns[*it].situation += weigh[x][*it] * ns[x].situation;
				if(!vis[*it]) {
					q.push(*it);
					vis[*it] = 1;
				}
			}
		}
	}
	bool flag = true;
	for(int i = 1; i <= n; i++) {
		if(g[i].empty() && ns[i].situation > 0) {
			flag = false;
			cout << i << ' ' << ns[i].situation << endl;
		}
	}
	if(flag) cout << "NULL"; 
}

posted on 2016-11-13 15:43  蒟蒻konjac  阅读(183)  评论(0编辑  收藏  举报