-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2637.cpp
More file actions
48 lines (38 loc) · 854 Bytes
/
2637.cpp
File metadata and controls
48 lines (38 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<vector>
#include<queue>
#define N 105
using namespace std;
int n, m, check[N], ans[N], used[N];
vector<pair<int, int>> d[N];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i;
cin >> n >> m;
for(i=0; i<m; i++){
int a, b, c;
cin >> a >> b >> c;
d[a].push_back({b, c});
check[a]=1;
used[b]++;
}
queue<int> q;
q.push(n);
ans[n]=1;
while(!q.empty()){
int head = q.front();
q.pop();
for(i=0; i<d[head].size(); i++){
int p=d[head][i].first;
int x=d[head][i].second;
ans[p]+=ans[head]*x;
if(--used[p]==0) q.push(p);
}
}
for(i=1; i<=n; i++){
if(!check[i]) cout << i << " " << ans[i] << endl;
}
return 0;
}