Rokua P1119 Post-disaster reconstruction [Floyd]

Source: Internet
Author: User

Topic background

B area After the earthquake, all the villages caused some damage, but the earthquake did not affect the road. But before the villages were rebuilt, all the roads to the villages that had not been rebuilt were not open to traffic. In other words, only the roads connecting the two rebuilt villages can be opened to the villages where they were rebuilt.

Title Description

Given the number of villages in area B, the village number from 0 to N-1, and the length of all M-roads, the road is bidirectional. And given the time for the completion of the first village reconstruction T[i], you can assume that the reconstruction is started at the same time and rebuilt in the first t[i] days and will be open on the same day. If T[i] is 0, it means that the earthquake did not cause damage to the area and could be opened to traffic at the outset. After that there is Q a query (x, y, T), for each query you want to answer on the first day, the shortest path length from village x to village Y is how much. If you cannot find the path from village X to Y village, after several rebuilt villages, or village X or village y is still not rebuilt on T-day, you need to return-1.

Input/output format

Input format:

The first line of the input file rebuild.in contains two positive integer n,m, indicating the number of villages and the number of roads.

The second line contains N non-negative integers t[0], t[1], ..., t[n–1], indicating the time each village was rebuilt, the data guaranteed t[0]≤t[1]≤ ... ≤T[N–1].

The next M-line, 3 nonnegative integers i, j, and W,w for a positive integer of not more than 10000, indicates that there is a road connecting the village I with the village J, the length is W, the i≠j is guaranteed, and there is only one road for any pair of villages.

The next line, the M+3 line, contains a positive integer, q, which indicates Q queries.

The next Q line, 3 nonnegative integers x, y, T in each line, asks how long the shortest path length from village x to village Y is, and the data guarantees that T is not descending.

Output format:

The output file Rebuild.out contains the Q line, which corresponds to the answer for each query (x, y, T), that is, the shortest path length from village x to village Y on the day of the week. If the path from X village to Y village cannot be found on the first day, after several rebuilt villages, or village X or village Y is still not repaired on day T, then output-1.

Input and Output Sample input example # #:
4 51 2 3 40 2 12 3 13 1 22 1 40 3 542 0 20 1 20 1 30 1 4
Sample # # of output:
-1-154
Description

For 30% of the data, there are n≤50;

For 30% of the data, there is t[i] = 0, of which 20% of the data has t[i] = 0 and n>50;

For 50% of the data, there are q≤100;

For 100% of the data, there is n≤200,m≤n* (N-1)/2,q≤50000, all input data involving integers are not more than 100000.

Can not underestimate the application of Floyd in n hours

Floyd Cycle to K, the request is not through the K two points between the shortest, is suitable for the subject, even offline processing has been saved, has ordered

Add a t[n+1]=inf for convenience

Note that meeting X and Y fixes this condition.

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;Const intn=205, inf=1e9+5; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,q,d[n][n],t[n],a,b,w;intNow,x,y;inlinevoidRe () {x=read () +1; Y=read () +1; Now=read (); q--;//printf ("Re%d%d%d\n", x-1,y-1,now);}intMain () {n=read (); m=read ();  for(intI=1; i<=n;i++) t[i]=read ();  for(intI=1; i<=n;i++) for(intj=i+1; j<=n;j++) d[i][j]=d[j][i]=INF;  for(intI=1; i<=m;i++) {a=read () +1; B=read () +1; W=read ();d [a][b]=d[b][a]=W;} Q=read (); T[n+1]=INF;    Re ();  for(intk=1; k<=n+1; k++){         while(now<T[k]) {            if(d[x][y]==inf| | t[x]>now| | T[y]>now) puts ("-1"); Elseprintf"%d\n", D[x][y]); if(! Q)return 0;        Re (); }          for(intI=1; i<=n;i++)             for(intj=1; j<=n;j++) D[i][j]=min (d[i][j],d[i][k]+D[k][j]); }    return 0;}

Rokua P1119 Post-disaster reconstruction [Floyd]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.