【PAT】【Advanced Level】1087. All Roads Lead to Rome (30)

来源:互联网 发布:淘宝qq群推广抽成 编辑:程序博客网 时间:2024/05/22 03:39

1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 97HZH->PRS->ROM

原题链接:
https://www.patest.cn/contests/pat-a-practise/1087

https://www.nowcoder.com/pat/5/problem/4315

思路:

DFS:

PAT可过,牛客网超时

DJ:

待补充。。

CODE:

DFS:

#include<iostream>#include<cstring>#include<string>#include<vector>#include<cstdio>#include<map>#include<algorithm>#define N 210#define MX 1000000000using namespace std;typedef struct S{int mx;int sum;vector<int> pa; };vector<S> ch;map<string,int> ma;int n,m;string sta;string name[N];int hap[N];int dist[N][N];int path[N];int minc=MX;int flag[N];void dfs(int now,int cost,int flo){if (cost>minc) return;if (now==ma["ROM"]){if (cost<=minc){//cout<<cost<<" "<<minc<<" "<<now<<endl;if (cost<minc) ch.clear();minc=cost;S ne;ne.mx=cost;ne.sum=0;for(int i=0;i<flo;i++){ne.pa.push_back(path[i]);ne.sum+=hap[path[i]];}ch.push_back(ne);}return ;}for (int i=0;i<n;i++){if (flag[i]==1) continue;if (dist[now][i]==MX) continue;flag[i]=1;path[flo]=i;dfs(i,cost+dist[now][i],flo+1);flag[i]=0;path[flo]=0;}return;}bool cmp(S a,S b){if (a.sum==b.sum){return (a.sum/(a.pa.size()))>(b.sum/(b.pa.size()));}elsereturn a.sum>b.sum;}int main(){cin>>n>>m>>sta;//cout<<n<<" "<<m<<" "<<sta<<endl;for (int i=0;i<n;i++){dist[i][i]=0;for (int j=i+1;j<n;j++)dist[i][j]=dist[j][i]=MX;}ma[sta]=0;name[0]=sta;hap[0]=0;for (int i=1;i<n;i++){cin>>name[i]>>hap[i];//cout<<name[i]<<" "<<hap[i]<<endl;ma[name[i]]=i;}for (int i=0;i<m;i++){//cout<<i<<endl;string a,b;int c;cin>>a>>b>>c;dist[ma[a]][ma[b]]=dist[ma[b]][ma[a]]=c;}memset(flag,0,sizeof(flag));path[0]=ma[sta];flag[ma[sta]]=1;dfs(ma[sta],0,1);/*cout<<ch.size()<<endl;for (int i=0;i<ch.size();i++){cout<<ch[i].mx<<" "<<ch[i].sum<<endl;for (int j=0;j<ch[i].pa.size();j++){cout<<name[ch[i].pa[j]]<<" ";}cout<<endl;}*/sort(ch.begin(),ch.end(),cmp);cout<<ch.size()<<" "<<ch[0].mx<<" "<<ch[0].sum<<" "<<ch[0].sum/(ch[0].pa.size()-1)<<endl;for (int i=0;i<ch[0].pa.size();i++){if (i!=0) cout<<"->";cout<<name[ch[0].pa[i]];}return 0;}



原创粉丝点击