PAT1087. All Roads Lead to Rome
PAT1087. All Roads Lead to Rome
题目大意
给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大.
思路
先通过 Dijkstra 求得最短路径, 需要注意的是: 要保证每次松弛时 u 和 v 不相同, 否则会形成自环, 则从 ROM 开始 BFS 遍历每一条边权相同的路径.
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
#define MAXN 300
#define INF 0x7ffffff
int nVertex, nEdge;
map<string, int> s_i;
map<int, string> i_s;
vector<int> prepath[MAXN], temppath, anspath;
int vw[MAXN];
int ew[MAXN][MAXN];
int dis[MAXN];
int isVis[MAXN];
int cntRo = 0;
int ansHapy = 0;
double ansAvg = 0;
void dfs(int loc){
temppath.push_back(loc);
if(loc == 0){
int hapy = 0;
for(int i = 0; i < temppath.size(); i++)
hapy += vw[temppath[i]];
double avgHapy = hapy * 1.0 / (temppath.size() - 1);
if(hapy > ansHapy){
ansHapy = hapy;
ansAvg = avgHapy;
anspath = temppath;
}
else if(hapy == ansHapy && avgHapy > ansAvg){
ansAvg = avgHapy;
anspath = temppath;
}
cntRo++;
temppath.pop_back();
return;
}
for(int i = 0; i < prepath[loc].size(); i++){
dfs(prepath[loc][i]);
}
temppath.pop_back();
}
int main(){
scanf("%d%d", &nVertex, &nEdge);
string tempStr; cin >> tempStr;
s_i[tempStr] = 0; i_s[0] = tempStr;
for(int i = 1; i < nVertex; i++){
cin >> tempStr;
s_i[tempStr] = i; i_s[i] = tempStr;
scanf("%d", &vw[i]);
}
for(int i = 0; i < MAXN; i++){
for(int j = 0; j < MAXN; j++){
ew[i][j] = (i == j ? 0 : INF);
}
}
for(int i = 0; i < nEdge; i++){
string a, b; int c;
cin >> a >> b >> c;
ew[s_i[a]][s_i[b]] = ew[s_i[b]][s_i[a]] = c;
}
for(int i = 0; i < nVertex; i++)
dis[i] = ew[0][i];
dis[0] = 0;
for(int i = 0; i < nVertex; i++){
int u = -1, minn = INF;
for(int j = 0; j < nVertex; j++){
if(!isVis[j] && dis[j] < minn){
minn = dis[j];
u = j;
}
}
isVis[u] = 1;
for(int v = 0; v < nVertex; v++){
if(u != v)
{
if(dis[v] > ew[u][v] + dis[u]){
dis[v] = ew[u][v] + dis[u];
prepath[v].clear();
prepath[v].push_back(u);
}
else if(dis[v] == ew[u][v] + dis[u]){
prepath[v].push_back(u);
}
}
}
}
int rom = s_i["ROM"];
dfs(rom);
printf("%d %d %d %d\n", cntRo, dis[rom], ansHapy, (int)ansAvg);
for(int i = anspath.size() - 1; i != 0; i--){
cout << i_s[anspath[i]] << "->";
}
printf("ROM");
return 0;
}
PAT1087. All Roads Lead to Rome的更多相关文章
- 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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- 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 ...
- [图的遍历&多标准] 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 ...
- 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(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- PAT_A1087#All Roads Lead to Rome
Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- A1087. All Roads Lead to Rome
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
随机推荐
- Java性能调优-jstack-jstat-jmap
0. 必须在java进程的用户下执行 a). 先排查自己业务代码,再第三方的开源代码 b). 工具类都在jdk/bin目录下, 实现代码在tools.jar中 1. jstack-线程快照-死锁/阻塞 ...
- nyoj 211——Cow Contest——————【floyd传递闭包】
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...
- ebiao 报表工具使用入门
一.ebiao简价 e表是一个功能强大的Web报表工具,可使复杂报表的设计简单化,避免了大量的复杂SQL编写以及编程来准备数据,报表设计的效率大大提高.e表分为e表 for .NET和e表 for J ...
- 事件代理总结: 已经有一些使用主流类库的事件代理示例出现了,比如说jQuery、Prototype以及Yahoo! UI。你也可以找到那些不用任何类库的例子,比如说Usable Type blog上的这一个。一旦需要的话,事件代理将是你工具箱里的一件得心应手的工具,而且它很容易实现。
如果你想给网页添加点JavaScript的交互性,也许你已经听过JavaScript的事件代理(event delegation),并且觉得这是那些发烧友级别的JavaScript程序员才会关心的什么 ...
- Android界面编程--使用活动条(ActionBar)--添加Action View
ActionBar除了显示Action Item 外,还能显示普通的ui组件 2种方式添加Action View 1.指定ActionView的实现类 2.指定ActionView对应的视图资源 实现 ...
- 研究SSIS时候的一些常见错误
1.[OLE DB 目标 [59]] 错误: SSIS 错误代码 DTS_E_OLEDBERROR.出现 OLE DB 错误.错误代码: 0x80004005.已获得 OLE DB 记录.源:“Mic ...
- MVC页面简单post提交
页面代码 <script src="~/Scripts/jquery-1.10.2.js"></script> <script> $(funct ...
- linux防火墙与端口设置
1.编辑iptables文件 # sudo vi /etc/sysconfig/iptables 添加如下一行 -A INPUT -p tcp -m state --state NEW -m tcp ...
- python学习(七)--豆瓣爬取电影名,评分以及演员
import requestsimport re #爬取豆瓣电影排名pageNum = int(input("要查看第几页电影分数:"))#已知豆瓣默认每页展示20条#url= & ...
- 使用Spring的AOP实现切面日志
AOP切面日志的使用方式 @Aspect @Component public class HttpAspect { private static final Logger logger = Logge ...