There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.
Figure 1
Figure 1 illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3, we have 2 different shortest paths:
1. PBMC -> S1 -> S3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3, so that both stations will be in perfect conditions.
2. PBMC -> S2 -> S3. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; Sp, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci (i=1,...N) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0->S1->...->Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp is adjusted to perfect.
Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.
Sample Input:10 3 3 5 6 7 0 0 1 1 0 2 1 0 3 3 1 3 1 2 3 1Sample Output:
3 0->2->3 0
1 #include<stdio.h> 2 #include<vector> 3 #include<cmath> 4 using namespace std; 5 #define MAX 510 6 int INF = 1000000; 7 int wight[MAX]; 8 int Grap[MAX][MAX]; 9 int d[MAX]; //距离 10 bool visit[MAX]; 11 vector<int> adj[MAX],tempath,path; 12 int MINNeed = INF; 13 int MINRemain = INF; 14 15 16 void Dijkstra(int begin,int NodeNum) 17 { 18 d[begin] = 0; 19 for(int i=0 ; i<= NodeNum ; i++) 20 { 21 int index = -1; 22 int MIN = INF ; 23 for(int j = 0; j <= NodeNum ; j ++) 24 { 25 if(visit[j] == false && d[j] < MIN) 26 { 27 index = j ; 28 MIN = d[j]; 29 } 30 } 31 if(index == -1) return; 32 33 visit[index] = true; 34 for(int v =0 ; v<= NodeNum ;v++) 35 { 36 if(visit[v] == false && Grap[index][v] != INF) 37 { 38 if(d[index] + Grap[index][v] < d[v]) 39 { 40 d[v] = d[index] + Grap[index][v]; 41 adj[v].clear(); 42 adj[v].push_back(index); 43 } 44 else if(d[index] + Grap[index][v] == d[v]) 45 { 46 adj[v].push_back(index); 47 } 48 } 49 } 50 } 51 } 52 53 54 void DFS(int Sp) 55 { 56 if(Sp == 0) 57 { 58 tempath.push_back(Sp); 59 int need= 0;//需要带出去的 60 int remain = 0 ;//回收的 61 for(int i = tempath.size() - 1; i >= 0 ;i--) 62 { 63 int index = tempath[i]; 64 if(wight[index] > 0) 65 { 66 remain += wight[index]; 67 } 68 else 69 { 70 if(remain > abs(wight[index])) 71 { 72 remain += wight[index]; 73 } 74 else 75 { 76 need += abs(wight[index]) - remain; 77 remain = 0; 78 } 79 } 80 81 } 82 83 if(need < MINNeed) 84 { 85 MINNeed = need ; 86 MINRemain = remain; 87 path = tempath; 88 } 89 else if(need == MINNeed && remain < MINRemain) 90 { 91 MINRemain = remain; 92 path = tempath; 93 } 94 95 tempath.pop_back(); 96 return ; 97 } 98 tempath.push_back(Sp); 99 for(int i = 0;i < adj[Sp].size() ;i++) 100 { 101 DFS(adj[Sp][i]); 102 } 103 tempath.pop_back(); 104 } 105 106 int main() 107 { 108 int i,j,Cmax,N,Sp,M; 109 scanf("%d%d%d%d",&Cmax,&N,&Sp,&M); 110 for(i = 1 ; i <= N ;i ++) 111 { 112 scanf("%d",&wight[i]); 113 wight[i] -= (Cmax/2); 114 } 115 116 117 for(i = 0 ; i <= N ; i ++) 118 { 119 for(j = 0; j <= N;j ++) 120 { 121 Grap[i][j]= INF; 122 } 123 124 d[i] = INF; 125 } 126 int x,y; 127 for(i = 0 ; i < M ; i ++) 128 { 129 scanf("%d%d",&x,&y); 130 scanf("%d",&Grap[x][y]); 131 Grap[y][x] = Grap[x][y]; 132 } 133 134 Dijkstra(0,N); 135 136 DFS(Sp); 137 printf("%d ",MINNeed); 138 for(i = path.size() -1;i>=0 ; i --) 139 { 140 if(i == path.size()-1) 141 printf("%d",path[i]); 142 else printf("->%d",path[i]); 143 } 144 printf(" %d ",MINRemain); 145 return 0; 146 }