http://pat.zju.edu.cn/contests/pat-a-practise/1003
最短路
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<set> #include<map> #include<string> #include<queue> #include<stack> #include <iomanip> using namespace std; #define LL long long const double eps=1e-6; const int INF=0x3f3f3f3f; const int N=505; int road[N][N]; int a[N]; int dist[N]; int num[N]; int teams[N]; bool had[N]; void dijkstra(int n,int c1,int c2) { for(int i=0;i<n;++i) dist[i]=INF; memset(num,0,sizeof(num)); memset(teams,0,sizeof(teams)); memset(had,false,sizeof(had)); teams[c1]=a[c1]; num[c1]=1; dist[c1]=0; for(int w=0;w<n;++w) { int k=-1; for(int i=0;i<n;++i) if(!had[i]&&(k==-1||dist[i]<dist[k])) k=i; if(k==c2) break; had[k]=true; for(int i=0;i<n;++i) if(!had[i]) { if(dist[i]>dist[k]+road[k][i]) { dist[i]=dist[k]+road[k][i]; num[i]=num[k]; teams[i]=a[i]+teams[k]; }else if(dist[i]==dist[k]+road[k][i]) { num[i]+=num[k]; if(teams[i]<a[i]+teams[k]) teams[i]=a[i]+teams[k]; } } } } int main() { //freopen("data.in","r",stdin); int n,m,c1,c2; while(cin>>n>>m>>c1>>c2) { for(int i=0;i<N;++i) for(int j=0;j<N;++j) road[i][j]=INF; for(int i=0;i<n;++i) cin>>a[i]; while(m--) { int l,r,k; cin>>l>>r>>k; road[l][r]=road[r][l]=min(road[l][r],k); } dijkstra(n,c1,c2); cout<<num[c2]<<" "<<teams[c2]<<endl; } return 0; }