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 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
Code:
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
#pragma warning(disable:4996)
using namespace std;
const int maxn = 201;
const int INF = 0x3f3f3f3f;
int g[maxn][maxn];
bool vis[maxn] = { false };
map<string, int> city2id;
map<int, string> id2city;
int pre[maxn], pw[maxn], weight[maxn] = { 0 }, d[maxn], city[maxn], num[maxn] = { 0 };
// 分别是 先导节点,点权重,到i点的快乐值之和,到i点的花费之和,到i点的城市数,花费相同的方案数
int n, k, ni = 0;
int change(string s)
{
if (city2id.count(s))
return city2id[s];
else {
city2id[s] = ni;
id2city[ni] = s;
return ni++;
}
}
void Dijkstra(int st)
{
for (int cnt = 0; cnt < n; cnt++)
{
int v = -1, min_dis = INF;
for (int i = 0; i<n; i++)
{
if (!vis[i] && d[i] < min_dis)
{
v = i;
min_dis = d[i];
}
}
if (v == -1) return;
vis[v] = true;
for (int i = 0; i<n; i++)
{
if (!vis[i] && g[v][i] != INF)
{
if (d[i] > d[v] + g[v][i]) // 如果从st开始通过v城到i城花费更少
{
d[i] = d[v] + g[v][i]; // 更新花费
weight[i] = weight[v] + pw[i]; // 更新总快乐值 = 到v城总快乐值 + 到i城的快乐值
city[i] = city[v] + 1; // 更新城市数 = 到v城经过的总城市数 + 1
num[i] = num[v]; // 到i城的路径数就是到v城的路径数
pre[i] = v; // 到i城先经过v城
}
else if (d[i] == d[v] + g[v][i])
{
num[i] += num[v];
if (weight[i] < weight[v] + pw[i])
{
weight[i] = weight[v] + pw[i];
city[i] = city[v] + 1;
pre[i] = v;
}
else if (weight[i] == weight[v] + pw[i])
{
if (city[i] > city[v] + 1)
{
city[i] = city[v] + 1;
pre[i] = v;
}
}
}
}
}
}
}
void DFS(int dst)
{
if (dst == pre[dst])
{
cout << id2city[dst];
return;
}
DFS(pre[dst]);
cout << "->" << id2city[dst];
}
int main()
{
scanf("%d%d", &n, &k);
fill(g[0], g[0] + maxn*maxn, INF);
fill(d, d + maxn, INF);
fill(city, city + maxn, INF);
for (int i = 0; i<n; i++)
pre[i] = i;
string st_s; cin >> st_s;
int st = change(st_s);
pw[st] = 0; d[st] = 0; city[st] = 0; num[st] = 1;
for (int i = 0; i<n - 1; i++)
{
string t; cin >> t;
scanf("%d", &pw[change(t)]);
}
for (int i = 0; i<k; i++)
{
string t1, t2; cin >> t1 >> t2;
int td1 = change(t1);
int td2 = change(t2);
int lw; scanf("%d", &lw);
g[td1][td2] = g[td2][td1] = lw;
}
Dijkstra(st);
int dst = change("ROM");
printf("%d %d %d %d\n", num[dst], d[dst], weight[dst], weight[dst] / city[dst]);
DFS(dst);
return 0;
}
思路:
- fill(g[0], g[0] + maxnmaxn, INF); fill 二维数组的正确方法;这样fill(g, g + maxnmaxn, INF)是错的