Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)
来源:互联网 发布:linux shell脚本 echo 编辑:程序博客网 时间:2024/06/06 10:47
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. 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
AC代码:#include<cstdio>#include<iostream>#include<string>#include<map>#include<vector>#define MAXN 205#define MAX 0x0FFFFFFFusing namespace std;int n,k;int matrix[MAXN][MAXN];string name[MAXN];int happiness[MAXN];map<string,int> m;int dis[MAXN];int visited[MAXN];int sumHapp=0,avgHapp=0,routeSum=0,shortest=MAX;vector<int> resPath;void dijkstra(int s){int i,j,k,temp;for(int i=0;i<n;i++){dis[i]=matrix[s][i];visited[i]=0;}dis[s]=0;visited[s]=1;for(int i=1;i<n;i++){temp=MAX;k=s;for(int j=0;j<n;j++){if(!visited[j]&&dis[j]<temp){temp=dis[j];k=j;}}visited[k]=1;for(int j=0;j<n;j++){if(!visited[j]&&dis[k]+matrix[k][j]<dis[j]){dis[j]=dis[k]+matrix[k][j];}}}}void DFS(int start,int end,vector<int> &path,int hapSum,int pathSum){int i,j;if(pathSum>shortest)return;if(start==end){if(pathSum>shortest)return;routeSum++;if(hapSum<sumHapp)return;int avg=hapSum/(path.size()-1);if(avg>avgHapp){resPath=path;avgHapp=avg;sumHapp=hapSum;}return;}for(int i=0;i<n;i++){if(!visited[i]&&matrix[start][i]!=MAX){path.push_back(i);visited[i]=1;DFS(i,end,path,hapSum+happiness[i],pathSum+matrix[start][i]);path.pop_back();visited[i]=0;}}}int main(int argc,char *argv[]){int dest;string city;cin>>n>>k>>city;name[0]=city;m[city]=0;happiness[0]=0;for(int i=1;i<n;i++){cin>>name[i]>>happiness[i];if(name[i].compare("ROM")==0){dest=i;}m[name[i]]=i;}for(int i=0;i<n;i++){dis[i]=MAX;for(int j=0;j<n;j++){matrix[i][j]=MAX;}}string city1,city2;int cost;for(int i=0;i<k;i++){cin>>city1>>city2>>cost;int r=m[city1];int c=m[city2];if(cost<matrix[r][c]){matrix[r][c]=matrix[c][r]=cost;}}dijkstra(0);for(int i=0;i<n;i++){visited[i]=0;}shortest=dis[dest];vector<int> path(1,0);visited[0]=1;DFS(0,dest,path,0,0);printf("%d %d %d %d\n",routeSum,shortest,sumHapp,avgHapp);cout<<city;for(int i=1;i<resPath.size();i++){cout<<"->"<<name[resPath[i]];}cout<<endl;return 0;}
- Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)
- 【PAT】【Advanced Level】1087. All Roads Lead to Rome (30)
- PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
- PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS
- 1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise
- PAT 1087. All Roads Lead to Rome
- 【PAT】1087. All Roads Lead to Rome
- PAT-All Roads Lead to Rome
- pat 1087 All Roads Lead to Rome (30)
- pat 1087. All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome (30)
- PAT A 1087. All Roads Lead to Rome (30)
- [PAT]1087. All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome (30)
- 【PAT】1087. All Roads Lead to Rome (30)
- pat 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30) PAT
- jsp学习笔记01
- NodeJs的web开发
- struts2 jsp 获取 Session
- 介绍个学习Linux的好网站
- 柱状图最大矩形问题
- Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)
- 背包9讲之2 完全背包专题
- Unity之项目知识基础
- epoll源码分析
- equals问题
- 基于Android的计步器(Pedometer)的讲解-序(修改)
- hdu 2019 数列有序!
- VS2008编译RPC idl文件(支持.ACF文件)
- Java 引用类型学习