All Roads Lead to Rome (30)
1、题目:
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 alwaysROM
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 recommanded. 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 recommanded 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
2、思路
1、哈希映射,需要在加边的时候把每个英文节点映射成为整数:unordered_map可以做到,输出的时候要把整数映射回去
2、统计最短路的数量。这里是我做的时候遇到的最坑的,我一直以为只有到达了终点才需要判断最短路的条数,其实如果到达终点的某些中间节点也有很多条的话,那就会出错。对于最短路数量,我们可以开一个统计数组Cntdist[N],记录每个节点的最短路的数量。对于一个点,要么继承前一个点的最短路数量(第一次统计最短路),要么加上前一个点的数量(从另一个地方来的,也是最短路)
3、PAT最爱考输出细节,不能出错。
3、代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<unordered_map>
using namespace std;
typedef pair<int,int> PII;
const int N = 300,M = N*N;
unordered_map<string,int> S1;
unordered_map<int,string> S2;
int h[N],e[M],w[M],ne[M],idx;
int dist[N],pre[N],s[N],Cntdist[N];
bool st[N];
int a[N];
int z;
int n,m;
string start;
int get_s(string s)
{if(S1.count(s)==0) S1[s] = z++;return S1[s];
}
void add(int a,int b,int c)
{e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx ++;
}
void Dijkstra(int x)
{int ed = S1["ROM"];memset(dist,0x3f,sizeof dist);memset(pre,-1,sizeof pre);Cntdist[x] = 1;priority_queue<PII,vector<PII>,greater<PII>> heap;dist[x] = 0;heap.push({0,x});while(heap.size()){auto t = heap.top();heap.pop();int dis = t.first,ver = t.second;if(st[ver]) continue;st[ver] = true;for(int i = h[ver];~i;i= ne[i]){int j = e[i];if(dist[j] > dist[ver] + w[i]){dist[j] = dist[ver] + w[i];pre[j] = ver;heap.push({dist[j],j});Cntdist[j] = Cntdist[ver];}else if(dist[j] == dist[ver] + w[i]){Cntdist[j] += Cntdist[ver];int res1 = 1,res2 = 1,ans1 = a[j],ans2 = a[ver];for(int k = pre[j];k!=x;k = pre[k]){res1 ++;ans1 += a[k];}for(int k = pre[ver];k!=x;k = pre[k]){res2 ++;ans2 += a[k];}// 如果加上最后一个节点,那么最后的值会是多少res2++,ans2 += a[j];if(ans2 > ans1) pre[j] = ver;else if(ans2==ans1&&ans2/res2 > ans1/res1) pre[j] = ver; heap.push({dist[j],j});}}}
}
int main()
{std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);memset(h,-1,sizeof h);cin >> n >> m >> start;int sta = get_s(start);S2[sta] = start;for(int i = 1;i<=n-1;i++){string name;int cos;cin>>name>>cos;int t1 = get_s(name);a[t1] = cos;}for(int i = 1;i<=m;i++){int cost;string name1,name2;cin>> name1 >> name2 >> cost;int na1 = get_s(name1),na2 = get_s(name2);S2[na1] = name1,S2[na2] = name2;add(na1,na2,cost),add(na2,na1,cost);}Dijkstra(sta);int ed = S1["ROM"];int cost = 0;int cnt = 1;s[0] = ed;for(int i = pre[ed];i!=sta;i = pre[i])s[cnt++] = i;s[cnt++] = sta;reverse(s,s+cnt);cout<< Cntdist[ed] << " "<<dist[ed]<<" ";for(int i = 1;i<cnt;i++)cost += a[s[i]];cout<< cost<<" "<< cost/(cnt-1)<<endl;cout<<S2[s[0]];for(int i = 1;i<cnt;i++)cout<<"->"<<S2[s[i]];return 0;}