【PAT1018】Public Bike Management
2017-08-03 10:26
281 查看
题目描述:
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.
输入测试
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
输出结果
思路分析:
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.
输入测试
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<iostream> #include<fstream> #include<vector> using namespace std; #define MAX 505 #define INF 10000 int cap,N,sp,M; int vex; int dist[MAX][MAX]; int bike[MAX]; #define PF cap/2 vector<int> curpath; vector<int> shortpath; int minsend=INF,minback=INF; int minlen=INF; int cursend=0,curback=0; int curlen=0; bool visit[MAX]={0}; void dfs(int cur){ if(curlen>minlen) return; if(cur==sp){ //到达目标点,看是否最优 if(curlen<minlen){ minlen=curlen; minsend=cursend; minback=curback; shortpath=curpath; } else if(curlen==minlen){ if(cursend<minsend||(cursend==minsend&&curback<minback)){ minsend=cursend; minback=curback; shortpath=curpath; } } return; } for(int i=1;i<vex;i++){ if(visit[i]==true||dist[cur][i]==INF) continue; int lastsend=cursend; int lastback=curback; //计算到达当前点的send和back数 if(bike[i]+curback<PF){ cursend+=PF-bike[i]-curback; curback=0; } else{ curback=bike[i]+curback-PF; } visit[i]=true; curpath.push_back(i); curlen+=dist[cur][i]; dfs(i); curpath.pop_back(); visit[i]=false; curlen-=dist[cur][i]; cursend=lastsend; curback=lastback; } } int main(){ cin>>cap>>N>>sp>>M; //初始化,距离置为INF vex=N+1; for(int i=0;i<vex;i++){ for(int j=0;j<vex;j++){ dist[i][j]=dist[j][i]=INF; } } for(int i=1;i<vex;i++){ cin>>bike[i]; } for(int k=0;k<M;k++){ int i,j; cin>>i>>j; cin>>dist[i][j]; dist[j][i]=dist[i][j]; } dfs(0); printf("%d 0",minsend); for(int i=0;i<shortpath.size();i++){ printf("->%d",shortpath[i]); } printf(" %d",minback); return 0; }
相关文章推荐
- 1018. Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS)
- PAT-1018 Public Bike Management (30)
- PAT-1018 Public Bike Management (30)
- PAT1018 Public Bike Management
- 1018. Public Bike Management (30)——PAT (Advanced Level) Practise
- PAT 1018 Public Bike Management(Dijkstra 最短路)
- PAT (Advanced Level) Practise 1018 Public Bike Management (30)
- PAT 1018 Public Bike Management(Dijkstra 最短路)
- PAT-1018 Public Bike Management(dijkstra + dfs)
- PAT-Java-1018-Public Bike Management (30)
- PAT (Advanced Level)1018. Public Bike Management (30) 迪杰斯特拉算法 DFS 递归(难)
- PAT 1018 Public Bike Management DFS+SPFA
- PAT 1018 Public Bike Management
- PAT 甲级 1018 Public Bike Management
- PAT (Advanced Level) Practise 1018 Public Bike Management (30)
- 1018. Public Bike Management (30) PAT 甲级
- PAT(甲级)1018. Public Bike Management (30)
- 1018. Public Bike Management (30)
- 1018. Public Bike Management (30)
- 1018. Public Bike Management (30)