甲级pat-1087

#include
#include
#include
#include
#include
#include
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 cityToindex;
map 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;icost[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<"<>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>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;
}

你可能感兴趣的:(数据结构,算法学习)