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. 解决iScroll中事件点击一次却触发两次的问题

    var t1=null;//全局 function myClick() { if (t1 == null){ t1 = new Date().getTime(); }else{ var t2 = ne ...

  2. thinkphp 常见问题

    0.写在最前面的不断更新 (1)trace不起作用 A:必须要输出到模板,才会有trace信息 (2)提示“您浏览的页面暂时发生了错误!请稍后再试-” A:检查控制器(看看能进到控制器没有,设断点输出 ...

  3. Spring 注解回顾

    [copy] 参考资料 赵蒙

  4. 【原创教程】一、Angular教程系列之认识angular

    为什么我会准备写这个原创教程系列? 写下这个标题之后,看着屏幕上空白的内容区,不知从何下手,想说的似乎有很多,似乎又没啥说的.有时候就会陷入这种矛盾中,有时候就是这样,于是,我下定决心这一次一定要把这 ...

  5. Eclipse项目 迁移到 Intellj IDEA

    自从用了Intellj IDEA,很多项目都想迁移到Intellj上面去开发  鉴于我们的大部分项目都是基于Maven构建的,所以就可以利用maven的命令来做这个事情.     1.选择一个ecli ...

  6. 浅析MySQL中exists与in的使用 (写的非常好)

    转自http://sunxiaqw.blog.163.com/blog/static/990654382013430105130443/ exists对外表用loop逐条查询,每次查询都会查看exis ...

  7. 类库探源——System.Exception

    一.MSDN描述 Exception 类: 表示在应用程序执行期间发生的错误 命名空间 : System 程序集:   mscorlib.dll 继承关系: 常用属性(含字段)和方法: 1. 属性Me ...

  8. Flex:CSS3布局利器

    实习以来做了三个小控件,都是用的CSS2.1里传统的DIV+CSS布局方式,综合使用position.margin.float.BFC等属性或特性将元素放到指定的位置上.然而面对日益复杂的界面,这些来 ...

  9. 217. Contains Duplicate(C++)

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  10. Windows phone 之Xml操作

    最近在做天气预报应用,这个应用中需要用到Xml来存储关注的城市列表,保存一下代码,方便以后使用,也给博友们一个参考: 其中:添加关注城市的操作代码是: 其实就是, (1)先把数据从CareCityCo ...