Topic: Post-Disaster Reconstruction

【Description】

After the earthquake in area B, all the villages were damaged to some extent, but the earthquake did not have any impact on the road. But until the villages are rebuilt, all the roads to the villages that have not been rebuilt cannot be opened to traffic. In other words, only the road connecting the two rebuilt villages can be opened to traffic, and only the rebuilt village can be reached.
  Given the number N of villages in area B, the number of villages from 0 to N-1, and the length of all M roads, the roads are bidirectional. And give the time t[i] when the reconstruction of the i-th village is completed, you can think that the reconstruction started at the same time and the reconstruction was completed on the t[i] day, and it will be open to traffic on the same day. If t[i] is 0, it means that the earthquake did not cause damage to this area, and it can be opened to traffic from the beginning. Then there are Q queries (x, y, t), for each query you have to answer the length of the shortest path from village x to village y on day t. If the path from village x to village y cannot be found, after several villages have been rebuilt, or village x or village y has not been rebuilt on day t, you need to return -1.

input and output format

Input format:

The first line of the input file rebuild.in contains two positive integers N, M, which represent the number of villages and the length of the road.
  The second line contains N non-negative integers t[0], t[1], …, t[N – 1], indicating the completion time of each village reconstruction, and the data guarantees that t[0] ≤ t[1] ≤ … ≤ t[N – 1].
  Next M lines, each line contains 3 non-negative integers i, j, w, w is a positive integer not exceeding 10000, indicating that there is a road connecting village i and village j, the length is w, and i≠j is guaranteed, and For any pair of villages, only one road exists.
  The next line, that is, line M+3 contains a positive integer Q, indicating Q queries.
  The next Q line, each line contains 3 non-negative integers x, y, t, asking what is the shortest path length from village x to village y on day t, and the data guarantees that t will not decrease.

Output format:

The output file rebuild.out contains Q lines, and outputs the corresponding answer for each query (x, y, t), that is, what is the length of the shortest path from village x to village y on day t. If the path from village x to village y cannot be found on day t, and passes through several rebuilt villages, or village x or village y has not been repaired on day t, output -1.

Input and output samples

Input example #1:

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

Sample output #1:

-1
-1
5
4

train of thought

This is actually a shortest path problem, using the Floyd algorithm , but we need to add some stuff in the process of Floyd.
Our 3-layer cycle of floyd is as follows:

for (int k=1;k<= n;k++) 
     for (int i = 1;i <= n;i++) 
          for (int j=1;j <= n;j++) 
              w[i][j]=min(w[i][j],w[i][k]+w[k][j]);

The k-layer loop (first layer) here enumerates which points are passed as intermediate points to shorten the distance from i->j. It can be understood that the first k points are used as intermediate points to try to update the distance between any two points. , which can be used by us.
Take a look at our query: x, y, time If t[x] or t[y]>time, it will definitely output -1. For others, we can add the following things after the i and j cycles in the k-level cycle are completed.
That is:
for (k=1->n) { for (i=1->n) for (j =1->n) ... add what we said below at this position; } The
k-level loop is still a list Take n points. If t[k]<=a[now].time and t[k+1] <=a[now].time then k can continue to enumerate. Indicates that we can use k and k+1 as intermediate points to update the distance between any two points. If you encounter t[k]<=a[now].time and t[k+1]>a[now].time. It means that we can only use k at most as the intermediate point to update the distance between any two points. At this time, we can only try to output w[x][y] after using the first k points. The point of k+1 can no longer be used. Because the point k+1 has not been built at a[now].time.
After encountering such k. now++.(a[now].time is incremented with the increase of now).
If t[k+1]<=a[now].time after now is incremented. Then you can continue to use k+1 as the intermediate point to update the distance between any two points.
Then we change the lower bound of the k-level loop to 0. Because there may be a query at time 0

That's probably it, you can look at the code

AC code

#include <cstdio>
#include <cstring>
 
struct question //用结构体把询问存下来。
{
    
    
	int x, y, time;
};
 
int n, m, t[201] = {
    
     0 }, w[201][201], q; //t数组是各个节点修建好的时间。
question a[50001] = {
    
     0 };
 
void input_data()
{
    
    
	memset(w, 127 / 3, sizeof(w));//一开始w数组赋值为一个很大的数字。
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) //输入各个节点修建好的时刻。
		scanf("%d", &t[i]);
	for (int i = 1; i <= m; i++) //输入边权信息。
	{
    
    
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		x++; y++;
		w[x][y] = w[y][x] = z;
	}
	scanf("%d", &q);
	for (int i = 1; i <= q; i++) //输入q个询问。
	{
    
    
		scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].time);
		a[i].x++;
		a[i].y++;
	}
}
 
void get_ans()
{
    
    
	int now = 1;
	t[n + 1] = t[n] + 10000; //这是防止上溢。
	for (int k = 0; k <= n; k++) //k从0开始枚举
	{
    
    
		for (int i = 1; i <= n; i++) //以k作为中间节点尝试更新任意两点之间的距离。
			for (int j = 1; j <= n; j++)
				if (w[i][j] > w[i][k] + w[k][j])
					w[i][j] = w[i][k] + w[k][j];
		while (now <= q && t[k] <= a[now].time && t[k + 1] > a[now].time)
		{
    
    //如果询问还没结束。且这个节点在所询问的时间内。且k+1这个节点修建的时间超过询问的时间
			if (t[a[now].x] > a[now].time || t[a[now].y] > a[now].time)
				printf("-1\n");
			else //输出依靠前k个节点作为中间节点更新出的任意两点之间的距离
			{
    
    
				if (w[a[now].x][a[now].y] >= w[0][0])
					printf("-1\n");
				else
					printf("%d\n", w[a[now].x][a[now].y]);
			}
			now++; //看一下下一个询问是否符合要求。
		}
		if (now > q) //如果询问都输出了则结束。
			break;
	}
}
 
int main()
{
    
    
	input_data();
	get_ans();
	return 0;
}

Don't forget to like it ( * ^ ▽ ^ * )

Guess you like

Origin blog.csdn.net/m0_61360607/article/details/132206267