x

zhaojianchao 2024-05-04 10:10:53 2024-05-04 10:11:08 2 返回题目

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
    int next,w;
};
vector<node> v[100010],v1[100010];
queue<int> q;
bool iq[100010];
int n,m,s,ans,d[100010],d1[100010];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>s;
    memset(d,0x6f,sizeof(d));
    memset(d1,0x6f,sizeof(d1));
    for(int i=1;i<=m;i++){
        int u,g,w;
        cin>>u>>g>>w;
        v[u].push_back({g,w});
        v1[g].push_back({u,w});
    }
    q.push(s),iq[s]=1,d[s]=0;
    while(!q.empty()){
        int x=q.front();
        iq[x]=0,q.pop();
        for(auto p:v[x]){
            int u=p.next,w=p.w;
            if(d[x]+w<d[u]){
                d[u]=d[x]+w;
                q.push(u);
                iq[u]=1;
            }
        }
    }
    memset(iq,0,sizeof(iq));
    q.push(s),iq[s]=1,d1[s]=0;
    while(!q.empty()){
        int x=q.front();
        iq[x]=0,q.pop();
        for(auto p:v1[x]){
            int u=p.next,w=p.w;
            if(d1[x]+w<d1[u]){
                d1[u]=d1[x]+w;
                q.push(u);
                iq[u]=1;
            }
        }
    }
    for(int i=1;i<=n;i++) ans=max(ans,d[i]+d1[i]);
    cout<<ans;
    return 0;
}
{{ vote && vote.total.up }}