官网
1087. All Roads Lead to Rome (30)
时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
题目大意
- 1.给出每个城市的happiness和各个城市的路程,要你找出一条路径具有以下性质,(1)cost最小,(2)最大的happiness,(3)最大平均happiness。最后输出路径。
- 2.输出最小cost路线的条数,cost,happiness和平均happiness,路径。
解题思路
- 1.dijkstra求最短路径,并更新相应其他量并比较,注意输出要求,先仔细看题目,把题目看完。
- 2.注意要输出cost最小路线的条数,不是记住最后的最优路径就行了,可以外用一个数组routes记录路径的条数,有个非常机智的方法就是如果djkstra算法中,找到一条cost比之前小的,更新的时候,也要更新routes[next] = routes[now],如果遇到相等的,证明另外一条路过来的也可以走通,而且cost也相等,所以更新routes为routes[next] += routes[now]。
AC代码
#include<iostream>
#include<vector>
#include<deque>
#include<algorithm>
#include<string>
#include<map>
#include<fstream>
using namespace std;
//ifstream in("1.txt");
//#define cin in
#define INF 0x3f3f3f3f
int n, m, ROM_id;
string first;
map<string, int> city_id;
map<int, string> id_city;
int road[201][201];
int hapiness[201], dist[201], visited[201], lever[201], father[201], total_hap[201];
int routes = 0;
void djkstra(int k)
{
visited[k] = 1;
for (int i = 1; i <= n; i++)
{
;
//如果cost有更小的
if (road[k][i] != 0 && dist[k] + road[k][i]<dist[i])
{
dist[i] = dist[k] + road[k][i];
lever[i] = lever[k] + 1;
father[i] = k;
total_hap[i] = total_hap[k] + hapiness[i];
if (i == city_id["ROM"])
{
routes = 0;
}
}
//如果cost相等
else if (dist[i] != INF&&road[k][i] != 0 && dist[k] + road[k][i] == dist[i])
{
if (i == city_id["ROM"])
{
routes++;
}
int tem_total_hap = total_hap[k] + hapiness[i];
int tem_lever = lever[k] + 1;
//如果总幸福度相等
if (tem_total_hap != total_hap[i])
{
if (tem_total_hap>total_hap[i])
{
dist[i] = dist[k] + road[k][i];
lever[i] = lever[k] + 1;
father[i] = k;
total_hap[i] = total_hap[k] + hapiness[i];
}
}
else
{
if (tem_lever<lever[i])
{
dist[i] = dist[k] + road[k][i];
lever[i] = lever[k] + 1;
father[i] = k;
total_hap[i] = total_hap[k] + hapiness[i];
}
}
}
}
//找出未访问过的cost最小的
int nx = -1, minroad = INF;
for (int i = 1; i <= n; i++)
{
if (visited[i] == 0 && dist[i]<minroad)
{
minroad = dist[i];
nx = i;
}
}
if (nx != -1 && visited[nx] != 1)
{
djkstra(nx);
}
}
int main()
{
cin >> n >> m >> first;
int cnt = 0;
city_id[first] = 1;
id_city[1] = first;
cnt++;
string a;
int hap;
for (int i = 0; i < n - 1; i++)
{
cin >> a >> hap;
if (city_id[a] == 0)
{
cnt++;
city_id[a] = cnt;
id_city[cnt] = a;
}
hapiness[cnt] = hap;
}
string a1, a2;
int v;
for (int i = 0; i < m; i++)
{
cin >> a1 >> a2 >> v;
road[city_id[a1]][city_id[a2]] = road[city_id[a2]][city_id[a1]] = v;
}
//初始化
fill(dist, dist + n + 1, INF);
dist[1] = 0;
father[1] = -1;
ROM_id = city_id["ROM"];
//计算
djkstra(1);
//输出
cout << routes + 1 << " " << dist[ROM_id] << " " << total_hap[ROM_id] << " " << total_hap[ROM_id] / lever[ROM_id] << endl;
int now = ROM_id;
deque<int> dq;
while (now != -1)
{
dq.push_back(now);
now = father[now];
}
bool flag = false;
while (!dq.empty())
{
now = dq.back();
if (!flag)
{
flag = true;
cout << id_city[now];
}
else
{
cout << "->" << id_city[now];
}
dq.pop_back();
}
cout << endl;
return 0;
}