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 alwaysROM
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
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 ...
随机推荐
- 网络流入门 Drainage Ditches
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) ...
- 找出二叉查找树中指定结点的”下一个"结点(也即中序后继)
设计一个算法.找出二叉查找树中指定结点的"下一个"结点(也即中序后继).能够假定每一个结点都含有指向父结点的连接. watermark/2/text/aHR0cDovL2Jsb2c ...
- HDU 3001
题目中说明每个城市至少要走一次,至多走2次,因此要用到三进制压缩,然后就是状态转移方程了. 这道题就处理三进制的地方麻烦一点.同时注意,在选择最小长度时,一定是要每一个点都经过至少一次的,即是状态的每 ...
- POJ 3370 Halloween treats 鸽巢原理 解题
Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include ...
- Oracle EBS LOV速度优化
一.现象 本文地址:http://blog.csdn.net/sunansheng/article/details/50952758 当我们的EBS LOV的SQL写得比較复杂.或者数据量比較多时,L ...
- python 005 正则表达式
. 任意字符 ^ 匹配字符串开始 $ 匹配字符串结尾 * 匹配前面出现了零次或多次 + 匹配前面出现了一次或多次 ? 匹配前面出现零次或一次 {N} 匹配前面出现了N次 {M,N} 匹配重复出现M-N ...
- jsencrypt代码分析
jsencrypt代码分析——openssl的rsa加密解密在js的实现 在js上做rsa,感觉jsencrypt这个是封装的比较好的,但用起来还是遇到了些坑,所以踩进代码里填填坑- 项目在这里 ...
- Ant报错之out of memory
用Ant打包一个比較大的项目的时候,遇到OutOfMemory的问题,求助于Google和百度,网上的解决方式非常多,可是个人认为不够具体全面.我的问题须要综合两种方法才解决.把方案记下来.以期帮助大 ...
- mongoDB学习笔记——在C#中查询
1.下载安装 想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动貌似有很多种,如官方提供的samus. 实现思路大都类似.这里我们用官方提供的mongo-csh ...
- luogu2467 [SDOI2010]地精部落
题目大意 求在$[1,n]$的排列中是波动序列的数量. 题解 性质 当我们对波动序列$a$进行以下操作时,得到的新序列仍然是个波动序列: 若$a_i = a_j+1且|j-i|>1$,将$a_i ...