Python Selenium自動化獲取頁面資訊的方法
阿新 • • 發佈:2020-09-01
spfa
const int N=1510; vector<PII> g[N]; LL dist[N]; bool vis[N]; int n,m; void spfa() { memset(dist,-0x3f,sizeof dist); queue<int> q; q.push(1); dist[1]=0; while(q.size()) { int t=q.front(); q.pop(); vis[t]=false; for(int i=0;i<g[t].size();i++) { int j=g[t][i].fi,w=g[t][i].se; if(dist[j] < dist[t] + w) { dist[j]=dist[t]+w; if(!vis[j]) { q.push(j); vis[j]=true; } } } } } int main() { cin>>n>>m; while(m--) { int a,b,c; cin>>a>>b>>c; g[a].push_back({b,c}); } spfa(); if(dist[n] < -5e9) cout<<-1<<endl; else cout<<dist[n]<<endl; //system("pause"); }
注意到原圖為DAG,不由聯想到拓撲排序,至於最長路只需dp就可以了
- 注意只能初始化1號店距離為0,因為是求1號點到n號點的最長路
const int N=1510; vector<PII> g[N]; LL f[N]; int din[N]; int n,m; void topo() { memset(f,-0x3f,sizeof f); queue<int> q; for(int i=1;i<=n;i++) if(!din[i]) q.push(i); f[1]=0; while(q.size()) { int t=q.front(); q.pop(); for(int i=0;i<g[t].size();i++) { int j=g[t][i].fi,w=g[t][i].se; f[j]=max(f[j],f[t]+w); if(--din[j] == 0) q.push(j); } } } int main() { cin>>n>>m; while(m--) { int a,b,c; cin>>a>>b>>c; g[a].push_back({b,c}); din[b]++; } topo(); if(f[n] < -5e9) cout<<-1<<endl; else cout<<f[n]<<endl; //system("pause"); }