#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<map> using namespace std; const int maxn=1000; const int INF=100000000; int st,n,m,G[maxn][maxn]; int d[maxn],cost[maxn],pre[maxn],weight[maxn],num[maxn],pt[maxn]; bool vis[maxn]={false}; map<string,int> cityToindex; map<int,string> indexTocity; void Dijkstra(int st){ fill(d,d+maxn,INF); memset(cost,0,sizeof(cost)); memset(num,0,sizeof(num)); memset(pt,0,sizeof(pt)); for(int i=0;i<n;i++) pre[i]=i; d[st]=0; cost[st]=weight[st]; num[st]=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; 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]; cost[v]=cost[u]+weight[v]; num[v]=num[u]; pt[v]=pt[u]+1; pre[v]=u; } else if(d[u]+G[u][v]==d[v]){ num[v]+=num[u]; if(cost[u]+weight[v]>cost[v]){ cost[v]=cost[u]+weight[v]; pt[v]=pt[u]+1; pre[v]=u; } else if(cost[u]+weight[v]==cost[v]){ double uavg=1.0*(cost[u]+weight[v])/(pt[u]+1); double vavg=1.0*cost[v]/pt[v]; if(uavg>vavg){ pt[v]=pt[u]+1; pre[v]=u; } } } } } } } void printPath(int v){ if(v==0){ cout<<indexTocity[0]; return; } printPath(pre[v]); cout<<"->"<<indexTocity[v]; } int main(){ string start,city1,city2; cin>>n>>m>>start; cityToindex[start]=0; indexTocity[0]=start; for(int i=1;i<=n-1;i++){ cin>>city1>>weight[i]; cityToindex[city1]=i; indexTocity[i]=city1; } fill(G[0],G[0]+maxn*maxn,INF); for(int i=0;i<m;i++){ cin>>city1>>city2; int c1=cityToindex[city1]; int c2=cityToindex[city2]; cin>>G[c1][c2]; G[c2][c1]=G[c1][c2]; } Dijkstra(0); int rom=cityToindex["ROM"]; printf("%d %d %d %d\n",num[rom],d[rom],cost[rom],cost[rom]/pt[rom]); printPath(rom); return 0; }
甲级pat-1087
猜你喜欢
转载自blog.csdn.net/qq_36926514/article/details/80273829
今日推荐
周排行