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.
![](http://nos.patest.cn/11_lrrk0m5o4kg.jpg)
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 1
Sample Output:
3 0->2->3 0
题目大意是找一条最短路径,(走每条路径,都要把节点调整为最佳状态,带走或增加一些车子)有多条最短路径时,输出从管理中心最少带出的车子。带出的车子相同时,输出带回最少的车子。
dijkstra求最短路径有一些拓展问题:如何求出有多少条最短路径;如何输出路径;包含一些附加信息是我的处理
有多少条最短路径和输出最短路径可以归为一种解法。我们定义vector<int>q[n],q[i].size()表示最短路径的个数。通过dfs可以输出最短路径。
对于这个问题,我们还有限制条件。所以对所有的最短路径做一遍dfs,保存答案即可。
下面是代码:
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; int e[510][510]; int dis[510]; int c[510]; bool vis[510]; const int inf=0x3f3f3f3f; int cmax,n,s,m; vector<int>q[510]; vector<int>path,temppath; int mintake,minback; void dijkstra(int d) { memset(vis,0,sizeof(vis)); dis[0]=0; for(int i=1;i<=n;i++) dis[i]=e[0][i]; while(1) { int maxx=inf,tmp=-1; for(int i=0;i<=n;i++) { if(vis[i]==0&&dis[i]<maxx) { maxx=dis[i]; tmp=i; } } vis[tmp]=1; if(tmp==-1) break; for(int i=0;i<=n;i++) { if(vis[i]==0&&e[tmp][i]!=inf) { if(dis[i]>dis[tmp]+e[tmp][i]) { q[i].clear(); q[i].push_back(tmp); dis[i]=dis[tmp]+e[tmp][i]; } else if(dis[i]==dis[tmp]+e[tmp][i]) { q[i].push_back(tmp); } } } } } void dfs(int v) { temppath.push_back(v); if(v==0) { int back=0,take=0; for(int i=temppath.size()-1;i>=0;i--) { int id=temppath[i]; if(c[id]>0) back+=c[id]; //需要带走的 else { if(back>(0-c[id]))//如果之前带走的大于当前需要的 { back+=c[id]; } else { take+=-(back+c[id]); back=0; } } } if(take<mintake) { mintake=take; minback=back; path=temppath; } else if(take==mintake&&back<minback) { mintake=take; minback=back; path=temppath; } temppath.pop_back(); return; } for(int i=0;i<q[v].size();i++) dfs(q[v][i]); temppath.pop_back(); } int main() { mintake=inf;minback=inf; scanf("%d%d%d%d",&cmax,&n,&s,&m); int a,b,cost; memset(e,inf,sizeof(e)); for(int i=1;i<=n;i++) { scanf("%d",&c[i]); c[i]-=cmax/2; } for(int i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&cost); e[a][b]=e[b][a]=cost; } dijkstra(s); dfs(s); printf("%d 0",mintake); for(int i=path.size()-2;i>=0;i--) cout<<"->"<<path[i]; cout<<" "<<minback<<endl; }
类似的问题还有PAT1003,也是多条最短路径的限制问题。