1961 Avoiding Tai Lung
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description
Description
You wake up in the morning, leisurely to the school gate, found that it is eight o'clock the whole! (There is a more important condition in this sentence)
There are N locations in the school, numbered 1~n, where 1th is the school gate (which is where you are now), and number 2nd is your classroom (i.e. your destination). Between these sites there is m two-way road, for the first road, in order not to arouse the suspicion of the week team teacher, you pass it time must be exactly ti seconds. This number may be negative, meaning the time is backwards.
However, even without suspicion, the team also made the last line of defense: Tai Lung will appear in the classroom at irregular intervals. Of course, you also understand the behavior of Tai Lung: the smaller the number of seconds in the current time, the lower the probability of the occurrence of the dragon, for example: 8:13:06 the moment is 06 seconds, which is more secure than 8:12:57.
The question now is how many seconds of the safest time to arrive without suspicion. If the school entrance to the classroom does not have the road (-_-| |), please output 60.
Note that you can choose to pass the classroom at any time on the way, rather than end the "journey", see the sample.
Enter a description
Input Description
The first act of two integers, N and M, meaning is already mentioned above.
Line 2nd ~ Line M+1, each row represents a road. Line I+1 represents the road of section I, which has 3 integers, ai,bi,ti, which indicates that the Ai location has a two-way road with the Bi location, and that the time must be Ti seconds.
Output description
Output Description
Only one row, the number of seconds to the safest time to arrive.
Sample input
Sample Input
INPUT1:
2 1
2 1 54
Input2:
3 3
1 2 26
1 3 17
2 3-9
INPUT3:
3 1
1 3 110
INPUT4:
2 2
1 2 7
2 1 9
INPUT5:
2 2
1 2 3
1 1 1
INPUT6:
2 2
1 2 9
1 2 11
Sample output
Sample Output
OUTPUT1:
06
Output2:
00
OUTPUT3:
60
OUTPUT4:
01
OUTPUT5:
00
OUTPUT6:
01
Data range and Tips
Data Size & Hint
Description of Example 1: There are only two locations (how well-being data ah), there is only one road, time is 54 seconds. The best solution is to go through this road 9 times, take 486 seconds, which is 8 minutes and 06 seconds, and arrive at 8:08:06 in the classroom. Of course, the optimal scheme is not unique.
Example 2 Description: Walk 1->3->1->2, Time 17+17+26, arrive at 8:01:00, or go 1->2->3->1->2, time 26-9+17+26, arrive at 8:01:00.
For 20% data, n≤2; for 40% data, n≤100; for 70% data, n≤1000;
For 100% of data, 2≤n≤7000,0≤m≤9000,1≤ai,bi≤n,| ti|≤109.
Category labels
Tags Click here to expandSPFA Breadth First Search Depth First search search graph theory
/*The topic asks us to find a 1->2 the safest way, so we can start Dfs from 1, then AC Yes, that's it.*/#include<cstdio>#include<iostream>using namespacestd;#defineN 7001inte[n*2+ the][3],pd[n][ -],head[n],tot,n,m;voidAddintUintVintW) {//compiling tables, recording pathse[++tot][0]=u; e[tot][1]=W; e[tot][2]=Head[v]; HEAD[V]=tot;}voidDfsintXintY) {//x: node change; y: Time consuming if(Pd[x][y])return ; Pd[x][y]=1;//pd[x][0..59] Indicates whether each point can be reached in each second intC=Head[x]; while(c) {DFS (e[c][0], ((y+e[c][1])% -+ -)% -);//have negative numbersc=e[c][2]; }}intMain () {scanf ("%d%d",&n,&m); for(intI=1, u,v,w;i<=m;i++) scanf ("%d%d%d",&u,&v,&W), add (U,v,w), add (V,U,W); DFS (1,0); for(intI=0; i<= -; i++){ if(pd[2][i]) {if(i<Ten) printf ("0");//complement 0printf"%d\n", i);//after 0->, output optimal value, end return 0; }} printf ("60\n"); return 0;}
1961 Avoiding Tai Lung