P1087. All Roads Lead to Rome (30)
来源:互联网 发布:微博域名格式 编辑:程序博客网 时间:2024/05/02 10:48
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 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1
3 3 195 97HZH->PRS->ROM
#include <iostream>#include <cstring>#include <vector>#include <algorithm>#include <cstdio>#include <map>using namespace std;map<int,string>dsa;map<string,int> asd;int grond[201][201];int flag[201];int dist[201];int val[201];vector<int> last[201];vector<int> rem;int m,n,fin;double ans,pin;int num1;void dfs(int s,vector<int> d){ if(s == 0){ num1++; int len = d.size(); double www = 0; for(int i = 0;i<len;i++){ www += val[d[i]]; } if(www == ans){ if(www/len > pin){ ans=www; pin = www/(len-1); rem = d; } } else if(www > ans) { ans=www; pin = www/(len-1); rem = d; } } int len = last[s].size(); if(len<1) return; for(int i =0 ;i<len;i++){ if(!flag[last[s][i]]){ flag[last[s][i]]=1; d.push_back(last[s][i]); dfs(last[s][i],d); d.pop_back(); flag[last[s][i]]=0; } }}void dis(){ memset(flag,0,sizeof(flag)); dist[0]=0; flag[0]=1; for(int i =1;i<n;i++){ dist[i]=grond[0][i]; if(dist[i]!=9999) last[i].push_back(0); } for(int i = 1;i<n;i++){ int k = -1; for(int j = 1;j<n;j++){ if(!flag[j] && (k==-1 || dist[k]>dist[j])) k=j; } if(k==-1) break; flag[k]=1; for(int j = 0;j<n;j++){ if(!flag[j] && dist[j]>dist[k]+grond[k][j]){ dist[j]=dist[k]+grond[k][j]; last[j].clear(); last[j].push_back(k); } else if(!flag[j] && dist[j]==dist[k]+grond[k][j]){ last[j].push_back(k); } } } ans = 0; pin = 0; memset(flag,0,sizeof(flag)); int now = fin; int v = val[fin]; vector<int> sd; sd.push_back(fin); num1 = 0; dfs(now,sd); int lll = rem.size(); int ppppp = (int)pin; cout<<num1<<" "<<dist[fin]<<" "<<ans<<" "<<ppppp<<endl; for(int i = lll-1;i>0;i--){ cout<<dsa[rem[i]]<<"->"; } cout<<dsa[rem[0]]<<endl;}int main(){ string s; cin>>n>>m>>s; asd[s]=0; dsa[0]=s; int tem; for(int i = 1;i<n;i++){ cin>>s>>tem; if(s=="ROM") fin = i; asd[s]=i; dsa[i]=s; val[i]=tem; } string d; memset(grond,9999,sizeof(grond)); for(int i =0;i<m;i++){ cin>>s>>d>>tem; int aa = asd[s]; int bb = asd[d]; grond[aa][bb]=tem; grond[bb][aa]=tem; } dis(); return 0;}
- P1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- go语言实现九九乘法表
- 快速排序
- 视频监控安防平台-GB28181-20位地址编码解析
- Add Two Numbers
- 51Nod 1014 X^2 Mod P
- P1087. All Roads Lead to Rome (30)
- 高德地图自定义marker图片相关
- ZYNQ7000 AXI 总线相关介绍(待补充)
- Tomcat 7.0 'javax.el.ELException' 的解决方式
- Jenkins 打包 spring boot 项目 丢失 配置文件
- 一个综合示例:通讯录管理系统
- 斯坦福大学深度学习公开课cs231n学习笔记(2)线性分类器及最优化
- 汇编语言寄存器的英文全称中英对照表
- 计算机组成原理——总线相关例题