L2-001 紧急救援 (25 分)

SPFA

#include<bits/stdc++.h>
#define rep(i,a,b) for(auto i = (a); i <= (b); ++i)
#define dep(i,a,b) for(auto i = (a); i >= (b); --i)
#define endl '\n'
#define debug(x) cout<<#x<<"="<<(x)<<endl;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const int N = 1e3 + 10;
const int mod = 1e9 + 10;
const int inf = 1e9;
string yes = "yes\n",no = "no\n";
inline ll add(ll a,ll b){
    
    return (a+=b)>=mod?a-mod:a;}
inline ll sub(ll a,ll b){
    
    return (a-=b)<0?a+mod:a;}
inline ll mul(ll a,ll b){
    
    return 1ll*a*a%mod;}

int n,m,s,d;
int a[N],D[N];
vector<P>G[N];
vector<int>ans;
int cnt,minn = inf,maxn = -1;
struct node{
    
    
    vector<int>id;
    int peo,u,len;
};
void input(){
    
    
    cin >> n >> m >> s >> d;
    rep(i,0,n-1){
    
    
        cin >> a[i];
    }
    rep(i,1,m){
    
    
        int u,v,w;
        cin >> u >> v >> w;
        G[u].push_back(P(v,w));
        G[v].push_back(P(u,w));
    }
}
void spfa(int s){
    
    
    memset(D,0x3f,sizeof D);
    D[s] = 0;
    queue<node>q;
    vector<int>v;
    v.push_back(s);
    q.push(node{
    
    v,a[s],s,0});
    while(!q.empty()){
    
    
        node now = q.front();q.pop();
        if(now.u == d){
    
    
            if(now.len<=minn){
    
    
                if(now.len == minn)cnt++;
                else cnt = 1;
                minn = now.len;
                if(now.peo>=maxn){
    
    
                    maxn = now.peo;
                    ans = now.id;
                }
            }
            continue;
        }
        for(auto it:G[now.u]){
    
    
            int v = it.first,w = it.second;
            if(now.len+w<=D[v]){
    
    
                D[v] = now.len+w;
                vector<int>tmp = now.id;
                tmp.push_back(v);
                q.push(node{
    
    tmp,now.peo+a[v],v,D[v]});
            }
        }
    }
}

void AC(){
    
    
    input();
    spfa(s);
    cout << cnt << " " << maxn << endl;
    for(int i = 0;i<(int)ans.size();i++){
    
    
        if(i)cout << " ";
        cout << ans[i];
    }
}

int main()
{
    
    
    int _=1;
    //scanf("%d",&_);
    while(_--){
    
    
        AC();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51152918/article/details/123273323