萌新第一天学C艹,性感广搜73分在线求条,很急
  • 板块P1807 最长路
  • 楼主2011Andy
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/1/22 15:47
  • 上次更新2024/1/22 18:59:55
查看原帖
萌新第一天学C艹,性感广搜73分在线求条,很急
660871
2011Andy楼主2024/1/22 15:47
#include<bits/stdc++.h>
using namespace std;
const int M = 5e4 + 5;
const int W = 1e5 + 5;
int n , m , cnt;
int times[M] , ans[M] , h[M] , ne[M] , e[M] , ind[M] , oud[M] , w[W];//这里都往大开了 
void add(int a , int b , int c){
	e[cnt] = b , w[cnt] = c , ne[cnt] = h[a] , h[a] = cnt++;
}
int main(){
	cin >> n >> m;
	memset(h , -1 , sizeof(h));
	memset(ans , -1e9 , sizeof(ans));
	for(int i = 1 ; i <= m ; i++){
		int a , b , c;
		cin >> a >> b >> c;
		add(a , b , c);
		ind[b]++;
		oud[a]++;
	} 
	queue<int > q;
	for(int i = 2 ; i <= n ; i++){
		if(!ind[i]){
			q.push(i);
		}
	}
	while(!q.empty()){
		int t = q.front();
		q.pop();
		for(int i = h[t] ; i != -1 ; i = ne[i]){
			int k = e[i];
			ind[k]--;
			if(!ind[k]){
				q.push(k);
			}
		}
	}
	q.push(1);
	ans[1] = 0;
	while(!q.empty()){
		int t = q.front();
		q.pop();
		for(int i = h[t] ; i != -1 ; i = ne[i]){
			int k = e[i];
			ind[k]--;
			ans[k] = max(ans[k] , ans[t] + w[i]);
			if(!ind[k]){
				q.push(k);
			}
		}
	}
	if(ans[n] == -1e9) cout << -1;
	else cout << ans[n];
    return 0;
}
2024/1/22 15:47
加载中...