1087. All Roads Lead to Rome (30)
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
#include<stdio.h>
#include<map>
#include<string>
#include<string.h>
#include<stack>
using namespace std;
#define MAX 210
int INF = ;
int HappyVal[];
int visit[MAX];
int Grap[MAX][MAX];
int d[MAX];
int h[MAX];
int num[MAX];
int pre[MAX];
int Count[MAX]; void Dijkstra(int Begin,int NodeNum)
{
d[Begin] = ;
h[Begin] = HappyVal[Begin];
num[Begin] = ;
Count[Begin] = ;
for(int i = ;i < NodeNum ;i++)
{
int index = -;
int MIN = INF;
for(int j = ;j <NodeNum ;j++)
{
if(!visit[j] && d[j] < MIN)
{
index = j;
MIN = d[j];
}
} if(index == -) return ;
visit[index] = true;
for(int v = ;v <NodeNum ;v++)
{
if(!visit[v] && Grap[index][v]!=INF)
{
if(d[index]+Grap[index][v]<d[v])
{
d[v] = d[index]+Grap[index][v];
num[v] = num[index];
h[v] = h[index] + HappyVal[v];
pre[v] = index;
Count[v] = Count[index] +;
}
else if(d[index]+Grap[index][v]==d[v])
{
num[v] = num[v] + num[index]; if(h[v] < h[index] + HappyVal[v])
{
h[v] = h[index] + HappyVal[v];
Count[v] = Count[index] +;
pre[v] = index;
}
else if( h[v] == h[index] + HappyVal[v] && (double)(h[index] + HappyVal[v])/(Count[index]+) > (double)h[v]/Count[v])
{
Count[v] = Count[index] +;
pre[v] = index;
}
}
}
}
} } int main()
{
int i,j,N,K,happy,ROM;
char Begin[],tem[];
scanf("%d%d%s",&N,&K,Begin);
map<string,int> mm;
map<int,string> mm2;
mm[Begin] = ;
mm2[] = Begin ;
HappyVal[mm[Begin]] = ;
for(i = ; i < N ;i++)
{
scanf("%s%d",tem,&happy);
if(strcmp("ROM",tem)==) ROM = i;
mm[tem] = i;
mm2[i] = tem;
HappyVal[i] = happy;
} char x[],y[]; for(i = ; i < N ;i++)
{
for(j = ; j < N ;j++)
{
Grap[i][j] = INF;
}
d[i] = h[i] = INF;
pre[i] = -;
Count[i] = ;
} for(i = ; i < K ;i++)
{
scanf("%s%s",x,y);
scanf("%d",&Grap[mm[x]][mm[y]]);
Grap[mm[y]][mm[x]] = Grap[mm[x]][mm[y]];
} Dijkstra( mm[Begin] , N); printf("%d %d %d %d\n",num[mm["ROM"]],d[mm["ROM"]],h[mm["ROM"]],h[mm["ROM"]]/Count[mm["ROM"]]); stack<int> ss;
i= mm["ROM"];
while(i != -)
{
ss.push(i);
i = pre[i];
}
int fir = ;
while(!ss.empty())
{
if(fir == )
{
fir = ;
printf("%s",mm2[ss.top()].c_str());
}
else printf("->%s",mm2[ss.top()].c_str());
ss.pop();
} printf("\n"); return ;
}
1087. All Roads Lead to Rome (30)的更多相关文章
- [图的遍历&多标准] 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 ...
- 1087 All Roads Lead to Rome (30)(30 分)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- 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 ...
- 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
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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- envi中selected rgb bands contain different spatial sizes
是选择了不同的影像文件envi中selected rgb bands contain different spatial sizes
- 【安卓面试题】使用SQLiteOpenHelper的getReadableDatabase()获得的数据库能不能,做写的操作
可以! 不要被Readable的意思误导啦,readable是可读的意思,但不代表不能写哦. getReadableDatabase() 会获取用于操作SQLiteDatabase的实例. getRe ...
- linux下 cmatrix的安装和使用
安装过程 wget http://www.asty.org/cmatrix/dist/cmatrix-1.2a.tar.gztar xvf cmatrix-1.2a.tar.gzcd cmatrix- ...
- 神奇的CSS3选择器
话说园子里也混迹多年了,但是基本没写过blog,写点基础的,那就从css3选择器开始吧. Css3选择器 先说下,为什么提倡使用选择器. 使用选择器可以将样式与元素直接绑定起来,在样式表中什么样式与什 ...
- New MVC World
Note: /Controllers:controllers respond to input from the browser,decide what to do with it,and retur ...
- spark RDD的元素顺序(ordering)测试
通过实验发现: foreach()遍历的顺序是乱的 但: collect()取到的结果是依照原顺序的 take()取到的结果是依照原顺序的 为什么呢???? 另外,可以发现: take()取到了指定数 ...
- oracle 所有下级
--所有下级 SELECT SAP_ORGAN_CODE FROM SAP_ORGAN_LEVEL CONNECT BY PRIOR SAP_FATHER_ORGAN_CODE= SAP_ORGAN_ ...
- batch 数字进制的问题
when set viable to number type in cmdexample: set /a num=0833echo %num% display: Invalid number. Nu ...
- sql防注入式
SQL注入式攻击是利用是指利用设计上的漏洞,在目标服务器上运行Sql命令以及进行其他方式的攻击动态生成Sql命令时没有对用户输入的数据进行验证是Sql注入攻击得逞的主要原因.比如: 如果你的查询语句是 ...
- Setup Project 安装项目
从vs2012起,微软已经不支持setup project了.以此纪念一下setup project. 在新建Setup Project 增加安装内容,通常是直接Oupput一个项目,或者直接 ...