pat1087. All Roads Lead to Rome (30)

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 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


提交代码

pat中类似的题目做得比较多,现在总结一下:

1.最大和初始距离为-1。这个条件不管是dis[],还是mindis,在使用Dijstra的时候,更新邻边当前最短路和求当前最短路的点的时候要注意,边不可到初始点的点先剔除。

2.对比string要比对比int慢很多,所要转换映射。

3.相邻点的数学关系要分析清楚。

4.最好加vis,可以更清晰。

5.初始点的初始化不要忘记!!

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<stack>
  4 #include<algorithm>
  5 #include<iostream>
  6 #include<stack>
  7 #include<set>
  8 #include<map>
  9 #include<vector>
 10 using namespace std;
 11 map<string,int> city;//string-int
 12 map<int,string> rcity;
 13 map<int,vector<pair<int,int> > > edge;
 14 int dis[205],path[205],hcount[205],happ[205],fstep[205],f[205];
 15 bool vis[205];
 16 int main()
 17 {
 18     //freopen("D:\\INPUT.txt","r",stdin);
 19     int n,k,i,d,s;
 20     string st,u,v;
 21     scanf("%d %d",&n,&k);
 22     memset(dis,-1,sizeof(dis));//每个点的最短路长
 23     memset(hcount,-1,sizeof(hcount));//幸福指数之和
 24     memset(vis,false,sizeof(vis));//是否计算过最短路径
 25     memset(path,0,sizeof(path));//前一个点
 26     memset(fstep,0,sizeof(fstep));//到这个点需要几步
 27     cin>>st;//起始城市
 28     city[st]=0;//编号
 29     rcity[0]=st;//反编号
 30     happ[0]=0;
 31     dis[0]=0;
 32     hcount[0]=0;
 33     fstep[0]=0;
 34     path[0]=1;//init
 35     f[0]=0;
 36     for(i=1; i<n; i++)
 37     {
 38         f[i]=i;
 39         cin>>u;
 40         rcity[i]=u;
 41         city[u]=i;
 42         scanf("%d",&happ[i]);
 43     }
 44
 45     /*for(i=0;i<n;i++){
 46         cout<<rcity[i]<<endl;
 47     }*/
 48
 49
 50     for(i=0; i<k; i++)
 51     {
 52         cin>>u>>v;
 53         scanf("%d",&d);
 54         //cout<<u<<" "<<v<<" "<<d<<endl;
 55         edge[city[u]].push_back(make_pair(city[v],d));
 56
 57         edge[city[v]].push_back(make_pair(city[u],d));
 58     }
 59
 60     /*for(i=0;i<n;i++){
 61         vector<pair<int,int> >::iterator it;
 62         cout<<"i:  "<<i<<endl;
 63         for(it=edge[i].begin(); it!=edge[i].end(); it++){
 64             cout<<it->first<<" "<<it->second<<endl;
 65         }
 66     }*/
 67
 68     s=0;
 69     vector<pair<int,int> >::iterator it;
 70     int next;
 71     while(s!=city["ROM"])
 72     {
 73
 74         //cout<<"s: "<<s<<endl;
 75
 76         vis[s]=true;
 77         for(it=edge[s].begin(); it!=edge[s].end(); it++) //update
 78         {
 79             next=it->first;
 80             if(dis[next]==-1||dis[next]>dis[s]+it->second)
 81             {
 82                 dis[next]=dis[s]+it->second;
 83                 hcount[next]=hcount[s]+happ[next];
 84                 path[next]=path[s];
 85                 fstep[next]=fstep[s]+1;
 86                 f[next]=s;
 87             }
 88             else
 89             {
 90                 if(dis[next]==dis[s]+it->second)
 91                 {
 92                     path[next]+=path[s];//
 93                     if(hcount[next]<hcount[s]+happ[next])
 94                     {
 95                         hcount[next]=hcount[s]+happ[next];
 96                         fstep[next]=fstep[s]+1;
 97                         f[next]=s;
 98                     }
 99                     else
100                     {
101                         if(hcount[next]==hcount[s]+happ[next])
102                         {
103                             if(fstep[next]>fstep[s]+1)
104                             {
105                                 fstep[next]=fstep[s]+1;
106                                 f[next]=s;
107                             }
108                         }
109                     }
110                 }
111             }
112         }
113
114         /*for(i=1;i<n;i++){
115             cout<<"i:  "<<i<<"  "<<dis[i]<<endl;
116         }*/
117
118         int mindis=-1,minnum;
119         for(i=1;i<n;i++)//find the min
120         {
121             if(dis[i]==-1){//如果当前边到不了初始点,直接pass
122                 continue;
123             }
124             if(!vis[i]&&(mindis==-1||(dis[i]<mindis))){
125                 //cout<<"ii:  "<<i<<" "<<dis[i]<<endl;
126                 mindis=dis[i];
127                 minnum=i;
128             }
129         }
130
131         //cout<<"minnum: "<<minnum<<" "<<dis[minnum]<<endl;
132
133         s=minnum;
134     }
135     printf("%d %d %d %d\n",path[s],dis[s],hcount[s],hcount[s]/fstep[s]);
136     int p=s;
137     stack<int> ss;
138     while(p){
139         ss.push(p);
140         p=f[p];
141     }
142     cout<<rcity[p];
143     while(!ss.empty()){
144         cout<<"->"<<rcity[ss.top()];
145         ss.pop();
146     }
147     cout<<endl;
148     return 0;
149 }
时间: 2024-11-03 20:55:09

pat1087. All Roads Lead to Rome (30)的相关文章

[PAT]1087. All Roads Lead to Rome (30)

/************************************************************** 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 suppos

1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise

题目信息 1087. All Roads Lead to Rome (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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. I

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 Spec

PAT1087. All Roads Lead to Rome

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, th

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

暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace st

PAT甲级 All Roads Lead to Rome (dijkstra+dfs回溯)

All Roads Lead to Rome 本题需要记录一共有几条最短路径,并输出最短路中开心值最大的路径或者开心值相等的情况下输出平均开心值最大的路径. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <algori

Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)

Pat1087代码 题目描述: 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. F

PAT 1087 All Roads Lead to Rome

1 #include <cstdio> 2 #include <climits> 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <queue> 7 #include <unordered_map> 8 #include <algorithm> 9 10 using namespace std; 11 12 typ

PAT (Advanced Level) 1087 All Roads Lead to Rome

题解   最短路径经典题型.套最短路的板子再加上额外的要求就可以了(说起来好简单).SPFA也行,Dijkstra也可以.这里我用的是SPFA.因为题目要求,将地名和其对应的数字用map映射一下,这样方便处理. same[i]代表到达地点 i 有几种路径: dist[i]代表从起点到地点 i 的最短距离: happy[i]代表从起点到地点 i 的幸福值: cnt[i]代表从起点到地点 i 需要经过几个城市: ans[i]代表从哪个地点到达了地点 i : 代码 #include<bits/stdc