博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pat1087. All Roads Lead to Rome (30)
阅读量:5265 次
发布时间:2019-06-14

本文共 3560 字,大约阅读时间需要 11 分钟。

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

 

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

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

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

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

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

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

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std; 11 map
city;//string-int 12 map
rcity; 13 map
> > 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
>u; 40 rcity[i]=u; 41 city[u]=i; 42 scanf("%d",&happ[i]); 43 } 44 45 /*for(i=0;i
>u>>v; 53 scanf("%d",&d); 54 //cout<
<<" "<
<<" "<
<
>::iterator it; 62 cout<<"i: "<
<
first<<" "<
second<
>::iterator it; 70 int next; 71 while(s!=city["ROM"]) 72 { 73 74 //cout<<"s: "<
<
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]
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
ss;138 while(p){139 ss.push(p);140 p=f[p];141 }142 cout<
"<

 

转载于:https://www.cnblogs.com/Deribs4/p/4785102.html

你可能感兴趣的文章
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
android:scaleType属性
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>
Linux中防火墙centos
查看>>
mysql新建用户,用户授权,删除用户,修改密码
查看>>
FancyCoverFlow
查看>>
JS博客
查看>>