PAT甲级-1003 Emergency (25 分)
题目链接#include<iostream>#include<algorithm>using namespace std;const int MAXV=510;const int INF=1000000000; //10^9int n,m,c1,c2,g[MAXV][MAXV],weigth[MAXV];int d[MAXV],w[MAXV],num[MAXV];bool
·
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXV=510;
const int INF=1000000000; //10^9
int n,m,c1,c2,g[MAXV][MAXV],weigth[MAXV];
int d[MAXV],w[MAXV],num[MAXV];
bool vis[MAXV]={false};
void Dijkstra(int s){
fill(d,d+MAXV,INF);
fill(w,w+MAXV,0);
fill(num,num+MAXV,0);
d[s]=0;
w[s]=weigth[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]&&d[j]<min){
min=d[j];
u=j;
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(!vis[v]&&g[u][v]!=INF){
if(d[u]+g[u][v]<d[v]){
d[v]=d[u]+g[u][v];
w[v]=w[u]+weigth[v];
num[v]=num[u];
}else if(d[u]+g[u][v]==d[v]){ //出现相同长度的路径
if(w[u]+weigth[v]>w[v]) w[v]=w[u]+weigth[v];
num[v]+=num[u];
}
}
}
}
}
int main(){
cin>>n>>m>>c1>>c2;
for(int i=0;i<n;i++)
cin>>weigth[i];
fill(g[0],g[0]+MAXV*MAXV,INF); //注意g[0]
int u,v;
for(int i=0;i<m;i++){
cin>>u>>v;
cin>>g[u][v];
g[v][u]=g[u][v];
}
Dijkstra(c1);
cout<<num[c2]<<" "<<w[c2]<<endl;
return 0;
}
更多推荐
已为社区贡献4条内容
所有评论(0)