P3074 [USACO13FEB] Milk Scheduling S(拓扑排序)

思路:

核心:拓扑排序+

复制代码
ans[x]=max(ans[x],ans[t]+f[x]);

注意比当前大才更新!!!

接下来几乎就是拓扑排序模板啦~

ACcode:

复制代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e4+10;
int f[N],n,m,ru[N],ans[N];
vector<int>v[N];
void topsort() {
	queue<int>q;
	for(int i=1; i<=n; i++) {
		if(ru[i]==0) {
			ans[i]=f[i];
			q.push(i);
		}
	}
	while(!q.empty()) {
		int t=q.front();
		q.pop();
		for(auto x:v[t]) {
			 ans[x]=max(ans[x],ans[t]+f[x]);
			ru[x]--;
			if(ru[x]==0)q.push(x);
		}
	}
}
void solve() {
	cin>>n>>m;
	for(int i=1; i<=n; i++) cin>>f[i];
	for(int i=1; i<=m; i++) {
		int x,y;
		cin>>x>>y;
		v[x].push_back(y);
		ru[y]++;
	}
	topsort();
	int mmax=-1;
	for(int i=1;i<=n;i++){
		mmax=max(mmax,ans[i]);
	}
	cout<<mmax<<"\n";
}
signed main() {

	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int tt=1;
	//cin>>tt;
	while(tt--) solve();
	return 0;
}
//3

over~

相关推荐
小年糕是糕手3 分钟前
【数据结构】算法复杂度
c语言·开发语言·数据结构·学习·算法·leetcode·排序算法
程序员-King.33 分钟前
day86——有效的字母异位词(LeetCode-242)
算法·字符串
xxxxxxllllllshi1 小时前
Java 代理模式深度解析:从静态到动态,从原理到实战
java·开发语言·笔记·算法·代理模式
Starry_hello world1 小时前
C++ 二分算法(1)
c++·算法·有问必答
小杨勇敢飞2 小时前
拼图小游戏开发日记 | Day3(已完结)
java·数据结构·算法
Guan jie2 小时前
10.6作业
数据结构·算法·排序算法
haidizym2 小时前
ssc-FinLLM 金融大模型 相关链接
人工智能·算法
Macre Aegir Thrym3 小时前
逻辑回归实践
算法·机器学习·逻辑回归
relis3 小时前
llama.cpp RMSNorm CUDA 优化分析报告
算法·llama
chaofa用代码打点酱油3 小时前
RAG 进化之路:传统 RAG 到工具与强化学习双轮驱动的 Agentic RAG
算法·llm