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 was said to being in perfect condition if it was exactly half-full. If a station was full or empty, PBMC would collect or send bikes to adjust the condition of the. And more, all the stations on the the-the-adjusted as well.
When a problem station is reported, PBMC'll always choose the shortest path to reach this station. If there is more than one shortest path, the one that requires the least number of bikes sent from PBMC would be chosen.
Figure 1
Figure 1 illustrates an example. The stations is represented by vertices and the roads correspond to the edges. The number on a 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 all 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 is sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3, so that B OTH stations'll is 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 are the one that would be chosen.
Input Specification:
Each input file contains the one test case. For each case, the first line contains 4 Numbers:cmax (<=), always a even number, is the maximum capacity of each Station N (<=), the total number of stations; Sp, the index of the problem station (the stations was numbered from 1 to N, and PBMC was 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 was 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 is separated by a space.
Output Specification:
For each test case, print your results on one line. First output the number of bikes that PBMC must send. Then after one space, the output of the path in the FORMAT:0->S1->...->SP. Finally after another space, output the number of bikes so we must take back to PBMC after the condition of Sp is adjust Ed to Perfect.
Note If such a path is not unique and output the one that requires minimum number of bikes so we must take back to PBM C. The judge ' s data guarantee that such a path is unique.
Sample Input:
10 3 3 56 7 00 1 10 2 10 3 31 3 12 3 1
Sample Output:
3 0->2->3 0
//hahaha.cpp: Defines the entry point of the console application. //#include<stdafx.h>#include<stdio.h>#include<iostream>#include<vector>#include<queue>#include<map>#include<string>#include<cstdio>#include<Set>#include<algorithm>#include<string.h>using namespacestd;Const intmaxv=510;Const intinf=1000000000;intN,m,cmax,sp,numpath=0, g[maxv][maxv]={0},WEIGHT[MAXV];intd[maxv],minneed=inf,minremain=INF;BOOLvis[maxv]={false};vector<int>Pre[maxv];vector<int>Tmppath,path;voidDij (ints) {Fill (d,d+Maxv,inf); for(intI=0; i<=n;i++) {pre[i].push_back (i);} D[s]=0; for(intI=0; i<n;i++) { intu=-1, min=INF; for(intj=0; j<=n;j++) { if(vis[j]==false&&d[j]<min) {u=J; Min=D[j]; } } if(u==-1)return; Vis[u]=true; for(intv=0; v<=n;v++) { if(vis[v]==false&&g[u][v]!=INF) { if(d[u]+g[u][v]<D[v]) {D[v]=d[u]+G[u][v]; Pre[v].clear (); Pre[v].push_back (U); } Else if(d[u]+g[u][v]==D[v]) {pre[v].push_back (U); } } } } }voidDasointv) {if(v==0) {tmppath.push_back (v); intNeed=0, remain=0; for(intI=tmppath.size ()-1; i>=0; i--) { intId=Tmppath[i]; if(weight[id]>0) {remain+=Weight[id]; } Else { if(remain>ABS (Weight[id])) {Remain-=ABS (Weight[id]); }Else{Need+=abs (Weight[id])-remain; Remain=0; } } } if(need<minneed) {Minneed=need; Minremain=remain; Path=Tmppath; } Else if(need==minneed&&remain<Minremain) {Minremain=remain; Path=Tmppath; } tmppath.pop_back (); return; } tmppath.push_back (v); for(intI=0; I<pre[v].size (); i++) {DFS (pre[v][i]); } tmppath.pop_back (); }intMain () {scanf ("%d%d%d%d",&cmax,&n,&sp,&m); intu,v; Fill (g[0],g[0]+maxv*Maxv,inf); for(intI=1; i<=n;i++) {scanf ("%d",&Weight[i]); Weight[i]-=cmax/2; } for(intI=0; i<m;i++) {scanf ("%d%d",&u,&v); scanf ("%d",&G[u][v]); G[v][u]=G[u][v]; } dij (0); DFS (SP); printf ("%d", Minneed); for(intI=path.size ()-1; i>=0; i--) {printf ("%d", Path[i]); if(i>0) printf (" -"); } printf ("%d", Minremain); return 0;}
A1018. Public Bike Management (30)