zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are provides fantastic ski conditions, many ski lifts and slopes of various difficulty profiles. However, some lifts go faster than others, and some are so popular that a queue forms at the bottom.
Per is a beginner skier and he is afraid of lifts, even though he wants to ski as much as possible. Now he sees that he can take several different lifts and then many different slopes or some other lifts, and this freedom of choice is starting to be too puzzling...
He would like to make a ski journey that:
- starts at the bottom of some lift and ends at that same spot
- has only two phases: in the first phase, he takes one or more lifts up, in the second phase, he will ski all the way down back to where he started
- is least scary, that is the ratio of the time spent on the slopes to the time spent on the lifts or waiting for the lifts is the largest possible.
Can you help Per find the least scary ski journey? A ski resort contains n places, m slopes, and k lifts (2 <= n <= 1000, 1 <= m <= 1000, 1 <= k <= 1000). The slopes and lifts always lead from some place to another place: the slopes lead from places with higher altitude to places with lower altitude and lifts vice versa (lifts cannot be taken downwards).
Input
The first line of the input contains the number of cases - the number of ski resorts to process. Each ski resort is described as follows: the first line contains three integers n, m, and k. The following m lines describe the slopes: each line contains three integers - top and bottom place of the slope (the places are numbered 1 to n), and the time it takes to go down the slope (max. 10000). The final k lines describe the lifts by three integers - the bottom and top place of the lift, and the time it takes to wait for the lift in the queue and be brought to its top station (max. 10000). You can assume that no two places are connected by more than one lift or by more than one slope.
Output
For each input case, the program should print two lines. The first line should contain a space-separated list of places in the order they will be visited - the first place should be the same as the last place. The second line should contain the ratio of the time spent in the slopes to the time spent on the lifts or wating for the lifts. The ratio should be rounded to the closest 1/1000th. If there are two possibilities, then the rounding is away from zero (e.g., 1.9812 and 1.9806 become 1.981, 3.1335 becomes 3.134, and 3.1345 becomes 3.135). If there are multiple journeys that prior to rounding are equally scary, print an arbitrary one.
Sample Input
1
5 4 3
1 3 12
2 3 6
3 4 9
5 4 9
4 5 12
5 1 12
4 2 18
Sample Output
4 5 1 3 4
0.875
题意:
给你一个滑雪场,有两种边(用雪橇上升可看做一种边,从雪坡滑下来可看做一种边),让你找到两点A、B,A->B为经过第一种边上去,所需时间t1,B->A为经过第二种边下来,所需时间t2,使得t2/t1最大。
思路:
用两种边分别建两个图,用n次SPFA找到一个图中的任意一点到任意一点的最短路,再用n次SPFA找到另一个图中的任意一点到任意一点的最长路,然后枚举取得t2/t1的最大值就够了。
这肯定是我写的最揪心的一次代码了,自己写着写着就乱了。。。要注意的是递归打印路径 以及邻接表的建立,在结构体数组里多加了一个变量next,具体用法见加边函数。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int INF = 0x3f3f3f3f;
const int maxn = ;
struct node
{
int v,w;
int next;
}edge1[maxn],edge2[maxn]; int n,m,k;
int cnt;//边数;
int p[maxn];//保存与起始顶点u有边相连的那些边对应的下标,相当于邻接表;
int dis1[maxn][maxn],dis2[maxn][maxn];//dis[i][j]表示i为源点到j的最短路;
int parent1[maxn][maxn],parent2[maxn][maxn];//parent[i][j]表示以i为源点,j的前驱
int start,end;
queue<int>que; void init()
{
memset(parent1,,sizeof(parent1));
memset(parent2,,sizeof(parent2));
memset(dis1,,sizeof(dis1));
memset(dis2,INF,sizeof(dis2));
}
void add_edge_1(int u, int v, int w)//建第一个图
{
cnt++;
edge1[cnt].v = v;
edge1[cnt].w = w;
edge1[cnt].next = p[u];
p[u] = cnt;
} void add_edge_2(int u, int v, int w)//建第二个图
{
cnt++;
edge2[cnt].v = v;
edge2[cnt].w = w;
edge2[cnt].next = p[u];
p[u] = cnt;
}
void spfa1(int s)//spfa求最长路,dis1[][]初始化为最小
{
int inque[maxn];
while(!que.empty())
que.pop();
memset(inque,,sizeof(inque)); parent1[s][s] = s;
dis1[s][s] = ;
que.push(s);
inque[s] = ; while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ; for(int i = p[u];i;i = edge1[i].next)//注意p数组在这里的用法,找所有与u相连的边的下标
{
if(dis1[s][edge1[i].v] < dis1[s][u] + edge1[i].w)
{
dis1[s][edge1[i].v] = dis1[s][u] + edge1[i].w;
parent1[s][edge1[i].v] = u;
if(!inque[edge1[i].v])
{
inque[edge1[i].v] = ;
que.push(edge1[i].v);
}
}
}
}
} void spfa2(int s)//spfa求最短路,dis2[][]初始化为最大
{
int inque[maxn];
while(!que.empty()) que.pop();
memset(inque,,sizeof(inque)); parent2[s][s] = s;
dis2[s][s] = ;
que.push(s);
inque[s] = ; while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ; for(int i = p[u]; i; i = edge2[i].next)
{
if(dis2[s][edge2[i].v] > dis2[s][u] + edge2[i].w)
{
dis2[s][edge2[i].v] = dis2[s][u] + edge2[i].w;
parent2[s][edge2[i].v] = u;
if(!inque[edge2[i].v])
{
inque[edge2[i].v] = ;
que.push(edge2[i].v);
}
}
}
}
}
void output2(int e, int s)//递归打印路径2(A->B)
{
if(e == s)
printf("%d",e);
else
{
output2(parent2[s][e],s);
printf(" %d",e);
}
}
void output1(int e,int s) //递归打印路径1(B->A)
{
if(e==s) return;
else
{
output1(parent1[s][e],s);
printf(" %d",e);
}
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
scanf("%d %d %d",&n,&m,&k);
init(); cnt = ;
memset(p,,sizeof(p));
for(int i = ; i < m; i++)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
add_edge_1(u,v,w);
}
for(int i = ; i <= n; i++)
{
spfa1(i);
} cnt = ;
memset(p,,sizeof(p));
for(int i = ; i < k; i++)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
add_edge_2(u,v,w);
}
for(int i = ; i <= n; i++)
{
spfa2(i);
} double ans = ;
//枚举查找满足t2/t1最大的起始点A,B;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(i == j || dis2[i][j] == INF)
continue;
if(dis1[j][i]*1.0/dis2[i][j] > ans)
{
ans = dis1[j][i]*1.0/dis2[i][j];
start = i;//A
end = j;//B
}
}
}
output2(end,start);
output1(start,end);
printf("\n");
printf("%.3lf\n",ans);
}
return ;
}
zoj 3088 Easter Holidays(最长路+最短路+打印路径)的更多相关文章
- ZOJ 3795 Grouping(scc+最长路)
Grouping Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose there are N people in ZJU, whose ...
- 最长公共子序列Lcs(打印路径)
给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...
- codeforces mysterious present 最长上升子序列+倒序打印路径
link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...
- 牛客网NOIP赛前集训营-提高组(第六场)-A-最长路[拓扑排序+hash+倍增]
题意 给定一个 \(n\) 点 \(m\) 边的边权非负的有向图,边有字符,求以每个点为开头的最长路字典序最小的路径 \(hash\) 值. \(n,m\leq 10^6\) 分析 首先建反图拓扑排序 ...
- 洛谷 P1807 最长路_NOI导刊2010提高(07)题解
相当与一个拓扑排序的模板题吧 蒟蒻的辛酸史 题目大意:给你一个有向无环图,让你求出1到n的最长路,如果没有路径,就输出-1 思路:一开始以为是一个很裸的拓扑排序 就不看题目,直接打了一遍拓扑排序 然后 ...
- zoj 3795 Grouping tarjan缩点 + DGA上的最长路
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practic ...
- Grouping ZOJ - 3795 (tarjan缩点求最长路)
题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...
- ZOJ 3795 Grouping (强连通缩点+DP最长路)
<题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...
- ZOJ - 1655 Transport Goods(单源最长路+迪杰斯特拉算法)
题目: 有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资.问给定N-1个城市将要提供的物资,和每条路的消耗百分比.求能送到首都的最多的物 ...
随机推荐
- Go语言学习资料汇总
网站: Go语言官网(访问)(中文镜像) Go语言中文网(访问) Go编译器(访问) Go语言中国社区(访问) golanghome(访问) GoLang中国(访问) Gopher Academic( ...
- LAMP网站架构方案分析
本文引自:http://www.williamlong.info/archives/1908.html LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框 ...
- 日志记录组件[Log4net]详细介绍
转载:http://www.cnblogs.com/liwei6797/archive/2007/04/27/729679.html 因为工作中有要用到Log记录,找到一篇不错的文章,就转了过来. 一 ...
- ubuntu出现有线已连接却无法上网
或者直接追加到/etc/sysctl.conf 如果遇到“设备未托管”,一般是台式机默认移动ip后禁用网络. 那么修改/etc/NetworkManager/NetworkManager.conf,设 ...
- hibernate中一对多Set的排序问题
因为set是无序的,一旦涉及set排序,就需要配置hibernate的配置文件,参考如下博文 http://ykyfendou.iteye.com/blog/2094325
- 淘宝链接中的spm参数
什么是SPM SPM是淘宝社区电商业务(xTao)为外部合作伙伴(外站)提供的一套跟踪引导成交效果数据的解决方案. 下面是一个跟踪点击到宝贝详情页的引导成交效果数据的SPM示例: http://det ...
- CoreAnimation实现一个折线表
将折现表封装到一个view里,暴露给使用者的只有一个传入数据的方法. // // ChartLine.h // BoxingChampion //功能:根据传入的数组,绘制折线图 注意 其frame的 ...
- PHP设计模式之:建造者模式
建造者模式: 将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示的设计模式; 目的: 消除其他对象复杂的创建过程 结构图: 优点: 建造者模式可以很好的将一个对象的实现与相关的“业 ...
- Python传参数最简单易懂的描述
关于,python的传参,很多人会搞得一头雾水,我也跟朋友讨论多次,最终通过实验,得到结论. 一.所有传递都是引用传递 二.在函数内使用[变量名]=,相当于定义啦一个局部变量 OK,一段简单的 ...
- windows phone 之ListBox模板选择
有时做项目时,会遇到一种情况:需要把获取到的数据集合显示到首页,比如新闻,但是: 新闻也分类别啊,比如:图片类新闻.文字类新闻.视频类新闻. 那我们可能采用的模板就不一样了,那么,如何根据类别来选择模 ...