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 ...
随机推荐
- 洛谷——P1034 矩形覆盖
https://www.luogu.org/problem/show?pid=1034 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的 ...
- Linux下安装lvs
lvs已经编译到linux内核中,仅仅须要安装lvs的管理软件ipvsadm就可以 1. 插入光盘.查找设备 [root@chen ~]# ls -l /dev | grep cdrom lrwxrw ...
- 一键解决ScrollView嵌套ListView仅仅显示一行的问题
/** * 解决ScrollView嵌套ListView仅仅显示一行的问题 * * @param listView */ private void setListViewHeightBasedOnCh ...
- java调用百度地图API依据地理位置中文获取经纬度
百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...
- c# POST和GET方式通过server地址提交数据
1:POST方式提交: <strong><span style="font-size:14px;">private static string HttpPo ...
- C++高精度性能測试函数
在实际software开发工作中.我们常常会測试某个module或者function的执行效率.或者是某个算法的时间复杂度(尽管时间复杂度一定程度上依赖于机器性能.但在同一台computer上,经过算 ...
- 到底什么是nandflash,norflash,sdram,emmc,rom,ram【转】
本文转载自:http://blog.sina.com.cn/s/blog_6dd8f2b70101le26.html 最近被nandflash,norflash,sdram,emmc,rom,ram搞 ...
- canvas制作饼图和环形图,使用Excanvas兼容IE67
excanvas 地址:http://excanvas.sourceforge.net/ <!DOCTYPE html> <html> <head> <met ...
- 2017 Multi-University Training Contest - Team 2 &hdu 6055 Regular polygon
Regular polygon Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- Python开发利器PyCharm 2.7附注册码
PyCharm 2.7 下载 http://download.jetbrains.com/python/pycharm-2.7.2.exe 激活注册 user name:EMBRACE key: 14 ...