Title Link: http://acm.swust.edu.cn/problem/842/
Time limit (ms): $ Memory Limit (KB): 10000
Description
2012 The new summer camp began, everything is still pretty good, but there is a problem of students, that is, from the laboratory to the canteen, the weather is very hot, so that everyone try to avoid from the place where there is no shade, but some paths have to go from the place where there is no shade, So now the puzzle comes out and gives you some one-way sections that let you figure out the length of the path from the lab to the canteen, the least sun, and the total length of the path. If there is more than one path to the sun, the total length is minimal.
Enter N and M, indicating that there is a n<=1000, M-Path, and then input m (m<=100000) path, each path consists of 4 data, S,E,DIS1, DIS2, respectively, indicating the starting point of the path,
The end point, the total length of the path, and the length of the path without the shade (DIS1>=DIS2);
Results guaranteed to be less than 2^31
Input
Output two values, the first value is the minimum total length without a shaded path, the second is the total length of the path, and if the minimum total length of the shaded path is equal,
Output
The length value of the path that outputs the smallest total path length in both.
Sample Input
3 41 2 10 21 2 11 12 3 5 22 3 7 14 41 2 10 22 4 10 31 3 5 33 4 16 2 |
Sample Output
Problem-solving ideas: The starting point is 1, the end is N, two arrays, a storage path length, one is the minimum sun to the length of the sun, and then using the Dijkstra algorithm is ~ ~ ~ Code as follows:
1#include <iostream>2#include <cstring>3 Const intMAXN =1005;4 Const intINF =0x3f3f3f3f;5 using namespacestd;6 7 intDIAA[MAXN][MAXN], DISB[MAXN][MAXN], N, m, x, y, D1, D2;8 9 voidInit ()Ten { One for(inti =1; I <= N; i++){ A for(intj = i; J <= N; J + +) -DIAA[I][J] = Diaa[j][i] = disb[i][j] = Disb[j][i] =inf; - } the } - voidDijkstra () { - intDIS[MAXN], PAY[MAXN], VIS[MAXN], I, J, K; -memset (Vis,0,sizeof(Vis)); + for(i =1; I <= N; i++){ -Dis[i] = diaa[1][i]; +Pay[i] = disb[1][i]; A } atvis[1] =1; - for(i =1; I <= N; i++){ - intMin =inf; - for(j =1; J <= N; J + +){ - if(!vis[j] && Pay[j] <min) { -Min =Pay[j]; inK =J; - } to } +VIS[K] =1; - for(j =1; J <= N; J + +){ the if(!Vis[j]) { * if(Pay[k] + disb[k][j]<pay[j] | | pay[k] + disb[k][j] = = Pay[j] && dis[j]>dis[k] +Diaa[k][j]) { $PAY[J] = Pay[k] +Disb[k][j];Panax NotoginsengDIS[J] = Dis[k] +Diaa[k][j]; - } the } + } A } thecout << Pay[n] <<' '<< Dis[n] <<Endl; + } - $ intMain () { $ while(Cin >> N >>m) { - init (); - for(inti =0; I < m; i++){ theCin >> x >> y >> d1 >>D2; - if(D2 < disb[x][y] | | d2 = disb[x][y] && D1 <Diaa[x][y]) {WuyiDiaa[x][y] =D1; theDisb[x][y] =D2; - } Wu } - Dijkstra (); About } $ return 0; -}
View Code
[Swust OJ 842]--Laboratory and canteen (shortest way, Dijkstra algorithm)