暴力DFS。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn=+;
int n,m;
string st;
map<string ,int>m1;
map<int ,string>m2;
int sz;
int h[maxn];
struct Edge
{
int u,v,c;
}e[maxn*maxn];
int tot;
vector<int>g[maxn]; int des;
int ans_cost=0x7FFFFFFF;
int ans_count=;
int ans_happy=;
int ans_point=;
int path[maxn],ans_path[maxn];
bool flag[maxn]; void dfs(int x,int cost,int happy,int point)
{
if(cost>ans_cost) return;
if(x==des)
{
if(cost<ans_cost)
{
ans_cost=cost;
ans_count=;
ans_happy=happy;
ans_point=point;
for(int i=;i<point;i++)
ans_path[i]=path[i];
} else if(cost==ans_cost)
{
ans_count++;
if(happy>ans_happy)
{
ans_happy=happy;
ans_point=point;
for(int i=;i<point;i++)
ans_path[i]=path[i];
} else if(happy==ans_happy)
{
if(point<ans_point)
{
ans_point=point;
for(int i=;i<point;i++)
ans_path[i]=path[i];
}
}
}
return;
} for(int i=;i<g[x].size();i++)
{
int id=g[x][i];
path[point]=e[id].v;
if(flag[e[id].v]==) continue;
flag[e[id].v]=;
dfs(e[id].v,cost+e[id].c,happy+h[e[id].v],point+);
flag[e[id].v]=;
}
} int main()
{
scanf("%d%d",&n,&m); cin>>st;
m1[st]=++sz; m2[sz]=st; for(int i=;i<=n-;i++)
{
string name; cin>>name;
m1[name]=++sz; m2[sz]=name;
int val; scanf("%d",&val);
h[sz]=val;
} des=m1["ROM"]; tot=;
for(int i=;i<=m;i++)
{
string U,V; int c; cin>>U>>V>>c;
e[tot++].u=m1[U]; e[tot].v=m1[V]; e[tot].c=c;
g[m1[U]].push_back(tot); e[tot++].u=m1[V]; e[tot].v=m1[U]; e[tot].c=c;
g[m1[V]].push_back(tot);
} memset(flag,,sizeof flag);
flag[]=;
dfs(,,,); printf("%d %d %d %d\n",ans_count,ans_cost,ans_happy,ans_happy/ans_point); cout<<st;
for(int i=;i<ans_point;i++)
cout<<"->"<<m2[ans_path[i]];
printf("\n"); return ;
}

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)的更多相关文章

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

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

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

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

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

  4. 1087. All Roads Lead to Rome (30)

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

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

  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. Bootstrap 静态控件

    当您需要在一个水平表单内表单标签后放置纯文本时,请在 <p> 上使用 class .form-control-static. 实例: <!DOCTYPE html><ht ...

  2. ios 注册功能研究学习

    通常,移动App的注册功能通常采用手机号码注册或者邮箱帐号注册. 不过在国内这样隐私堪忧的环境下,需要手机号来注册会流失不少用户.即便是新浪微博这样的应用,需要绑定手机号也令我不信任.除非是像淘宝.支 ...

  3. Luogu P2664 树上游戏 dfs+树上统计

    题目: P2664 树上游戏 分析: 本来是练习点分治的时候看到了这道题.无意中发现题解中有一种方法可以O(N)解决这道题,就去膜拜了一下. 这个方法是,假如对于某一种颜色,将所有这种颜色的点全部删去 ...

  4. python 中requests 模块用py2exe生成exe后SSL certificate exception的问题

    [('system library', 'fopen', 'No such process'), ('BIO routines', 'BIO_new_file', 'no such file'), ( ...

  5. 什么是php?php的优缺点有哪些?与其它编程语言的优缺点?

    身为一个PHP开发者,有必要了解一下PHP的缺点,知道每种语言的优点和缺点,才能知道某种语言在什么场景下适合使用,在什么场景下不适合使用. 这个问题我曾经面试的时候遇到过,我之前没总结过,第一问大部分 ...

  6. LIN总线协议

    汽车电子类的IC有的采用LIN协议来烧录内部NVM,如英飞凌的TLE8880N和博世的CR665D. LIN总线帧格式如下,一个LIN信息帧有同步间隔.同步域.标示符域(PID域).数据域.校验码域. ...

  7. Oracle常用查询语句

    "ORACLE数据字典视图的种类分别为:USER,ALL 和 DBA. USER_*:有关用户所拥有的对象信息,即用户自己创建的对象信息 ALL_*:有关用户可以访问的对象的信息,即用户自己 ...

  8. Eclipse安装以及安装时遇到的问题解决办法

    1, 首先要安装JDK(最好使用最新版本),注意区分32位于64位 2, 安装程序,双击打开安装即可 3, 安装包下载:http://developer.android.com/sdk/index.h ...

  9. 七丶人生苦短,我用python【第七篇】

    模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...

  10. python第三方库之openpyxl(1)

    python第三方库之openpyxl(1) 简介 Openpyxl是一个用于读写Excel 2010 xlsx/xlsm/xltx/xltm文件的Python库,其功能非常强大.Excel表格可以理 ...