L2001 紧急救援 (25分)

最短路计数+最短路径下的最大点权和+路径输出

const int N=510;
vector<PII> g[N];
int p[N];
int dist[N];
bool vis[N];
int cnt[N];
int save[N];
int pre[N];
int n,m;
int st,ed;

void dijkstra()
{
    memset(dist,0x3f,sizeof dist);
    memset(pre,-1,sizeof pre);
    priority_queue<PII,vector<PII>,greater<PII> > heap;
    dist[st]=0;
    heap.push({0,st});
    cnt[st]=1;
    save[st]=p[st];

    while(heap.size())
    {
        int t=heap.top().se;
        heap.pop();

        if(vis[t]) continue;
        vis[t]=true;

        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;
                cnt[j]=cnt[t];
                save[j]=save[t]+p[j];
                pre[j]=t;
                heap.push({dist[j],j});
            }
            else if(dist[j] == dist[t]+w)
            {
                cnt[j]+=cnt[t];
                if(save[t]+p[j] > save[j])
                {
                    save[j]=save[t]+p[j];
                    pre[j]=t;
                }
            }
        }
    }
}

void print_path(int x)
{
    if(pre[x] == -1)
    {
        cout<<x-1;
        return;
    }
    print_path(pre[x]);
    cout<<' '<<x-1;
}

int main()
{
    cin>>n>>m>>st>>ed;
    st++,ed++;

    for(int i=1;i<=n;i++) cin>>p[i];

    while(m--)
    {
        int a,b,c;
        cin>>a>>b>>c;
        a++,b++;
        g[a].pb({b,c});
        g[b].pb({a,c});
    }

    dijkstra();

    cout<<cnt[ed]<<' '<<save[ed]<<endl;

    print_path(ed);

    //system("pause");
}
原文地址:https://www.cnblogs.com/fxh0707/p/14029272.html