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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值