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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- Difference between x:Reference and x:Name
{x:Reference ...} -> returns just a reference of an object it doesn't create that "bridge&qu ...
- python之道07
2.用户输入一个数字,判断一个数是否是水仙花数. 水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. 那这个数就是一个水仙花数, 例如: 153 = 1******3 + 5****** ...
- HashMap允许将null用作键 也允许将null作为值
HashMap不能保证元素的顺序,HashMap能够将键设为null,也可以将值设为null. 与之对应的是Hashtable,(注意大小写:不是HashTable),Hashtable不能将键和值设 ...
- 【dp】bzoj1613: [Usaco2008 Jan]Running贝茜的晨练计划
还记得这是以前看上去的不可做题…… Description 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1<=N<=10,000)分钟的晨跑.在 ...
- FTP实验报告
FTP实验报告 制作人:全心全意 准备工作: linux1:192.168.100.4 关闭防火墙.selinux机制 配置yum源 匿名访问 1.安装vsftpd服务和客户端 [root@local ...
- verilog behaviral modeling -- procedural timing contronls
1.delay control : an expression specifies the time duration between initially encountering the state ...
- lombok安装、配置、使用
eclipse安装Lombok 运行安装: java -jar ${path}\lombok.jar 选择IDE所在路径点击Install/Update即可使用. 手动安装1. 将lombok.jar ...
- ajax动态刷新下拉框
动态post,避免直接给页面传输大量数据 /** * ajax通过商品刷新供应商 * by_kangyx * @throws IOException */ @RequestMapping(params ...
- Objective-c 实例变量的访问级别
在C#和JAVA中无论是method还是variable都有严格的访问级别控制,那么在object-c中对访问级别的使用非常稀少,原因可能是因为在method上没有访问级别的语法,单单控制变量没有什么 ...
- IOS 自动布局-UIStackPanel和UIGridPanel(一)
我以前是做windows phone开发的,后来转做IOS的开发,因此很多windows phone上面的开发经验也被我带到了IOS中.其实有些经验本身跟平台无关,跟平台有关的无非就是实现方法而已.好 ...