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个城市将要提供的物资,和每条路的消耗百分比.求能送到首都的最多的物 ...
随机推荐
- api图片传输,转成64位字符串进行传输
byte[] getImageByte = HttpHelper.getImageByte(HttpContext.Current.Server.MapPath(("~/UploadFile ...
- 用Javascript评估用户输入密码的强度
<!-- 密码已经是我们生活工作中必不可少的工具,但一个不安全的密码有又有可能会给我们造成不必要的损失.作为网站设计者,如果我们在网页中能对用户输入的密码进行安全评估,并显示出相应的提示信息 ...
- Gradle插件
1.方法数统计 classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.1' apply plugin: 'com.getkeep ...
- Weex命令
1.下载安装 $ git clone https://github.com/alibaba/weex.git //通过brew安装node $ brew install node //通过node安装 ...
- 【转】Java中equals和==的区别
[转]Java中equals和==的区别 java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boole ...
- SqlSugar常用查询实例-拉姆达表达式
SqlSugar支持拉姆达表达式查询,匿名对象参数等,相对还是比较方便好用的. 一.查询列表: //查询列表 SqlSugarClient db = SugarContext.GetInstance( ...
- C语言之分配
#include "stdio.h" void main() { ] = {,,}; *sizeof(int));//malloc calloc relloc arr2[] = ; ...
- ACM HDU 2044 一只小蜜蜂
Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input 输入数据的第一 ...
- JavaScript学习总结【4】、JS深入
1.JS流程控制语句 (1).if 判断 if 语句是基于条件成立时才执行相应的代码. if...else 语句是在指定的条件成立时执行if后的代码,在条件不成立时执行else后的代码. if...e ...
- [CSS]列表属性(List)
CSS 列表属性(List) 属性 描述 CSS list-style 在一个声明中设置所有的列表属性. 1 list-style-image 将图象设置为列表项标记. 1 list-style- ...