社区讨论
91 pts 求调玄2关
P1807最长路参与者 3已保存回复 7
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 7 条
- 当前快照
- 1 份
- 快照标识符
- @mm0kr6mh
- 此快照首次捕获于
- 2026/02/24 20:21 2 周前
- 此快照最后确认于
- 2026/02/26 11:10 上周
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1505;
const int M=5e5+5;
int inline read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+(ch-'0'),ch=getchar();
return x*f;
}
queue<int> q;
int a[N],vis[N],c[N];
int n,m;
struct Edge{
int from,to,w;
}edge[M];
vector<Edge> v[M];
void topo_sort(){
for(int i=1;i<=n;i++){
if(c[i]==0)
q.push(i);
}
while(!q.empty()){
int now=q.front();
q.pop();
for(int j=0;j<v[now].size();j++){
c[v[now][j].to]--;
if(vis[now]){
if(a[v[now][j].to]<a[now]+v[now][j].w)
a[v[now][j].to]=a[now]+v[now][j].w;
vis[v[now][j].to]=1;
}
if(!c[v[now][j].to])
q.push(v[now][j].to);
}
}
}
signed main(){
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>edge[i].from>>edge[i].to>>edge[i].w;
v[edge[i].from].push_back(edge[i]);
c[edge[i].to]++;
}
a[n]=-1;
vis[1]=1;
topo_sort();
cout<<a[n];
return 0;
}
回复
共 7 条回复,欢迎继续交流。
正在加载回复...