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(最长路+最短路+打印路径)的更多相关文章

  1. ZOJ 3795 Grouping(scc+最长路)

    Grouping Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose there are N people in ZJU, whose ...

  2. 最长公共子序列Lcs(打印路径)

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...

  3. codeforces mysterious present 最长上升子序列+倒序打印路径

    link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...

  4. 牛客网NOIP赛前集训营-提高组(第六场)-A-最长路[拓扑排序+hash+倍增]

    题意 给定一个 \(n\) 点 \(m\) 边的边权非负的有向图,边有字符,求以每个点为开头的最长路字典序最小的路径 \(hash\) 值. \(n,m\leq 10^6\) 分析 首先建反图拓扑排序 ...

  5. 洛谷 P1807 最长路_NOI导刊2010提高(07)题解

    相当与一个拓扑排序的模板题吧 蒟蒻的辛酸史 题目大意:给你一个有向无环图,让你求出1到n的最长路,如果没有路径,就输出-1 思路:一开始以为是一个很裸的拓扑排序 就不看题目,直接打了一遍拓扑排序 然后 ...

  6. zoj 3795 Grouping tarjan缩点 + DGA上的最长路

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practic ...

  7. Grouping ZOJ - 3795 (tarjan缩点求最长路)

    题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...

  8. ZOJ 3795 Grouping (强连通缩点+DP最长路)

    <题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...

  9. ZOJ - 1655 Transport Goods(单源最长路+迪杰斯特拉算法)

    题目: 有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资.问给定N-1个城市将要提供的物资,和每条路的消耗百分比.求能送到首都的最多的物 ...

随机推荐

  1. 如何让android sdk manager飞奔安装sdk

    由于国内的gwf的原因,凡是google的服务都是无法正常访问,这两天从android官网上下载sdk不是一般的慢.不过现在有一个好的方法.sdk都是从https://dl-ssl.google.co ...

  2. Dom4j解析Xml文件,Dom4j创建Xml文件

    Dom4j解析Xml文件,Dom4j创建Xml文件 >>>>>>>>>>>>>>>>>>&g ...

  3. app抓包

    http://www.360doc.com/content/14/1126/11/9200790_428168701.shtml 记得下载证书  不然有些网站是抓不到的

  4. SQL小细节

    平时有些小细节,不留意的话很容易得到错误的答案,我们来看下下面的代码,看看你是否能答对呢? ) ,) SELECT @str = '中国CH',@info='MyTest' SELECT [字符串]= ...

  5. (六)Struts2 国际化

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:国际化简介 国际化(Internationlization),通俗地讲, ...

  6. .NET生成静态页面例子

    主要做法如下: 1.创建网站,并创建一个模板页,template.htm 2.添加一个web窗体Default.aspx 3.在网站下新建文件夹htm,设置该文件夹的属性,确保该文件夹具有可写权限 详 ...

  7. 【工具篇】source Insight

    不多说,阅读代码利器. 一.修改背景颜色 使用淡绿色更护眼(听说而已),菜单“选项”>>“属性”,使用自己喜欢的颜色吧.我的淡绿色RGB是181,236,207 二.行号,空格替换tabs ...

  8. HDU1862EXCEL排序

    其实最近都没有兴趣做排序题目,因为我觉得纯粹排序对我而言进步不大,但是舍友TLE了,叫我试一试. 整道题的思路很简单啦,我用的是快排,比较的原则也给得很清楚,不必多言,我没有用stdlib的快排,也没 ...

  9. 获得SQLSERVER的表字段等架构信息

    获得SQLSERVER的表字段等架构信息 SELECT 表名 = CASE WHEN A.COLORDER=1 THEN D.NAME ELSE '' END, 表說明 = CASE WHEN A.C ...

  10. 虚拟化技术与"云"

    虚拟化技术: 如网站在某一时间访问量大,平时访问量少,如果一直保持大量的服务器提供服务,显示效率好低,浪费资源,在 不增减服务器,存储设备,网络等实际物理设备,而是利用软件将这些物理设备虚拟化,在有必 ...