ZOJ-3088 Easter Holidays

Easter Holidays

Time Limit: 1 Second       Memory Limit: 32768 KB       Special Judge

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
————————————————————集训15.2的分割线————————————————————
前言:做了整整一上午,发现题目读错了。
思路:既然前半个过程都是上升,后半个过程都是下降,那就分成两张图做好了。惯性思维导致一直认为上升和下降交叉在一起,浪费了大量时间。
看到1000个顶点,说明Floyd不行了。必须SPFA。跑两次,然后枚举出两端距离之商最大的。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
//卧槽!题目没看清,原来是先上后下!
const int N = 1005;
int n, m, k, tot[2];
int head[2][N], dis[2][N][N];
bool inq[N];
struct Node {
	 int v, w, next;
}edge[2][N];
int q[N], fa[2][N][N];

void init()
{
	tot[0] = tot[1] = 0;
	for(int i = 1; i <= n; i++) {
		head[0][i] = head[1][i] = -1;
	}
}

void add(int u, int v, int w, int who)
{
	edge[who][tot[who]].v = v; edge[who][tot[who]].w = w;
	edge[who][tot[who]].next = head[who][u]; head[who][u] = tot[who]++;
}

void spfa(int st, const int who)
{
	for(int i = 1; i <= n; i++) {
		inq[i] = false;
		if(who == 0) dis[who][st][i] = 0;
		else dis[who][st][i] = INF;
	}
	int fron = 0, rear = 1;
	q[fron] = st;
	dis[who][st][st] = 0; inq[st] = true;
	fa[who][st][st] = st;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		inq[u] = false;
		for(int i = head[who][u]; i != -1; i = edge[who][i].next) {
			int v = edge[who][i].v;
			if(who == 0) {
	
				if(dis[who][st][v] < dis[who][st][u] + edge[who][i].w) {
					dis[who][st][v] = dis[who][st][u] + edge[who][i].w;
					fa[who][st][v] = u;
					if(!inq[v]) {
						q[rear%N] = v; rear++;
						inq[v] = true;
					}
				}
			}
			else if(who == 1) {
	
				if(dis[who][st][v] > dis[who][st][u] + edge[who][i].w) {
					dis[who][st][v] = dis[who][st][u] + edge[who][i].w;
					fa[who][st][v] = u;
					if(!inq[v]) {
						q[rear%N] = v; rear++;
						inq[v] = true;
					}
				}
			}
		}
	}
}

void PR(int st, int ed, int who)
{
	if(st == ed) return ;
	PR(fa[who][ed][st], ed, who);
	printf(" %d", st);
}

int main()
{	
#ifdef J_Sure
	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	int u, v, w;
	while(T--) {
		scanf("%d%d%d", &n, &m, &k);
		init();
		for(int i = 1; i <= m; i++) {
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w, 0);
		}
		for(int i = 1; i <= k; i++) {
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w, 1);
		}
		for(int i = 1; i <= n; i++) {
			spfa(i, 0);
			spfa(i, 1);
		}
		int st, ed;
		double ans = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) if(i != j) {
				if(dis[1][i][j] < INF) {
					if(1.0*dis[0][j][i]/dis[1][i][j] > ans) {
						st = i; ed = j;
						ans = 1.0*dis[0][j][i]/dis[1][i][j];
				
					}
				}
			}
		}
		printf("%d", st);
		PR(ed, st, 1);
		PR(st, ed, 0);
		printf("\n%.3f\n", ans);
	}
	return 0;
}


你可能感兴趣的:(ACM-ZOJ,SPFA,建图)