171,204 route ccf

reference

http://www.pianshen.com/article/5980255339/

(Code comments refer to the website of the "road plus road" and "road to increase the road" backwards, site description of the algorithm spfa of great reference value)

Thinking

lane road separately spfa +

achieve

Pure path between the draw point by Floyd shortest path algorithm, by dis [wide] [i] when the road to the source storage last road i shortest path, dis [narrow] [i] stores the last path when a path i point to the source of the shortest path, so spfa to consider "the road to increase the road" "road to increase the road" three conditions "plus lane road."

  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 #define MAXN 505
  6 
  7 typedef long long ll;
  8 
  9 int n,m;
 10 ll G[2][MAXN][MAXN];
 11 ll dis[2][MAXN];
 12 bool inque[MAXN];
 13 queue<int> q;
 14 
 15 const ll inf=1e18;
 16 const int wide=0;
 17 const int narrow=1;
 18 
 19 int spfa(int start,int n){
 20     //初始化 
 21     for(int i=1;i<=n;i++){
 22         dis[0][i]=dis[1][i]=inf;
 23         inque[i]=false;
 24     }
 25     //start入队 
 26     q.push(start);
 27     inque[start]=true;
28      DIS [Wide] [Start] = DIS [Narrow] [Start] = 0 ;
 29      // SPFA calculates the shortest path 
30      int U; 
 31 is      LL V;
 32      the while (! Q.empty ()) {
 33 is          // dequeue 
34 is          U = q.front ();
 35          q.pop ();
 36          inque [U] = to false ;
 37 [          
38 is          for ( int I = . 1 ; I <= n-; I ++ ) {
 39              V = G [Wide] [U ] [I];
 40              // road to increase the passage 
41 is              IF(dis[wide][i]>dis[wide][u]+v){
 42                 dis[wide][i]=dis[wide][u]+v;
 43                 if(!inque[i]){
 44                     q.push(i);
 45                     inque[i]=true;
 46                 }
 47             }
 48             //小路加大路
 49             if(dis[wide][i]>dis[narrow][u]+v){
 50                 dis[wide][i]=dis[narrow][u]+v;
 51                 if(!inque[i]){
 52                     q.push(i);
 53                     inque[i]=true;
 54                 }
 55             }
 56             //大路加小路 
 57             v=G[narrow][u][i];
 58             if(v!=inf&&dis[narrow][i]>dis[wide][u]+v*v){
 59                 dis[narrow][i]=dis[wide][u]+v*v;
 60                 if(!inque[i]){
 61                     q.push(i);
 62                     inque[i]=true;
 63                 }
 64             }
 65         }     
 66     }
 67     return min(dis[wide][n],dis[narrow][n]);
 68 }
 69 
 70 int main(){
 71     cin>>n;
 72     cin>>m;
 73     //初始化 
 74     for(int i=1;i<=n;i++){
 75         for(int j=1;j<=n;j++){
 76             G[0][i][j]=G[1][i][j]=inf;
 77         }
 78     }
 79     //Input side 
80      int type, U, V, W;
 81      for ( int I = 0 ; I <m; I ++ ) {
 82          CIN type >> >> >> U V >> W;
 83          IF (G [type] [ U] [V]> W) {                     // Note that there are multiple edges in the case of 
84              G [type] [U] [V] = G [type] [V] [U] = W;        
 85          }
 86      } 
 87      // the Floyd algorithm even small side small side case 
88      for ( int I = . 1 ; I <= n-; I ++ ) {
 89          for ( int J = I + . 1;j<=n;j++){
 90             for(int k=1;k<=n;k++){
 91                 if(k==i||k==j){
 92                     continue;
 93                 }
 94                 if(G[narrow][i][j]>G[narrow][i][k]+G[narrow][k][j]){
 95                     G[narrow][i][j]=G[narrow][j][i]=G[narrow][i][k]+G[narrow][k][j];
 96                 }
 97             }
 98         }
 99     } 
100     
101     cout<<spfa(1,n);
102     
103     return 0;
104 }
View Code

note

Case of duplicate input side, the size of intermediate results may exceed int

topic

Problem Description
  Xiao Ming and Xiao Fang out to the countryside to play, Xiao Ming is responsible for driving, Xiaofang to navigate.
  Xiaofang will likely Avenue and the road is divided into lanes. Relatively good road to go, every one kilometer Xiao Ming will increase fatigue 1. Trail go bad, if the continuous walking trail, Xiao Ming fatigue value will increase rapidly take successive s km Xiaoming will increase s 2 degree of fatigue.
  For example: five junctions, junction 1 is the path to the junctions 2, No. 2 No. 3 to the intersection of the intersection path, No. 3 No. 4 is the junction to junction Avenue, No. 4 No. 5 to the intersection of the intersection path, adjacent to the intersection It is two kilometers distance between. If Bob from intersection to intersection No. 5 No. 1, the total fatigue value (2 + 2) 2 + 2 2 + 2 = 16 + 4 + 2 = 22.
  Xiaofang now got the map, please help her plan a driving route, making this route by car Xiao Ming minimum degree of fatigue.
 
Input Format
  The first line of input contains two integers n- m , represent the number of intersections and roads. 1 from the intersection to n number Xiaoming need to drive from Junction 1 to n number junction.
  Next m lines describe the road, each line contains four integers T a b c , denotes a type T , connecting a and b two junctions, the length c km two-way road. Wherein t 0 represents Avenue, t 1 indicates the trail. 1 to ensure junction and n number junction is connected.
 
Output Format
  Output an integer representing the degree of fatigue of the optimal route Xiaoming.
 
Sample input
6 7
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1
 
Sample Output
76
 
Sample Description
  Take the trail from 1 to 2, 3 to walk path, fatigue 5 2 = 25; and 3 away from the road through 4 reaches 5, the fatigue of 20 + 30 = 50; and finally take the trail from 5 to 6, fatigue 1. A total of 76.
 
Scale data and conventions
  For 30% of the evaluation use cases. 1 ≤  n-  ≤ 8,1 ≤  m  ≤ 10;
  for the other 20% of the evaluation with the embodiment, the trail does not exist;
  for the other 20% of the evaluation with the embodiment, all of the trail disjoint;
  for all reviews use cases, ≤. 1  n-  ≤ 500,1 ≤  m  ≤ 10 . 5 ,. 1 ≤  A B  ≤  n- , T is 0 or. 1, C   ≤ 10 . 5 . The answer to ensure that no more than 10 6 .

Guess you like

Origin www.cnblogs.com/Gru-blog/p/11271903.html
Recommended