1087. All Roads Lead to Rome (30)

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 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, based on the given problem, here is a class diagram that represents the entities and their relationships: ``` +-----------------+ +-----------------+ | Country |<>----------| City | +-----------------+ +-----------------+ | | | | | - name | | - name | | - cities | | - roads | | | | | | + addCity() | | + addRoad() | | + removeCity() | | + removeRoad() | +-----------------+ +-----------------+ ^ | | | | | +----------------------+ | TravelingUnit | +----------------------+ | | | - type | | - driver | | - wheels/legs | | - weapons | | - canFire | | - maxSpeed | | | | + move() | | + captureByBandit() | +----------------------+ ^ | | | | | +---------------------+ | Driver | +---------------------+ | | | - nationality | | - name | | | +---------------------+ ``` Explanation: - The `Country` class represents a country and has a relationship with multiple `City` objects through the `cities` attribute. It also has attributes like `name` and methods like `addCity()` and `removeCity()` for managing cities. - The `City` class represents a city and has a relationship with multiple `Road` objects through the `roads` attribute. It has attributes like `name` and methods like `addRoad()` and `removeRoad()` for managing roads. - The `TravelingUnit` class represents a traveling unit and contains attributes like `type`, `driver`, `wheels/legs`, `weapons`, `canFire`, and `maxSpeed`. It has methods like `move()` for moving the unit and `captureByBandit()` for capturing the unit. - The `Driver` class represents a driver and contains attributes like `nationality` and `name`. It is associated with the `TravelingUnit` class. Note: This is a basic representation of the system based on the given problem statement. There might be additional classes or relationships required depending on the specific requirements of the system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值