PAT_A1087#All Roads Lead to Rome
Source:
Description:
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), 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 Klines 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 alwaysROMwhich 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
Keys:
Attention:
- 结点编号从1开始,因为映射判定的条件是结点编号==0
Code:
/*
Data: 2019-08-27 19:12:20
Problem: PAT_A1087#All Roads Lead to Rome
AC: 37:38 题目大意:
从起点至终点ROM花费最少且最幸福的一条路,
若不唯一,给出平均幸福指数最高的路径 输入:
第一行给出,城市数N,路径数K,起点城市
接下来N-1行,城市名,幸福指数
接下来K行,v1,v2,cost
输出:
最短路径数目,花费,幸福指数,平均幸福指数(整数部分,除去起点)
起点至终点的路径
*/
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=1e3,INF=1e9;
int n,m,v1,v2,t,grap[M][M],d[M],w[M],vis[M],pt=,optH=,cnt=;
vector<int> pre[M],path,optPath;
map<string,int> mp;
string city[M]; int ToInt(string s)
{
if(mp[s] == )
{
city[pt]=s;
mp[s]=pt++;
}
return mp[s];
} void Dijskra(int s)
{
fill(vis,vis+M,);
fill(d,d+M,INF);
d[s]=;
for(int i=; i<=n; i++)
{
int u=-,Min=INF;
for(int j=; j<=n; j++)
{
if(vis[j]== && d[j]<Min)
{
u = j;
Min=d[j];
}
}
if(u==-)
break;
vis[u]=;
for(int v=; v<=n; v++)
{
if(vis[v]== && grap[u][v]!=INF)
{
if(d[u]+grap[u][v]<d[v])
{
pre[v].clear();
pre[v].push_back(u);
d[v] = d[u]+grap[u][v];
}
else if(d[u]+grap[u][v]==d[v])
pre[v].push_back(u);
}
}
}
} void DFS(int v)
{
if(v == )
{
cnt++;
int happy=;
for(int i=; i<path.size(); i++)
happy+=w[path[i]];
if(happy > optH)
{
optH = happy;
optPath = path;
}
else if(happy==optH && path.size()<optPath.size())
optPath = path;
return;
}
path.push_back(v);
for(int i=; i<pre[v].size(); i++)
DFS(pre[v][i]);
path.pop_back();
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE string s1,s2;
cin >> n >> m >> s1;
ToInt(s1);
for(int i=; i<n; i++)
{
cin >> s1;
v1 = ToInt(s1);
scanf("%d", &w[v1]);
if(s1 == "ROM")
t = v1;
}
fill(grap[],grap[]+M*M,INF);
for(int i=; i<m; i++)
{
cin >> s1 >> s2;
v1 = ToInt(s1);
v2 = ToInt(s2);
scanf("%d", &grap[v1][v2]);
grap[v2][v1]=grap[v1][v2];
}
Dijskra();
DFS(t);
printf("%d %d %d %d\n", cnt,d[t],optH,optH/optPath.size());
printf("%s", city[].c_str());
for(int i=optPath.size()-; i>=; i--)
printf("->%s", city[optPath[i]].c_str()); return ;
}
PAT_A1087#All Roads Lead to Rome的更多相关文章
- PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]
1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...
- [图的遍历&多标准] 1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...
- PAT 1087 All Roads Lead to Rome
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- PAT1087. All Roads Lead to Rome
PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...
- pat1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- A1087. All Roads Lead to Rome
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
随机推荐
- 计算机与linux操作系统的发展
一.计算机 (一)计算机的概念 1.概念:计算机(computer)俗称电脑,是一种用于高速计算的电子计算机器,可以进行数值计算,又可以进行逻辑计算,还具有存储记忆功能.是能够按照程序运行,自动.高速 ...
- MVC.Net:对MVC5部署时出现403.14错误的解决方法
当我们部署MVC5到IIS 7的时候,有时会出现403.14的错误,如下图: 对于这个错误的解决方法就是在应用程序的web.config的system.webServer节点中加入这一句: <m ...
- Android 应用启动动画代码
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题 setContentView(R.layout.activity_main); getWin ...
- 【Linux学习】Ubuntu下 sambaserver搭建
1.安装samba,smbfs 2.配置smb.conf文件 配置文件之前须要先备份一下须要配置的文件(养成好的习惯) 输入命令: 进入到smb.conf文件里,在文件的最后加入下列语句 保存后.退出 ...
- Codeforces--626B--Cards(模拟)
Cards Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- hdoj Radar Installation
Problem Description Assume the coasting is an infinite straight line. Land is in one side of coastin ...
- 最新昆石VOS2009/VOS3000手机号段导入文件(手机归属地)
使用2017年4月最新版手机号段归属地制作,支持所有版本的VOS 共360569条记录,兼容所有版本的昆石VOS,包括VOS2009.vos3000.vos5000 导入比较简单.下载后解压到桌面在V ...
- Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...
- Organize Your Train part II(hash)
http://poj.org/problem?id=3007 第一次用STL做的,TLE了,自己构造字符串哈希函数才可以.. TLE代码: #include <cstdio> #inclu ...
- 关于每次取PC的值为PC+4的问题
关于ARM的书上常说由于流水线特性,在指令执行期间读取程序计数器时,读出的值需要为当前指令+4 一开始总是不理解,今天被一位大神一语道破其中精髓.... 程序计数器(PC)总是指向“正在取指”的指令 ...