首页 > 代码库 > oj--pat--a1003

oj--pat--a1003

邻接矩阵版。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXV=510;
const int INF=0x7fffffff;
int n,m,st,ed,G[MAXV][MAXV],nweight[MAXV];
int d[MAXV],w[MAXV],num[MAXV];
bool vis[MAXV]={false};

bool Dijkstra(int s){
    fill(d,d+MAXV,INF);
    memset(num,0,sizeof(num));
    memset(w,0,sizeof(w));
    d[s]=0;
    w[s]=nweight[s];
    num[s]=1;
    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 false;/*no-connected--->false */
        
        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]+nweight[v];
                    num[v]=num[u];
                }
                else if(d[u]+G[u][v]==d[v]){
                    if(w[u]+nweight[v]>w[v]){
                        w[v]=w[u]+nweight[v];
                    }
                    num[v]+=num[u];
                }
            }
        }
    }
}

int main(){
    scanf("%d %d %d %d",&n,&m,&st,&ed);
    for(int i=0;i<n;i++) scanf("%d",&nweight[i]);
    fill(G[0],G[0]+MAXV*MAXV,INF);
    int tmp1,tmp2;
    for(int i=0;i<m;i++) {
        scanf("%d %d",&tmp1,&tmp2);
        scanf("%d",&G[tmp1][tmp2]);
        G[tmp2][tmp1]=G[tmp1][tmp2];
    }
    bool flag=Dijkstra(st);
    if(flag)
        printf("%d %d\n",num[ed],w[ed]);
    else printf("no");    ///////
    return 0;
}    

 

oj--pat--a1003