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.
输入描述:
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.
示例1
输入
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
dijkstra+dfs
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=510;
const int inf=0x3f3f3f3f;
int G[N][N];
int dst[N];
int vst[N];
int weight[N];
int c,n,s,m;
int mineed=inf,minback=inf;
vector<int>pre[N],path,tpath;
void dijkstra(int p){
memset(vst,0,sizeof(vst));
for(int i=0;i<=n;i++){
dst[i]=inf;
}
dst[p]=0;
for(int i=0;i<=n;i++){
int v=-1,min_d=inf;
for(int j=0;j<=n;j++){
if(!vst[j]&&dst[j]<min_d){
v=j;
min_d=dst[j];
}
}
if(v==-1) break;
vst[v]=1;
for(int j=0;j<=n;j++){
if(!vst[j]&&G[v][j]!=inf){
if(dst[j]>dst[v]+G[v][j]){
dst[j]=dst[v]+G[v][j];
pre[j].clear();
pre[j].push_back(v);
}else if(dst[j]==dst[v]+G[v][j]){
pre[j].push_back(v);
}
}
}
}
}
void dfs(int v){
tpath.push_back(v);
if(v==0){
int need=0,back=0;
for(int i=tpath.size()-1;i>=0;i--){
int id=tpath[i];
if(weight[id]>0){
back+=weight[id];
}else{
if(back>(0-weight[id])){
back+=weight[id];
}else{
need+=((0-weight[id])-back);
back=0;
}
}
}
if(need<mineed){
mineed=need;
minback=back;
path=tpath;
}else if(need==mineed&&back<minback){
minback=back;
path=tpath;
}
tpath.pop_back();
return;
}
for(int i=0;i<pre[v].size();i++){
dfs(pre[v][i]);
}
tpath.pop_back();
}
int main(){
scanf("%d%d%d%d",&c,&n,&s,&m);
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
if(i==j){
G[i][j]=0;
}else{
G[i][j]=inf;
}
}
}
int x=c/2;
for(int i=1;i<=n;i++){
scanf("%d",&weight[i]);
weight[i]-=x;
}
int u,v,w;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
G[u][v]=G[v][u]=min(G[u][v],w);
}
dijkstra(0);
dfs(s);
printf("%d ",mineed);
for(int i=path.size()-1;i>=0;i--){
if(i>0){
printf("%d->",path[i]);
}else{
printf("%d ",path[i]);
}
}
printf("%d\n",minback);
return 0;
}
/*
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
*/
参考网上的代码,觉得直接dfs更简单一些,附上别人的代码
const int maxn = 510;
int cmax,N,sp,m; //车站容量,车站数量,出事位置,道路位置
int tmap[500][510]; //各点间的距离
int station[maxn],vis[maxn]; //各个车站的车数量,访问标签
int min_time=1E9, ans_have=1E9, ans_need=1E9; //已知到达出事车站的最小时间 ,最小需要带的车, 最少带回来的车
vector<int>path; //到达出事车站时经过的路径
vector<int>ans_path; //符合答案要求的路径
//s:当前点,e需要到达的点, time当前时间 , tneed需要从基地带来的车, 需要带回去的车
void dfs(int s,int e,int time,int tneed,int thave) {
vis[s] = true;
path.push_back(s);
if(s!=0){
int need = (cmax/2) - station[s];
if(need > 0) { //需要往此站放车
tneed += (thave<need? need-thave : 0);
thave -= (thave<need? thave : need);
} else thave -= need; //需要在此站搬走一些车
}
if(s==e) {
if( time<min_time || (time==min_time && tneed<ans_need) ) { //更优解条件
min_time = time;
ans_path = path;
ans_need = tneed;
ans_have = thave;
}
} else {
for(int i=1; i<=N; i++) {
if(vis[i]==false && tmap[s][i]>0) {
int next_time = time + tmap[s][i];
if( next_time <= min_time) dfs(i,e,next_time,tneed,thave); //如果到下一步的时间已经比已知最短时间长,直接排除
}
}
}
vis[s] = false;
path.pop_back();
}
int main() {
cin>>cmax>>N>>sp>>m;
memset(tmap,-1,sizeof(tmap));
for(int i=1; i<=N; i++) cin>>station[i];
for(int i=0; i<m; i++) {
int ta,tb,tc;
cin>>ta>>tb>>tc;
tmap[ta][tb] = tmap[tb][ta] = tc;
}
dfs(0,sp,0,0,0);
cout<<ans_need<<" 0";
for(int i=1; i<ans_path.size(); i++) cout<<"->"<<ans_path[i];
cout<<" "<<ans_have<<endl;
}