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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- NYOJ-06-喷水装置(一)
http://acm.nyist.net/JudgeOnline/problem.php?pid=6 喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现有 ...
- blog.yiz96.com
欢迎访问我的新博客 blog.yiz96.com
- #include <> 和 #inlude ""的区别
#include < >引用的是编译器的类库路径里面的头文件#include " "引用的是你程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继 ...
- shell脚本,一个字符一个字符输出。
[root@localhost wyb]# cat file abc def abc 789de f567 [root@localhost wyb]# cat fffile.sh #!/bin/bas ...
- Log4J的配置与使用详解
一.简介 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务器.NT的事件记录器.UNIX Syslog守护 ...
- 在Xcode中编辑运行 Python 脚本
http://www.zhihu.com/question/19872198 打开Xcode,File->New->Project选中OS X下的Other点击External Build ...
- laravel富文本编辑和图片上传
---恢复内容开始--- 首先先找到一个适合的编辑器是胜利的一步,选择wangEditor这个编辑器 地址:http://www.wangeditor.com/ 然后选择下载,我是通过网上学习的,所以 ...
- JS(异步与单线程)
JS(异步与单线程) 题目1.同步和异步的区别是什么,试举例(例子见知识点) 区别: 1.同步会阻塞代码执行,而异步不会 2.alert 是同步,setTimeout 是异步 题目2.关于 setTi ...
- skkyk:线段树浅谈
推荐前辈学姐博客文章,写的很细 https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html 学学半,此随笔主要是加深自己对线段树的理解 题目:洛谷P ...
- mac下secureCRT 客户端 $redis-cli回车后没有反应的解决办法
启动redis server后,SecureCRT进入redis-cli,输入不断在后面追加IP:Port显示设置当前的Session Options-->Terminal-->Emula ...