【PAT】1087. All Roads Lead to Rome (30)
来源:互联网 发布:阿里云免费套餐 编辑:程序博客网 时间:2024/05/22 03:31
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 1Sample Output:
3 3 195 97HZH->PRS->ROM
分析:查找所有最短路,列出最短路共有多少条,并且给出最短路中的最优解。
#include <iostream>#include <vector>#include <set>#include <algorithm>#include <string>#include <map>using namespace std;#define INF 9999999struct city{city(string _n, int _h){name=_n; happy=_h;}string name;int happy;};vector<city> cities; vector<vector<int> > cost;vector<vector<int> > allPath;vector<int> bestPath;vector<int> possiblePath;vector<bool> visited;vector<int> dist; //记录起点到各个点的距离 map<string, int> name2id;int cityNum, roadNum;int cnt = 0;void dijkstra(int s){int i;dist[s] = 0;while(true){int v = -1;for(int u=0; u<cityNum; u++){if(!visited[u]&&(v==-1||dist[u]<dist[v]))v=u;}if(v==-1) break;visited[v]=true;for(int u=0; u<cityNum; u++){if(dist[u] > dist[v] + cost[v][u]){dist[u] = dist[v] + cost[v][u];allPath[u].clear();allPath[u].push_back(v);}else if(dist[u]==dist[v]+cost[v][u]){ allPath[u].push_back(v);}}}}int maxHappy = 0;double maxAve = 0;int pathNum = 0; //最短路的条数 void findBestPath(int t){possiblePath.push_back(t);if(t==0){pathNum++;int happy = 0;for(int i=0; i<possiblePath.size(); i++){int index = possiblePath[i];happy += cities[index].happy;}if(happy > maxHappy){bestPath = possiblePath;maxHappy = happy;maxAve = (double)happy/(double)(possiblePath.size()-1);}else if(happy==maxHappy){double aveNow = (double)happy/(double)(possiblePath.size()-1);if(aveNow>maxAve){bestPath = possiblePath;maxAve = aveNow;}}return;}for(int i=0; i<allPath[t].size(); i++){findBestPath(allPath[t][i]);possiblePath.pop_back(); }}int main(int argc, char** argv) {int c, i, j, happy;string start, from, to, str;scanf("%d%d",&cityNum, &roadNum);cin>>start;cities.push_back(city(start,0));name2id[start] = 0;//起点 for(i=1; i<cityNum; i++){cin>>str>>happy;name2id[str] = i;cities.push_back(city(str,happy));}cost.resize(cityNum, vector<int>(cityNum,INF));for(i=0; i<roadNum; i++){cin>>from>>to>>c;int a = name2id[from];int b = name2id[to];cost[a][b] = cost[b][a] = c;}dist.resize(cityNum,INF);for(i=0; i<cityNum; i++) dist[i] = cost[0][i];int s = 0;//起点int t = name2id["ROM"];//终点 visited.resize(cityNum,false);allPath.resize(cityNum);dijkstra(s);//查找从起点出发的所有最短路 findBestPath(t);//找到最短路中的最优解 int minCost=0;for(i=bestPath.size()-1; i>0; i--){ int a = bestPath[i]; int b = bestPath[i-1]; minCost += cost[a][b];}cout<<pathNum<<" "<<minCost<<" "<<maxHappy<<" "<<(int)maxAve<<endl;for(i=bestPath.size()-1; i>=0; i--){string name = cities[bestPath[i]].name;if(i!=0) cout<<name<<"->";elsecout<<name<<endl;}return 0;}
解法二:可以不用结构体
#include <iostream>#include <string>#include <vector>#include <map>#include <algorithm>using namespace std;#define INF 99999int N,K;map<string, int> happy;map<string, int> name2id;map<int, string> id2name;vector<vector<int> > cost;vector<vector<int> > allPath;vector<int> d;void dijkstra(int s){allPath.resize(N);vector<bool> used(N,false);d.resize(N,INF);d[s] = 0;while(true){int v = -1;for(int u=0; u<N; u++){if(!used[u] && (d[u]<d[v] || v==-1))v = u;}if(v == -1) break;used[v] = true;for(int u=0; u<N; u++){if(d[u] > cost[v][u] + d[v]){d[u] = cost[v][u] + d[v];allPath[u].clear();allPath[u].push_back(v);}else if(d[u] == cost[v][u]+d[v]){allPath[u].push_back(v);}}}}vector<int> possiblePath;vector<int> bestPath;int maxHappy=0;double maxAve = 0;int pathNum = 0;void findBest(int des){possiblePath.push_back(des);if(des == 0){pathNum++;int h=0;double a=0;for(int i=0; i<possiblePath.size(); i++){int t = possiblePath[i];string str = id2name[t];h += happy[str];}a = (double)h/(double)(possiblePath.size()-1);if(h>maxHappy || (h==maxHappy&&a>maxAve)){maxHappy = h;maxAve = a;bestPath = possiblePath;}}for(int i=0; i<allPath[des].size(); i++){findBest(allPath[des][i]);possiblePath.pop_back();}}int main(int argc, char** argv) {string start;scanf("%d%d",&N,&K);cost.resize(N,vector<int>(N,INF));cin>>start;name2id.insert(make_pair(start,0));id2name.insert(make_pair(0,start));happy.insert(make_pair(start,0));int i, val;string str;for(i=1; i<N; i++){cin>>str>>val;name2id.insert(make_pair(str,i));id2name.insert(make_pair(i,str));happy.insert(make_pair(str,val));}string from, to;int a, b, c;for(i=0; i<K; i++){cin>>from>>to>>c;a = name2id[from];b = name2id[to];cost[a][b] = cost[b][a] = c;}dijkstra(0);int des = name2id["ROM"];findBest(des);printf("%d %d %d %d\n", pathNum, d[des], maxHappy, (int)maxAve);for(i=bestPath.size()-1; i>=0; i--){string tmp = id2name[bestPath[i]];if(i==bestPath.size()-1){cout<<tmp;}else{cout<<"->"<<tmp;}}return 0;}
- PAT 1087. All Roads Lead to Rome
- 【PAT】1087. All Roads Lead to Rome
- 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
- 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-A-1087. All Roads Lead to Rome (30)
- 【PAT】【Advanced Level】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)
- Xcode 7.1项目中Objective-C和swift混合编程(一)
- coderforce 554B Ohana Cleans Up
- 重建二叉树(已知两序求另一序)
- LVM逻辑卷扩容
- linux压缩和解压
- 【PAT】1087. All Roads Lead to Rome (30)
- OC第五天:字典、集、数组排序
- 某大学医疗系统更改版
- Ulua学习笔记
- 单例
- 10.80386指令-Intel格式和AT&T格式的指令格式
- oracle常用SQL语句
- S2SH+ajax+json-------Struts.xml文件编写
- HDU 1025 最长递增子序列 DP+二分法