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常用错误解决汇总

    一.No active compatible AVD's or devices found. Relaunch this configuration after connecting a device ...

  2. Android 读取txt文件并以utf-8格式转换成字符串

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 # 使用EncodingUtils 今天用到了城市选择三级联动的库,用的这个:https://gi ...

  3. CSS jQuery HTML5 CSS3

    jquery css3图片文字介绍鼠标滚动页面动画单页 http://www.17sucai.com/preview/1/2013-12-30/%E5%8A%A8%E7%94%BB%E5%8D%95% ...

  4. iOS:iOS开发中用户密码保存位置

    原文来自简书:http://www.jianshu.com/p/4af3b8179136/comments/1294203 如果要实现自动登录,不必每次打开应用都去登录,我们势必要把密码保存到本地.一 ...

  5. The version of CocoaPods used to generate the lockfile (*) is higher than the version of the current executable (*). Incompatibility issues may arise.

    解决方法: sudo gem update cocoapod

  6. java经典题目练习-第八题简单实现方式...

    *[程序8]*题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.* 例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制. 思考: 对于以上 ...

  7. Lucene5.x 中文 同义词

    查询好好多资料,英文同义词好好的,中文就不行,多谢网友支持,拼接了好多代码,然后修改了一些,不足之处,多谢指正. 直接上代码吧,在代码中了解怎么分词的最好 1,创建分词引擎 public interf ...

  8. HDU1862EXCEL排序

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

  9. Mediator 模式

    在面向对象系统的设计和开发过程中,对象之间的交互和通信是最为常见的情况,因为对象间的交互本身就是一种通信.在系统比较小的时候,可能对象间的通信不是很多.对象也比较少,我们可以直接硬编码到各个对象的方法 ...

  10. 【FJOI2014】【偏导+数学】病毒防护带

    转载:http://trinklee.blog.163.com/blog/static/23815806020150155296528/ 问题描述: 众所周知,在国王胖哥的带领下,K国国泰民安,空前繁 ...