1087. All Roads Lead to Rome (30)

来源:互联网 发布:蒙古舞演出服淘宝 编辑:程序博客网 时间:2024/05/21 19:37

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".

Sample Input:
6 7 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1
Sample Output:
3 3 195 97

HZH->PRS->ROM

题解:最短路径的变种,要求输出最短路径条数,总价值,并根据平均价值比较,输出路径

平均价值无法在dij中求得,所以这里先求出最短路径再用dfs比较平均值,取最短的。(总价值比较可在dij中先排除,但注意这里的条数只有最短路径的)

这次从十点开始做,还是刷到十一点四十五才ac,主要就是写的不仔细,调试了半天

AC代码:#include <iostream>#include <stdio.h>#include <algorithm>#include <string>#include <vector>#include <map>using namespace std;//查下哈希表映射 const int INF=1000000000;const int maxv=202;int n,K,total;int g[maxv][maxv],w[maxv]; int pathnum[maxv],wtotal[maxv],dis[maxv];  bool vis[maxv];  vector<int> pre[maxv]; void dij(int s){ fill(dis,dis+maxv,INF); dis[s]=0;    pathnum[s]=1;    wtotal[s]=w[s]; for(int i=0;i<n;i++){  // printf("testdij\n"); int u=-1,MIN=INF; for(int j=0;j<n;j++){ if(vis[j]==false&&dis[j]<MIN){ MIN=dis[j]; u=j; } } //cout<<"u="<<u<<endl; if(u==-1) return; vis[u]=true; for(int v=0;v<n;v++){ if(vis[v]==false&&g[v][u]!=INF){ if(dis[v]>dis[u]+g[u][v]){ dis[v]=dis[u]+g[u][v]; pathnum[v]=pathnum[u]; wtotal[v]=wtotal[u]+w[v]; pre[v].clear(); pre[v].push_back(u); } else if(dis[v]==dis[u]+g[u][v]){ pathnum[v]+=pathnum[u]; if(wtotal[v]<wtotal[u]+w[v]){  wtotal[v]=wtotal[u]+w[v];  pre[v].clear();  pre[v].push_back(u); } else if(wtotal[v]==wtotal[u]+w[v]){ pre[v].push_back(u); } } } } //printf("u:%d %d %d %d",) //for(int i=0;i<n;i++) printf("%d ",g[u][i]); cout<<endl; //for(int i=0;i<n;i++) printf("%d ",dis[i]);// cout<<endl; } }  vector<int> temppath,path; int maxh=0; void dfs(int v){ if(v==0){ temppath.push_back(v); //cout<<temppath.size()<<":size\n"; int avg=total/(temppath.size()-1); if(avg>maxh){ path=temppath; maxh=avg; } if(!temppath.empty()) temppath.pop_back(); return; } temppath.push_back(v); for(int i=pre[v].size()-1;i>=0;i--){ dfs(pre[v][i]); } //cout<<v<<endl; if(!temppath.empty()) temppath.pop_back();  }  map <string,int> stoi1; map<int,string> itos; int main(){ //freopen("E:input.txt","r",stdin); cin>>n>>K; string str; cin>>str; stoi1[str]=0; itos[0]=str; int s=0; for(int i=1;i<n;i++){ cin>>str; stoi1[str]=i; itos[i]=str; cin>>w[i]; if(str=="ROM") s=i; } string str1,str2; int m,n; fill(g[0],g[0]+maxv*maxv,INF); for(int i=0;i<K;i++){ cin>>str1>>str2; m=stoi1[str1]; n=stoi1[str2]; cin>>g[m][n]; g[n][m]=g[m][n]; dij(0); total=wtotal[s]; dfs(s);    printf("%d %d %d %d\n",pathnum[s],dis[s],wtotal[s],maxh);  for(int i=path.size()-1;i>=0;i--){ int m=path[i]; cout<<itos[m];  if(i!=0) cout<<"->"; }  } 

0 0
原创粉丝点击