PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力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)的更多相关文章
- 【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 (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- [图的遍历&多标准] 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)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- 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[图论][迪杰斯特拉+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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- CPP-基础:单目运算符重载
关于++运算符前置和后置重载的实现实例: #include <iostream> using namespace std; //创建时钟类 class Clock { public: Cl ...
- shell脚本,逻辑结构题练习。
awk '/5/{a=1}!a' file2结果:1234解释:第一行 /5/不匹配跳过{a=1},继续!a,此时a没有值属于假取反为真,故输出第一行 第二行 /5/不匹配跳过{a=1},继续!a,此 ...
- C++的反射
写得挺不错,支持转帖下 C++语言本身是不支持反射的,但实际应用中总是会有将对象序列化的需求,总不可能C++不支持,我们就不用C++了,既然发明C++的大师们没有考虑这个,那我们只有自己动手了,毛主席 ...
- iOS面试集锦3
1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEnco ...
- baidumap demo(一)
覆盖物概述 地图上自定义的标注点和覆盖物我们统称为地图覆盖物.您可以通过定制BMKAnnotation和BMKOverlay来添加对应的标注点和覆盖物.地图覆盖物的设计遵循数据与View分离的原则,B ...
- Bzoj 2752 高速公路 (期望,线段树)
Bzoj 2752 高速公路 (期望,线段树) 题目链接 这道题显然求边,因为题目是一条链,所以直接采用把边编上号.看成序列即可 \(1\)与\(2\)号点的边连得是. 编号为\(1\)的点.查询的时 ...
- 异步FIFO最小深度计算
计算FIFO深度是设计FIFO中常遇到的问题.常识告诉我们,当读速率慢于写速率时,FIFO便可被用作系统中的缓冲元件或队列.因此FIFO的大小基本上暗示了所需缓存数据的容量,该容量取决于读写数据的速率 ...
- 【php】运算符优先级界定
<?php $i = 1; $array[$i] = $i++; print_r($array);die; //输出 Array([2] => 1) $a = 1; echo $a + $ ...
- pytorch导入错误so: undefined symbol: _Z11libshm_initPKc
首先删除torch文件 或者直接卸载 删除会更彻底 https://blog.csdn.net/qq_37674858/article/details/88870124 但是会发现卸载重装pytorc ...
- 记如何解决蓝桥杯中to_string和atoi等无法使用的问题
#include<iostream> #include<sstream> using namespace std; int main() { // int 转 string ...