题目:
题目描述
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.
输入描述:
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.
输出描述:
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".
输入例子:
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
输出例子:
3 3 195 97
HZH->PRS->ROM
算法分析:
DFS+Dijkstra
图的边权重表示cost,图的顶点表示城市,顶点的权重表示该城市的幸福度;
注意:最后一个输出average happiness,不包括起始城市。如输出样例中195/2 =97.5(舍弃0.5)
AC代码
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1000000000;
const int N = 202;
const string destination = "ROM";
map<string, int> cityToint; //城市-->序号
map<int, string> intTocity; //序号-->城市
int happiness[N], num[N], e[N][N], dist[N], bypass[N]; //从起点s到终点i,对应的cost(dist),happiness,最短路径方案数目num...
bool visit[N];
int cityhappiness[N];
string name[N];
int pre[N];
int n, k;
string start;
void Dijkstra(int s); //s是起点
void print(int x); //逆序打印路径
int main(void) {
fill(e[0], e[0] + N*N, inf);
fill(dist, dist + N, inf);
fill(visit, visit + N, false);
cin >> n >> k >> start;
cityToint[start] = 0; intTocity[0] = start;
string city; int happy;
for (int i = 1;i < n; i++) {
cin >> city >> happy;
cityToint[city] = i; intTocity[i] = city;
cityhappiness[i] = happy;
}
cityhappiness[0] = 0;
string a, b; int c;
for (int i = 1; i <= k; i++) {
cin >> a >> b >> c;
int id1 = cityToint[a]; int id2 = cityToint[b];
e[id1][id2] = c;
e[id2][id1] = e[id1][id2]; //无向图
}
int d = cityToint[destination]; //终点id
Dijkstra(0);
cout << num[d] << " " << dist[d] << " " << happiness[d] << " " << happiness[d] / bypass[d] << endl;
//逆序打印路径
print(d);
puts("");
//system("pause");
return 0;
}
void Dijkstra(int s) {
dist[s] = 0;
fill(bypass, bypass + N, inf);
bypass[s] = 0;
fill(num, num + N, 0);
num[s] = 1;
fill(happiness, happiness + N, 0);
for (int i = 0; i < n; i++) {
int u = -1, mind = inf;
for (int j = 0; j < n; j++)
if (false == visit[j] && mind > dist[j]) {
mind = dist[j];
u = j;
}
if (-1 == u) return; //end
visit[u] = true;
for (int j = 0; j < n; j++) {
if (false == visit[j] && e[u][j] < inf) {
if (e[u][j] + mind < dist[j]) {
dist[j] = e[u][j] + mind;
// pre[j].clear();
pre[j] = u;
happiness[j] = happiness[u] + cityhappiness[j];
bypass[j] = bypass[u] + 1; //路过的城市数+1
num[j] = num[u]; //方案数目
}
else if (dist[j] == e[u][j] + mind) {
num[j] += num[u];
int temph = happiness[u] + cityhappiness[j];
int tempb = bypass[u] + 1;
if ((temph > happiness[j]) || //the maximum happiness
(temph == happiness[j] && tempb < bypass[j])) { //the maximum average happiness
happiness[j] = temph;
bypass[j] = tempb;
pre[j] = u;
}
}
}
}
}
}
void print(int x) {
if (x) {
print(pre[x]);
cout << "->";
}
cout << intTocity[x];
}