Face questions
【Problem Description】
Given \ (n-\) points (number \ (. 1 \) ~ \ (n-\) ), \ (m \) unidirectional edges. Now all simultaneously zero from the start point of ignition, the entire pilot FIG.
Ignite a point will ignite all at this point as a starting point edge.
Each edge burning need to consume a certain time.
When all the edges of a point all burn to ignite this point.
How much time seeking complete combustion Photo needs.
[Input Format]
The first line of the input two integers \ (n-\) ( \ (2 \ n-Le \ Le 100 \) ), \ (m \) ( \ (. 1 \ Le m \ Le 1000 \) ).
Next \ (m \) lines each input three integers \ (X \) , \ (Y \) , \ (C \) expressed from a \ (X \) go to \ (Y \) unidirectional End edge combustion requires \ (C \) ( \ (0 <C \ 10000 Le \) ) units of time.
[Output format]
The time required for output data to ensure a solution, acyclic and FIG.
[Sample input]
4 5
1 2 2
1 3 4
2 4 4
2 3 1
3 4 5
[Sample output]
9
solution
That is to find the longest path of node 0 on the counter in all the degrees of FIG end.
program
#include <bits/stdc++.h>
using namespace std;
#define MAXN 120
int n, m, ideg[MAXN], odeg[MAXN], d[MAXN];
struct Edge {
int d, w;
Edge(int d = 0, int w = 0): d(d), w(w) {}
};
vector<Edge> adj[MAXN], rev[MAXN];
int dp(int j) {
if (d[j] > -1) return d[j];
int r = 0;
for (int i = 0; i < rev[j].size(); i++) {
r = max(r, dp(rev[j][i].d) + rev[j][i].w);
}
return d[j] = r;
}
int main() {
memset(d, -1, sizeof(d));
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, c; cin >> x >> y >> c;
adj[x].push_back(Edge(y, c));
rev[y].push_back(Edge(x, c));
odeg[x]++; ideg[y]++;
}
int r = 0;
for (int i = 1; i <= n; i++) {
if (odeg[i] == 0) {
r = max(r, dp(i));
}
}
cout << r << endl;
return 0;
}