WEEK 7 B TT's travel diary

Title description

As we all know, TT has a magic cat.

Today he opened a live broadcast of the trip on Station B, recording his adventures with the magic cat while traveling on the cat. TT sets off from home and prepares to take the Cat Express to Meow Star Airport. The cat cat express line is divided into economic line and commercial line, their speed and price are different. Of course, the commercial line is more expensive than the economic line. TT can usually only take the economic line, but today the magic cat of TT has changed to a commercial line ticket and can take a commercial line. Assuming that the time for TT transfer is negligible, please help TT find the fastest route to Miaoxing Airport, otherwise you will miss the plane!

Input

The input contains multiple sets of data. The first line of each set of data is 3 integers N, S and E (2 ≤ N ≤ 500, 1 ≤ S, E ≤ 100), which is the total number of stations, starting point and end point (that is, the station where the Meowing Airport is located) )Numbering.

The next line contains an integer M (1 ≤ M ≤ 1000), which is the number of sections of the economic line.

Next there are M lines, each line of 3 integers X, Y, Z (1 ≤ X, Y ≤ N, 1 ≤ Z ≤ 100), which means that TT can take the economic line to and from station X and station Y, of which one way It takes Z minutes.

The number of road segments in the next line of the commercial line is K (1 ≤ K ≤ 1000).

The next line K is a description of the commercial line segment, the format is the same as the economic line.

All sections are bidirectional, but it may be necessary to use a commercial ticket to reach the airport. Ensure that the optimal solution is unique.

Output

For each set of data, output 3 lines. The first line gives the stations (including the start point and the end point) that TT passes through in the order of access. The second line is the station number of the TT transfer to the commercial line (if the commercial line ticket is not used, output "Ticket Not Used" without quotation marks) ), The third line is the total time spent by TT to the Meow Star Airport.

This question does not ignore the extra spaces and tabs, and a newline should be output between each set of answers

Input sample

4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3

Sample output

1 2 4
2
5

Ideas:


There are two different routes in the two Dijkstra questions. If there is no commercial route, we can directly consider the distance from the start point to the end point. Since business lines exist and can only be used once, we can enumerate each business line (a, b). We run the shortest route from the starting point, record the dis1 array, then run the shortest route from the end point, record the dis2 array, and then for the current business line (a, b), the weight is w, which is the shortest path through the current business line It is min (dis1 [a] + dis2 [b] + w, dis1 [b] + dis2 [a] + w). Finally, compare all commercial lines with non-used commercial lines and take the minimum value.
The point of this question is how to output, I am dull, so here I refer to the code of the big brother: the portal

Code:

#include <iostream>
#include <queue>
#include <vector>
#include <string.h>

using namespace std;

const int inf = 1e8;
const int M = 1e5+10;
const int N = 505;
vector <int> v;
struct edge
{
	int to, next, w;
}es[M];
int head[N], dis1[N], dis2[N], path1[N], path2[N], vis[N];
int tot, n, m, k, s, e, ans, min1, min2, x, y, z, minpath = inf;
bool flag = false;

void add(int x,int y,int z)
{
	es[++tot].to = y;
	es[tot].next = head[x];
	head[x] = tot;
	es[tot].w = z;
}

priority_queue<pair<int, int> > q;

void dijkstra(int s,int *dis,int *path)
{
	memset(vis, 0, sizeof(vis));
	for (int i = 1; i <= n; i++)
		dis[i] = inf, path[i] = -1;
	while (q.size())
		q.pop();
	dis[s] = 0;
	q.push(make_pair(0, s));
	while (q.size())
	{
		int x = q.top().second;
		q.pop();
		if (vis[x])
			continue;
		vis[x] = 1;
		for (int i = head[x]; i; i = es[i].next)
		{
			int y = es[i].to, w = es[i].w;
			if (dis[y] > dis[x] + w)
			{
				dis[y] = dis[x] + w;
				q.push(make_pair(-dis[y], y));
				path[y] = x;
			}
		}
	}
}

int main()
{
	while (scanf("%d %d %d", &n, &s, &e) != EOF)
	{
		ans = 0; tot = 0; minpath = inf;
		memset(head, 0, sizeof(head));
		scanf("%d", &m);
		for (int i = 1; i <= m; i++)
		{
			scanf("%d %d %d", &x, &y, &z);
			add(x, y, z);
			add(y, x, z);
		}
		dijkstra(s, dis1, path1);
		dijkstra(e, dis2, path2);
		scanf("%d", &k);
		for (int i = 1; i <= k; i++)
		{
			scanf("%d %d %d", &x, &y, &z);
			if (dis1[x] + dis2[y] + z < minpath) 
			{
				min1 = x; 
				min2 = y; 
				minpath = dis1[x] + dis2[y] + z; 
				ans = x;
			}
			if (dis2[x] + dis1[y] + z < minpath) 
			{
				min1 = y; 
				min2 = x; 
				minpath = dis2[x] + dis1[y] + z; 
				ans = y;
			}
		}
		if (!flag) 
			flag = true;
		else 
			putchar('\n');
		if(dis1[e]<minpath)
        {
            for (int i=e; i!=s; i=path1[i])
                v.push_back(i);
            v.push_back(s);
            for (int i=v.size()-1; i>=1; i--)
                printf("%d ",v[i]);
            printf("%d\nTicket Not Used\n%d\n",v[0],dis1[e]);
        }
        else 
		{
            for (int i=min1; i!=s; i=path1[i])
                v.push_back(i);
            v.push_back(s);
            for (int i=min2; i!=e; i=path2[i])
                v.insert(v.begin(),i);
            v.insert(v.begin(),e);
            for (int i=v.size()-1; i>=1; i--)
                printf("%d ",v[i]);
            printf("%d\n%d\n%d\n",v[0],ans,minpath);
        }
        v.clear();
	}
	return 0;
}
Published 32 original articles · praised 0 · visits 674

Guess you like

Origin blog.csdn.net/qq_43814559/article/details/105545249