时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  1. [图的遍历&多标准] 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 ...

  2. 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 ...

  3. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  4. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  5. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  6. 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 ...

  7. pat1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  8. 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 ...

  9. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

随机推荐

  1. Java基础知识强化103:Java常量池理解与总结

    一.相关概念 1. 什么是常量 用final修饰的成员变量表示常量,值一旦给定就无法改变! final修饰的变量有三种:静态变量.实例变量和局部变量,分别表示三种类型的常量. 2. Class文件中的 ...

  2. 记第一次web前端校招笔试

    是的,我今晚跑到隔壁学校参加某电商公司的宣讲会+现场笔试.只有俩字可以形容:苦笑! 在寝室复习了下以前学习的关于前端方面的知识,重点是JavaScript,javaweb开发技术(jsp+servle ...

  3. spring+hibernate+struts整合(2)

    spring和struts2的整合 1:配置Web.xml文件 <filter> <filter-name>struts2</filter-name> <fi ...

  4. rpm命令使用说明

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...

  5. js验证 button 提交

    <form class="form-horizontal" role="form" action="member_add" metho ...

  6. Web前端学习笔记2

    一.开发工具sublime的常用快捷键. 1.                     快捷键                        功能 ctrl+shift+D 快速复制 ctrl+L 快 ...

  7. Ehcache(2.9.x) - API Developer Guide, Using Explicit Locking

    About Explicit Locking Ehcache contains an implementation which provides for explicit locking, using ...

  8. Android实现贪吃蛇游戏

    [绥江一百]http://www.sj100.net                                                  欢迎,进入绥江一百感谢点击[我的小网站,请大家多 ...

  9. Delphi IP 控件源码

    interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ComCtrls, Co ...

  10. 页面所有的button绑定同一个事件,点击不同的button赋值不同

    <script type="text/javascript"> $(function(){ $("input[type='button']").cl ...