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.
The above figure 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:
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.
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.
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
3 0->2->3 0
题目给出每个站点的容纳量、站点总数和需要调节的站点编号,并给出各站点间的路径信息。在前往目的站点时总选择最短路径,并调节途中所有结点到完美状态,如果有多条最短路径则选择需要从中心携带最少自行车的路径。并将该路径输出。
#include
#define maxn 510
#define INF 100000000
int G[maxn][maxn];//邻接矩阵
int d[maxn];//距离数组
int vis[maxn];//访问数组
int needs[maxn];//需求的数量
int C,N,S,M;//最大数量C,站点数N,目标站点S,路数M
int temp[maxn]={0},path[maxn]={0};//临时路径,最短路径
int mindis,mintake=INF,minremain=INF;//最小距离
int pre[maxn][maxn];
int prenum[maxn];
void Init(){
int i,j;
int a,b,v;
scanf("%d%d%d%d",&C,&N,&S,&M);
C=C/2;
for(i=1;i<=N;i++){
scanf("%d",&needs[i]);
needs[i]=C-needs[i];
}//初始化needs数组
for(i=0;i<=N;i++){
d[i]=INF;
vis[i]=0;
for(j=0;j<=N;j++){
G[i][j]=INF;
}
G[i][i]=0;
}
for(i=0;i=0;i--){
v=temp[i];
if(remain>=needs[v]){//剩余数量可以满足
remain=remain-needs[v];//更新剩余数量
}else{//不够数
take+=needs[v]-remain;//不够数,remain清零,剩余部分打白条
remain=0;
}
}
if(take=0;i--){
path[j++]=temp[i];//将temp记录于path
}
}else if(take==mintake&&remain=0;i--){
path[j++]=temp[i];//将temp记录于path
}
}
return;
}
for(i=0;i%d",path[i]);
if(path[i]==S){
break;
}
}
printf(" %d\n",minremain);
return 0;
}