2020-09-18

  1. #include <bits/stdc++.h>
    
    using namespace std;
    int n,m,s,d;
    const int maxn=550;
    int city[maxn][maxn],save[maxn];
    int num[maxn],cnt[maxn],pre[maxn];
    int dis[maxn];
    bool vis[maxn]= {false};
    const int inf = 0x3f3f3f3f;
    void dijkstra(int s)
    {
        int u;
        num[s] = 1;
        cnt[s] = save[s];
    
        for(int i = 0; i<n-1; i++)
        {
            int minx = inf;
            for(int j = 0; j<n; j++)
            {
                if(!vis[j]&&dis[j]<minx)
                {
                    minx = dis[j];
                    u = j;
                }
            }
            vis[u] = true;
            for(int k = 0; k<n; k++)
            {
                if(!vis[k]&&dis[k]>dis[u]+city[u][k])
                {
                    dis[k] = dis[u]+city[u][k];
                    num[k] = num[u];
                    cnt[k] = save[k] + cnt[u];
                    pre[k] = u;
                }
                else if(!vis[k]&&dis[k]==dis[u]+city[u][k])
                {
                    num[k] = num[u] + num[k];
                    if(cnt[u] + save[k]>cnt[k])
                    {
                        cnt[k] = save[k] + cnt[u];
                        pre[k] = u;
                    }
                }
            }
        }
    }
    void dfs(int v)
    {
        if(s==v)
        {
            cout<<s;
            return;
        }
        else
        {
            dfs(pre[v]);
            cout<<" "<<v;
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin>>n>>m>>s>>d;
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<n; j++)
            {
                if(i==j)
                    city[i][j] = 0;
                else
                    city[i][j] = inf;
            }
        }
        for(int i=0; i<n; i++)
        {
            cin>>save[i];
        }
        for(int i=0; i<m; i++)
        {
            int t1,t2,t3;
            cin>>t1>>t2>>t3;
            city[t1][t2]=city[t2][t1]=t3;
        }
        for(int i = 0; i<n; i++)
        {
            dis[i] = city[s][i];
        }
        dijkstra(s);
        cout<<num[d]<<" "<<cnt[d]<<endl;
        dfs(d);
        return 0;
    }
    

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页