Instructions Arrangement
#include<bits/stdc++.h>
using namespace std;
const int Max=1001;
const int INF=INT_MAX;
struct Edge {
int to;
int length;
Edge(int x,int y):to(x),length(y) {
}
};
vector<Edge> graph[Max];
int indegree[Max]= {0};
int el[Max]= {0};
void CritialPath(int n) {
vector<int> topology;
queue<int> q;
for(int i=0; i<n; i++) {
if(indegree[i]==0) {
q.emplace(i);
el[i]=1;
}
}
while(!q.empty()) {
int u=q.front();
q.pop();
topology.emplace_back(u);
for(int i=0; i<graph[u].size(); i++) {
int v=graph[u][i].to;
int l=graph[u][i].length;
indegree[v]--;
if(indegree[v]==0) {
q.emplace(v);
}
el[v]=max(el[v],el[u]+l);
}
}
return ;
}
int main() {
int n,m;
while(cin>>n>>m) {
memset(graph,0,sizeof(graph));
for(int i=0; i<m; i++) {
int from,to,length;
cin>>from>>to>>length;
graph[from].emplace_back(Edge(to,length));
indegree[to]++;
}
CritialPath(n);
int answer=0;
for(int i=0; i<n; i++) {
answer=max(answer,el[i]);
}
cout<<answer<<endl;
}
return 0;
}
using namespace std;
const int Max=1001;
const int INF=INT_MAX;
struct Edge {
int to;
int length;
Edge(int x,int y):to(x),length(y) {
}
};
vector<Edge> graph[Max];
int indegree[Max]= {0};
int el[Max]= {0};
void CritialPath(int n) {
vector<int> topology;
queue<int> q;
for(int i=0; i<n; i++) {
if(indegree[i]==0) {
q.emplace(i);
el[i]=1;
}
}
while(!q.empty()) {
int u=q.front();
q.pop();
topology.emplace_back(u);
for(int i=0; i<graph[u].size(); i++) {
int v=graph[u][i].to;
int l=graph[u][i].length;
indegree[v]--;
if(indegree[v]==0) {
q.emplace(v);
}
el[v]=max(el[v],el[u]+l);
}
}
return ;
}
int main() {
int n,m;
while(cin>>n>>m) {
memset(graph,0,sizeof(graph));
for(int i=0; i<m; i++) {
int from,to,length;
cin>>from>>to>>length;
graph[from].emplace_back(Edge(to,length));
indegree[to]++;
}
CritialPath(n);
int answer=0;
for(int i=0; i<n; i++) {
answer=max(answer,el[i]);
}
cout<<answer<<endl;
}
return 0;
}
查看10道真题和解析