PAT1030. Travel Plan (30)
#include <iostream> #include <limits> #include <vector> using namespace std; int n,m,s,d; int cityMap[500][500]; int costMap[500][500]; #define INF numeric_limits<int>::max() int dp[500]; int vis[500]; int costDp[500]; vector<int> route[500]; void djkstra(){ for(int i=0;i<n;i++) dp[i]=INF; dp[s]=0;route[s].push_back(s); for(int i=0;i<n;i++){ int x,m=INF; for(int j=0;j<n;j++){ if(!vis[j]&&dp[j]<m){ m=dp[x=j]; } } vis[x]=1; for(int j=0;j<n;j++){ if(cityMap[x][j]!=0){ if(dp[j]>dp[x]+cityMap[x][j]){ dp[j]=dp[x]+cityMap[x][j]; costDp[j]=costDp[x]+costMap[x][j]; route[j]=route[x]; route[j].push_back(j); }else if(dp[j]==dp[x]+cityMap[x][j]){ if(costDp[j]>(costDp[x]+costMap[x][j])){ costDp[j]=costDp[x]+costMap[x][j]; route[j]=route[x]; route[j].push_back(j); } } } } } } int main(){ cin>>n>>m>>s>>d; int c1,c2; for(int i=0;i<m;i++){ cin>>c1>>c2; cin>>cityMap[c1][c2]>>costMap[c1][c2]; cityMap[c2][c1]=cityMap[c1][c2]; costMap[c2][c1]=costMap[c1][c2]; } djkstra(); vector<int> tmp=route[d]; vector<int>::iterator ii=tmp.begin(); while(ii!=tmp.end()){ cout<<(*ii)<<" "; ii++; } cout<<dp[d]<<" "<<costDp[d]; return 0; }