甲级pat 1003

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1001;
const int INF=10000000;
int n,m,s,t;
//d[]表示的是起始点到每个定点的最短路径,G[][]表示的是每两个定点之间的路径长度,cost[]表示的是每个定点权值的大小
int d[maxn],G[maxn][maxn],cost[maxn],w[maxn],num[maxn]; 
bool vis[maxn]={false};

void Dijkstra(int s){
    fill(d,d+maxn,INF);
    memset(num,0,sizeof(num));
    memset(w,0,sizeof(w));
    w[s]=cost[s];
    num[s]=1;
    d[s]=0;
    for(int i=0;i<n;i++){
        int u=-1,MIN=INF;
        for(int j=0;j<n;j++){
            if(vis[j]==false&&d[j]<MIN){
                u=j;
                MIN=d[j];
            }
        }
        if(u==-1)
            return;
        vis[u]=true;
        for(int v=0;v<n;v++){
            if(vis[v]==false&&G[u][v]!=INF){
                if(d[u]+G[u][v]<d[v]){
                    d[v]=d[u]+G[u][v];
                    w[v]=w[u]+cost[v];
                    num[v]=num[u];  // 更新数量
                }else if(d[u]+G[u][v]==d[v]){
                    if(w[v]<cost[v]+w[u])
                        w[v]=w[u]+cost[v];
                    num[v]+=num[u];
                }
            }
        }
    }
}
int main(){
    int u,v,p;
    scanf("%d %d %d %d",&n,&m,&s,&t);
    for(int i=0;i<n;i++)
        scanf("%d",&cost[i]);
    fill(G[0],G[0]+maxn*maxn,INF);
    for(int i=0;i<m;i++){
        scanf("%d %d %d",&u,&v,&p);
        G[u][v]=p;
        G[v][u]=p;
    }
    Dijkstra(s);
    printf("%d %d\n",num[t],w[t]);
    return 0;
}
/*5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1*/

猜你喜欢

转载自blog.csdn.net/qq_36926514/article/details/80371486
今日推荐