All Roads Lead to Rome (30)
2016-05-24 12:55
525 查看
问题描述:
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
AC代码:
#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
const int infinity = 35565;
int N, K;
map<string, int> cId;
vector<vector<int>> cost;
vector<bool> visited;
vector<string> cName;
vector<int> cHappiness;
vector<int> path;
vector<int> totalCost;
vector<int> numOfRoute;
vector<int> totalHapp;
vector<int> citiesInRoute;
string startCity;
void dijkstra(){
totalCost[0] = 0;
numOfRoute[0] = 1;
citiesInRoute[0] = 0;
int k;//k为每一轮花费最少的city
for (int i = 0; i<N; i++){
int k = -1;
for (int j = 0; j<N; j++){
if (!visited[j] && (k<0 || totalCost[j]<totalCost[k])){
k = j;
}
}
visited[k] = true;
for (int j = 0; j<N; j++){
if (!visited[j]){
if (totalCost[j]>cost[k][j] + totalCost[k]){
totalCost[j] = cost[k][j] + totalCost[k];
numOfRoute[j] = numOfRoute[k];
totalHapp[j] = totalHapp[k] + cHappiness[j];
path[j] = k;
citiesInRoute[j] = citiesInRoute[k] + 1;
}
else if (totalCost[j] == cost[k][j] + totalCost[k]){
numOfRoute[j] += numOfRoute[k];
if (totalHapp[j]<totalHapp[k] + cHappiness[j]){
totalHapp[j] = totalHapp[k] + cHappiness[j];
path[j] = k;
citiesInRoute[j] = citiesInRoute[k] + 1;
}
else if (totalHapp[j] == totalHapp[k] + cHappiness[j]){
if (citiesInRoute[j]>citiesInRoute[k] + 1){
path[j] = k;
citiesInRoute[j] = citiesInRoute[k] + 1;
}
}
}
}
}
}
}
int main(){
cin >> N >> K >> startCity;
cId[startCity] = 0;
cost.resize(N, vector<int>(N, infinity));
cName.resize(N);
cHappiness.resize(N);
path.resize(N, -1);
totalHapp.resize(N, 0);
citiesInRoute.resize(N, infinity);
totalCost.resize(N, infinity);
numOfRoute.resize(N, 0);
visited.resize(N, false);
cName[0] = startCity;
for (int i = 1; i<N; i++){
string tmpstr;
int tmpint;
cin >> tmpstr >> tmpint;
cName[i] = tmpstr;
cHappiness[i] = tmpint;
cId[tmpstr] = i;
}
for (int i = 0; i<K; i++){
string str1, str2; int cost1, c1, c2;
cin >> str1 >> str2 >> cost1;
c1 = cId[str1]; c2 = cId[str2];
cost[c1][c2] = cost1;
cost[c2][c1] = cost1;
}
int d = cId["ROM"];
dijkstra();
cout << numOfRoute[d] << " " << totalCost[d] << " " << totalHapp[d] << " " << totalHapp[d] / citiesInRoute[d] << endl;
vector<string> answer;
int j = d;
while (j != -1){
answer.push_back(cName[j]);
j = path[j];
}
for (int i = answer.size() - 1; i >= 0; i--){
if (0 == i){
cout << answer[i] << endl;
}
else{
cout << answer[i] << "->";
}
}
return 0;
}
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
AC代码:
#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
const int infinity = 35565;
int N, K;
map<string, int> cId;
vector<vector<int>> cost;
vector<bool> visited;
vector<string> cName;
vector<int> cHappiness;
vector<int> path;
vector<int> totalCost;
vector<int> numOfRoute;
vector<int> totalHapp;
vector<int> citiesInRoute;
string startCity;
void dijkstra(){
totalCost[0] = 0;
numOfRoute[0] = 1;
citiesInRoute[0] = 0;
int k;//k为每一轮花费最少的city
for (int i = 0; i<N; i++){
int k = -1;
for (int j = 0; j<N; j++){
if (!visited[j] && (k<0 || totalCost[j]<totalCost[k])){
k = j;
}
}
visited[k] = true;
for (int j = 0; j<N; j++){
if (!visited[j]){
if (totalCost[j]>cost[k][j] + totalCost[k]){
totalCost[j] = cost[k][j] + totalCost[k];
numOfRoute[j] = numOfRoute[k];
totalHapp[j] = totalHapp[k] + cHappiness[j];
path[j] = k;
citiesInRoute[j] = citiesInRoute[k] + 1;
}
else if (totalCost[j] == cost[k][j] + totalCost[k]){
numOfRoute[j] += numOfRoute[k];
if (totalHapp[j]<totalHapp[k] + cHappiness[j]){
totalHapp[j] = totalHapp[k] + cHappiness[j];
path[j] = k;
citiesInRoute[j] = citiesInRoute[k] + 1;
}
else if (totalHapp[j] == totalHapp[k] + cHappiness[j]){
if (citiesInRoute[j]>citiesInRoute[k] + 1){
path[j] = k;
citiesInRoute[j] = citiesInRoute[k] + 1;
}
}
}
}
}
}
}
int main(){
cin >> N >> K >> startCity;
cId[startCity] = 0;
cost.resize(N, vector<int>(N, infinity));
cName.resize(N);
cHappiness.resize(N);
path.resize(N, -1);
totalHapp.resize(N, 0);
citiesInRoute.resize(N, infinity);
totalCost.resize(N, infinity);
numOfRoute.resize(N, 0);
visited.resize(N, false);
cName[0] = startCity;
for (int i = 1; i<N; i++){
string tmpstr;
int tmpint;
cin >> tmpstr >> tmpint;
cName[i] = tmpstr;
cHappiness[i] = tmpint;
cId[tmpstr] = i;
}
for (int i = 0; i<K; i++){
string str1, str2; int cost1, c1, c2;
cin >> str1 >> str2 >> cost1;
c1 = cId[str1]; c2 = cId[str2];
cost[c1][c2] = cost1;
cost[c2][c1] = cost1;
}
int d = cId["ROM"];
dijkstra();
cout << numOfRoute[d] << " " << totalCost[d] << " " << totalHapp[d] << " " << totalHapp[d] / citiesInRoute[d] << endl;
vector<string> answer;
int j = d;
while (j != -1){
answer.push_back(cName[j]);
j = path[j];
}
for (int i = answer.size() - 1; i >= 0; i--){
if (0 == i){
cout << answer[i] << endl;
}
else{
cout << answer[i] << "->";
}
}
return 0;
}
相关文章推荐
- 定制班第7课
- poj之旅——3666
- C++第六次作业
- SparkStream文件监控和数据读取
- background-position 用法详细介绍
- shell脚本基础与练习
- 第十二周项目二-Time类中的运算符重载
- runtime——消息机制
- Java之------多线程(加强篇)
- [ 系统]win10下vmware workstation 12虚拟机安装MAC OS X
- tomcat启用不同端口,同时启动多个项目
- fashion
- 史上最全linux内核配置详解
- Contiki协议栈:索引目录
- 图解Windows网络命令使用实例
- 项目1-3 定义一个定义完整的类。这样的类在(2)的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; d
- 清除浮动
- GT-n8000平板开机密码忘记 解决办法
- ASP.NET——内置对象
- Linux内核配置和编译过程详解