78
题目看错了,应该输出1到n的最长路而不是1~n中的最长路
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 1505;
struct Node {
int w;
int v;
Node(int _v,int _w):v(_v),w(_w){}
};
vector<Node> Adj[maxn];
int n;
int dp[maxn];
bool vis[maxn];
int pre[maxn];
const int INF = -1e9;
int long_path(int x) {
if (vis[x]) return dp[x];
else {
vis[x] = true;
for (int i = 0; i < Adj[x].size(); i++) {
dp[x] = max(dp[x], Adj[x][i].w + long_path(Adj[x][i].v));
}
return dp[x];
}
}
int main() {
int m;
scanf("%d %d", &n,&m);
for (int i = 0; i < m; i++) {
int a, b, c,flag = 0;
scanf("%d %d %d", &a, &b, &c);
for (int j = 0; j < Adj[b].size(); j++) {
if (a == Adj[b][j].v) {
flag = 1;
if (Adj[b][j].w < c) {
Adj[b][j].w = c;
}
}
}
if(flag==0)
Adj[b].push_back(Node(a, c));
}
fill(dp, dp + maxn, INF);
//printf("%d\n\n", dp[2]);
vis[1] = true;
dp[1] = 0;
int ans = INF;
for (int i = 2; i <= n; i++) {
ans = max(ans, long_path(i));
}
if (ans > INF) printf("%d\n", ans);
else printf("-1\n");
}